ios - Programatically add contact in Swift -



ios - Programatically add contact in Swift -

i want add together contact (just name , phone number) programatically in swift. i've found objective-c examples didn't them work, not in objective-c. don't want involve addressbookui, because want values own ui.

here's quick method add together contact in swift. verified on iphone 5 ios 7.1 i've found simulator doesn't match same results phone ab stuff.

you can add together button , point method:

@ibaction func createcontact(sender: anyobject) { var newcontact:abrecordref! = abpersoncreate().takeretainedvalue() var success:bool = false var newfirstname:nsstring = "aa" var newlastname = "a" //updated work in xcode 6.1 var error: unmanaged<cferrorref>? = nil //updated error &error code builds in xcode 6.1 success = abrecordsetvalue(newcontact, kabpersonfirstnameproperty, newfirstname, &error) println("setting first name successful? \(success)") success = abrecordsetvalue(newcontact, kabpersonlastnameproperty, newlastname, &error) println("setting lastly name successful? \(success)") success = abaddressbookaddrecord(adbk, newcontact, &error) println("adbk addrecord successful? \(success)") success = abaddressbooksave(adbk, &error) println("adbk save successful? \(success)") }//createcontact

btw-it assumes you've got addressbook var assigned, can on opening view overriding viewdidappear. security prompt well:

override func viewdidappear(animated: bool) { super.viewdidappear(animated) if !self.authdone { self.authdone = true allow stat = abaddressbookgetauthorizationstatus() switch stat { case .denied, .restricted: println("no access") case .authorized, .notdetermined: var err : unmanaged<cferror>? = nil var adbk : abaddressbook? = abaddressbookcreatewithoptions(nil, &err).takeretainedvalue() if adbk == nil { println(err) homecoming } abaddressbookrequestaccesswithcompletion(adbk) { (granted:bool, err:cferror!) in if granted { self.adbk = adbk } else { println(err) }//if }//abaddressbookreqeustaccesswithcompletion }//case }//if }//viewdidappear

ios iphone swift abaddressbook

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -