I want to create a button that, when pressed, will open the Add Contacts Page of Phone Application. And once the Done/Cancel button is pressed, it should come back to my Application. I know this is possible, but i don't know how.
Asked
Active
Viewed 927 times
1 Answers
1
I think the Apple address book programming guide will help you:
ABNewPersonViewController *view = [[ABNewPersonViewController alloc] init];
view.newPersonViewDelegate = self;
UINavigationController *newNavigationController = [[UINavigationController alloc]
initWithRootViewController:view];
[self presentModalViewController:newNavigationController
animated:YES];
ABNewPersonViewController is deprecated with iOS9. One should use CNContactViewController instead. Unfortunately there is a bug, refer to this thread (CNContactViewController Bug).
CNContactStore().requestAccessForEntityType(.Contacts) {ok, err in
guard ok else {return} // only if access is granted
dispatch_async(dispatch_get_main_queue()) {
let con = CNMutableContact()
let unkvc = CNContactViewController(forUnknownContact: con)
unkvc.contactStore = CNContactStore()
unkvc.delegate = self
unkvc.allowsActions = false
self.presentViewController(unkvc, animated: true, completion: nil)
}
and the delegate methods:
func contactViewController(vc: CNContactViewController, didCompleteWithContact con: CNContact?)
func contactViewController(vc: CNContactViewController, shouldPerformDefaultActionForContactProperty prop: CNContactProperty) -> Bool
-
thanks a lot guys, it worked! Is it possible to come back to my application after i press done/cancel button? – Sachin Dinesh Mar 09 '16 at 09:21
-
You defined your ViewController, where you present the `ABNewPersonViewController`, as delegate. Therefore you have to implement the delegate methods of this `ABNewPersonViewController`, like this one `unknownPersonViewController:didResolveToPerson:` – Marc Mar 09 '16 at 09:47
-
I just saw that with iOS9 this `ABNewPersonViewController` is deprecated. you should use CNContactViewController instead. I will provide some information my edited answer. – Marc Mar 09 '16 at 10:03
-
Im sorry I can't figure out what you just said. I am new to coding so it'll be really helpful for me if you could explain it. – Sachin Dinesh Mar 09 '16 at 10:05
-
that would be really nice @marc14 – Sachin Dinesh Mar 09 '16 at 10:06
-
@SachinDinesh what information didn't you understand? – Marc Mar 09 '16 at 10:13
-
could you please share the exact code to do the process? – Sachin Dinesh Mar 09 '16 at 11:58
-
Please look at the link, which I posted in my answer and which is referring to another stackoverflow thread. There is a github link which contains an example Xcode project. – Marc Mar 09 '16 at 12:07
-
I think i found the answer: (void)newPersonViewController:(ABNewPersonViewController *)newPersonView didCompleteWithNewPerson:(ABRecordRef)person – Sachin Dinesh Mar 10 '16 at 08:55