访问通讯录Contacts

作者: 楓葉乄神無月 | 来源:发表于2016-11-25 20:50 被阅读1次
// 是否开通了访问权限
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())\"请求访问您的通讯录", cancelButtonTitle: "取消", otherButtonTitle: "去设置", 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{
            
        }
    }
//往通讯录里添加数据
                    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{
                    }

相关文章

网友评论

    本文标题:访问通讯录Contacts

    本文链接:https://www.haomeiwen.com/subject/xrewpttx.html