[swift] 연락처(Contacts) 정보를 인쇄하는 기능 추가 방법

iOS 앱에 연락처 정보를 인쇄하는 기능을 추가하는 방법을 알아보겠습니다.

1. 사용자 권한 요청

연락처에 접근하고 인쇄 기능을 사용하기 위해서는 사용자의 권한이 필요합니다. 앱에서 연락처에 접근하는 권한을 요청하는 코드를 추가해야 합니다.

import Contacts

func requestContactAccess() {
    let store = CNContactStore()
    store.requestAccess(for: .contacts) { (granted, error) in
        if granted {
            // 권한이 허용된 경우
        } else {
            // 권한이 거부된 경우
        }
    }
}

2. 연락처 정보 인쇄하기

인쇄 기능을 추가하려면 UIPrintInteractionController를 사용하여 연락처 정보를 인쇄할 수 있습니다.

import UIKit
import ContactsUI

func printContactInfo() {
    let contact = CNContact()
    let contactFormatter = CNContactFormatter.string(from: contact, style: .fullName)
    
    let printInfo = UIPrintInfo(dictionary: nil)
    printInfo.outputType = .general
    
    let printController = UIPrintInteractionController.shared
    printController.printInfo = printInfo
    printController.printFormatter = contactFormatter
    printController.present(animated: true) { (printController, completed, error) in
        if completed {
            // 인쇄 완료
        } else if let error = error {
            // 인쇄 오류
        }
    }
}

마치며

이제 앱에 연락처 정보를 인쇄하는 기능을 추가하는 방법을 알아보았습니다. 사용자 권한을 요청하고 UIPrintInteractionController를 사용하여 연락처 정보를 인쇄하는 방법을 살펴보았습니다.

더 많은 정보를 원하시면 Apple 공식 문서 를 참고하십시오.