2016-06-09 6 views
0

in meiner App Ich spare Kontakte bei Ereignis der Schaltfläche klicken. So zum Beispiel den Klick auf den Button, um den entsprechenden Kontakt zu speichern. aber wenn ich versuchte, einen anderen Knopf zu drücken, anstatt diese Daten zu speichern, zeigt er Fehler des dulicate Kontakts an. also wie kann ich das lösen?So speichern Sie Kontakte mit Hilfe von Kontakte-Framework

hier ist mein Code

let fooBar = CNMutableContact() 
var store = CNContactStore() 

in cellforrow bei indexPath

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) 
     } 
    } 


} 

bitte lassen Sie mich wissen, wie kann ich dieses Problem lösen?

Antwort

0

Bitte folgen Sie diesen Methoden

100% Arbeitslösung

public func askPermissionForContacts(callBack:(Bool)->()) 
{ 
    let status : CNAuthorizationStatus = CNContactStore.authorizationStatusForEntityType(CNEntityType.Contacts) 
    if status == CNAuthorizationStatus.NotDetermined{ 
     store.requestAccessForEntityType(CNEntityType.Contacts, completionHandler: { (temp: Bool, error : NSError?) -> Void in 
      if temp == true{ 
       callBack(true) 
      }else 
      { 
       callBack(false) 
      } 
     }) 

    }else if status == CNAuthorizationStatus.Authorized { 
     callBack(true) 
    } 
    else if status == CNAuthorizationStatus.Denied { 
     callBack(false) 
    } 
} 



public func createAndSaveContact(userData : AnyObject) 
{ 
    let userDict = userData as! NSDictionary 
    let contactNew = CNMutableContact() 
    let homePhone = CNLabeledValue(label: CNLabelHome, value: CNPhoneNumber(stringValue: userDict.objectForKey("contact_phone") as! String)) 
    let homeEmail = CNLabeledValue(label:CNLabelHome, value: userDict.objectForKey("contact_email") as! String) 
    contactNew.givenName = userDict.objectForKey("contact_name") as! String 
    contactNew.phoneNumbers = [homePhone] 
    contactNew.emailAddresses = [homeEmail] 
    let request = CNSaveRequest() 
    request.addContact(contactNew, toContainerWithIdentifier: nil) 
    do{ 
     try store.executeSaveRequest(request) 
     print("Done") 

    } catch let err{ 
     print("Failed to save the contact. \(err)") 
    } 
} 
+0

Es funktioniert gut mit Swift 2.2 –

Verwandte Themen