0
在我的应用程序我保存联系人按钮点击事件。点击按钮保存适当的联系人。但当我试图按另一个按钮,而不是保存该数据显示错误的联系。所以我该如何解决这个问题?如何使用联系人框架保存联系人
这里是我indexpath代码
let fooBar = CNMutableContact()
var store = CNContactStore()
在cellforrow
cell.btnClick.tag = indexPath.row
cell.btnClick.addTarget(self, action: #selector(ViewController.buttonInsertPressed(_:)), forControlEvents: UIControlEvents.TouchUpInside)
func buttonInsertPressed(sender:UIButton) {
getData()
let index = sender.tag
print(fooBar)
phone = dic.valueForKey("mobile").objectAtIndex(index) as! String
print(phone)
// fooBar.givenName = dic.valueForKey("name").objectAtIndex(indexPath.row) as! String
let name : String = dic.valueForKey("name").objectAtIndex(index) as! String
print(fooBar.givenName)
let homePhone = CNLabeledValue(label: CNLabelHome,
value: CNPhoneNumber(stringValue: phone))
fooBar.setValue(name, forKey: "givenName")
fooBar.setValue([homePhone], forKey: "phoneNumbers")
// fooBar.phoneNumbers = [homePhone]
print(fooBar)
fooBar.middleName = "A."
fooBar.familyName = "Bar"
// fooBar.nickname = "Fooboo"
if #available(iOS 9.0, *) {
switch CNContactStore.authorizationStatusForEntityType(.Contacts){
case .Authorized:
createContact()
case .NotDetermined:
store.requestAccessForEntityType(.Contacts){succeeded, err in
guard err == nil && succeeded else{
return
}
self.createContact()
}
default:
print("Not handled")
if let url = NSURL(string: "tel://\(phone)") {
UIApplication.sharedApplication().openURL(url)
}
}
} else {
if let url = NSURL(string: "tel://\(phone)") {
UIApplication.sharedApplication().openURL(url)
}
// Fallback on earlier versions
}
}
func createContact()
{
let request = CNSaveRequest()
request.addContact(fooBar, toContainerWithIdentifier: nil)
do{
try store.executeSaveRequest(request)
print("Successfully stored the contact")
if let url = NSURL(string: "tel://\(phone)") {
UIApplication.sharedApplication().openURL(url)
}
} catch let err{
print("Failed to save the contact. \(err)")
if let url = NSURL(string: "tel://\(phone)") {
UIApplication.sharedApplication().openURL(url)
}
}
}
请让我知道我怎么能解决这个问题?
它正常工作与雨燕2.2 –