訪問(wèn)通訊錄Contacts

// 是否開(kāi)通了訪問(wèn)權(quán)限
func getContacts(){
        phoneStr = ""
        let store = CNContactStore()
        let authorizationStatus = CNContactStore.authorizationStatusForEntityType(.Contacts)
        switch authorizationStatus {
        case .Authorized:
            self.retrieveContactsWithStore(store)
        case .Denied, .NotDetermined:
            store.requestAccessForEntityType(.Contacts, completionHandler: { (access: Bool, error: NSError?) in
                if access{
                    self.retrieveContactsWithStore(store)
                }else{
                    self.view.viewAlert(self, title: "提示", msg: "\"\(ToolKit.getProjectName())\"請(qǐng)求訪問(wèn)您的通訊錄", cancelButtonTitle: "取消", otherButtonTitle: "去設(shè)置", handler: { (buttonIndex, action) in
                        if buttonIndex == 1{
                            let settingUrl = NSURL(string: "prefs:root=Privacy&path=CONTACTS")
                            let application = UIApplication.sharedApplication()
                            if application.canOpenURL(settingUrl!) {
                                application.openURL(settingUrl!)
                            }else{
                                LoadingAnimation.showError(self, msg: nil)
                            }
                        }
                    })
                }
            })
        default:
            break
        }
    }
// 獲取通訊錄信息
func retrieveContactsWithStore(store: CNContactStore){
        do{
            let keysToFetch = [CNContactFormatter.descriptorForRequiredKeysForStyle(.FullName),
                               CNContactImageDataKey,
                               CNContactPhoneNumbersKey]
            let fetchRequest = CNContactFetchRequest(keysToFetch: keysToFetch)
            try store.enumerateContactsWithFetchRequest(fetchRequest, usingBlock: { (contact, stop) in
                if contact.phoneNumbers.count > 0{
                    let labelValue = contact.phoneNumbers[0] as CNLabeledValue
                    let phoneNumer = (labelValue.value as! CNPhoneNumber).valueForKey("digits")
                    if phoneNumer == nil{
                        self.phoneStr = "\(self.phoneStr),{\"phoneNum\":\"\"}"
                    }else{
                        self.phoneStr = "\(self.phoneStr),{\"phoneNum\":\"\(phoneNumer!)\"}"
                    }
                }
            })
        }catch{
            
        }
    }
//往通訊錄里添加數(shù)據(jù)
                    let saveRequest = CNSaveRequest()
                    let contactsData: JSON = data["data"]
                    for i in 0 ..< contactsData.count {
                        let contact = CNMutableContact()
                        contact.givenName = "name\(contactsData[i]["Id"].stringValue)"
                        let phoneFirst = CNLabeledValue(label: CNLabelPhoneNumberiPhone, value: CNPhoneNumber(stringValue: contactsData[i]["PhoneNum"].stringValue))
                        contact.phoneNumbers = [phoneFirst]
                        saveRequest.addContact(contact, toContainerWithIdentifier: nil)
                    }
                    let store = CNContactStore()
                    do{
                        try store.executeSaveRequest(saveRequest)
                    }catch{
                    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容