Created
April 13, 2015 05:19
-
-
Save jfreyre/605a5e500eaf8a1b6610 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.m | |
// | |
// Created by Jérome Freyre on 13.04.15. | |
// Copyright (c) 2015 @jfreyre. All rights reserved. | |
// | |
#import "ViewController.h" | |
#import <AddressBook/AddressBook.h> | |
@interface ViewController () | |
@end | |
@implementation ViewController | |
- (void)viewDidLoad { | |
[super viewDidLoad]; | |
// Do any additional setup after loading the view, typically from a nib. | |
} | |
- (void) ASimpleMethodOrAnIBAction { | |
ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL); | |
if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) { | |
ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) { | |
if (granted) { | |
[self addContact]; | |
} else { | |
NSLog(@"oups... %@", error); | |
} | |
}); | |
} | |
else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) { | |
[self addContact]; | |
} | |
else { | |
NSLog(@"No way!!!"); | |
} | |
} | |
- (void) addContact { | |
CFErrorRef error = NULL; | |
NSLog(@"%@", [self description]); | |
ABAddressBookRef iPhoneAddressBook = ABAddressBookCreateWithOptions(NULL, NULL); | |
ABRecordRef newPerson = ABPersonCreate(); | |
ABRecordSetValue(newPerson, kABPersonFirstNameProperty, people.firstname, &error); | |
ABRecordSetValue(newPerson, kABPersonLastNameProperty, people.lastname, &error); | |
ABMutableMultiValueRef multiPhone = ABMultiValueCreateMutable(kABMultiStringPropertyType); | |
ABMultiValueAddValueAndLabel(multiPhone, people.phone, kABPersonPhoneMainLabel, NULL); | |
ABMultiValueAddValueAndLabel(multiPhone, people.other, kABOtherLabel, NULL); | |
ABRecordSetValue(newPerson, kABPersonPhoneProperty, multiPhone,nil); | |
CFRelease(multiPhone); | |
// ... | |
// Set other properties | |
// ... | |
ABAddressBookAddRecord(iPhoneAddressBook, newPerson, &error); | |
ABAddressBookSave(iPhoneAddressBook, &error); | |
CFRelease(newPerson); | |
CFRelease(iPhoneAddressBook); | |
if (error != NULL) | |
{ | |
CFStringRef errorDesc = CFErrorCopyDescription(error); | |
NSLog(@"Contact not saved: %@", errorDesc); | |
CFRelease(errorDesc); | |
}} | |
} | |
- (void)didReceiveMemoryWarning { | |
[super didReceiveMemoryWarning]; | |
// Dispose of any resources that can be recreated. | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
extra brace at line 72?