Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
iOS保存联系人适配 iOS 8
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(20, 200, self.view.frame.size.width, 30);
button.backgroundColor = [UIColor orangeColor];
[self.view addSubview:button];
[button setTitle:@"现有联系人" forState:UIControlStateNormal];
[button addTarget:self action:@selector(haha) forControlEvents:UIControlEventTouchUpInside];
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
button2.frame = CGRectMake(20, 260, self.view.frame.size.width, 30);
button2.backgroundColor = [UIColor orangeColor];
[button2 setTitle:@"新建联系人" forState:UIControlStateNormal];
[self.view addSubview:button2];
[button2 addTarget:self action:@selector(haha2) forControlEvents:UIControlEventTouchUpInside];
}
- (void)haha2 {
// 新建联系人
if (([[[UIDevice currentDevice] systemVersion] floatValue] < 9)) {
ABNewPersonViewController *newP = [[ABNewPersonViewController alloc] init];
newP.newPersonViewDelegate = self;
ABRecordRef person = ABPersonCreate();
ABMultiValueRef phones = ABMultiValueCreateMutable(kABMultiStringPropertyType);
CFStringRef mobile = (__bridge_retained CFStringRef)@"17600101706";
ABMultiValueAddValueAndLabel(phones, mobile, kABPersonPhoneMobileLabel, NULL);
ABRecordSetValue(person, kABPersonPhoneProperty, phones, NULL);
ABAddressBookRef ref = ABAddressBookCreate();
ABAddressBookAddRecord(ref, person, nil);
newP.displayedPerson = person;
CFRelease(mobile);
CFRelease(person);
CFRelease(ref);
ABPeoplePickerNavigationController *pNC = [[ABPeoplePickerNavigationController alloc] initWithRootViewController:newP];
[self presentViewController:pNC animated:YES completion:nil];
}
}
- (void)haha {
// 现有联系人
if (([[[UIDevice currentDevice] systemVersion] floatValue] < 9)) {
ABPeoplePickerNavigationController *peoplePickerController=[[ABPeoplePickerNavigationController alloc]init];
//设置代理
peoplePickerController.peoplePickerDelegate=self;
[self presentViewController:peoplePickerController animated:YES completion:nil];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)newPersonViewController:(ABNewPersonViewController *)newPersonView didCompleteWithNewPerson:(nullable ABRecordRef)person {
if (person) {
NSLog(@"保存完成");
} else {
NSLog(@"保存取消");
}
[newPersonView.navigationController dismissViewControllerAnimated:YES completion:nil];
}
- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person {
ABNewPersonViewController *p = [[ABNewPersonViewController alloc] init];
p.newPersonViewDelegate = self;
CFTypeRef typeRef = ABRecordCopyValue(person, kABPersonPhoneProperty);
ABMutableMultiValueRef multiValue;
if (ABMultiValueGetCount(typeRef) == 0)
multiValue = ABMultiValueCreateMutable(kABStringPropertyType);
else
multiValue = ABMultiValueCreateMutableCopy (typeRef);
CFStringRef mobile = (__bridge_retained CFStringRef)@"17600101701";
ABMultiValueAddValueAndLabel(multiValue, mobile, kABPersonPhoneMobileLabel, NULL);
ABRecordSetValue(person, kABPersonPhoneProperty, multiValue, NULL);
p.displayedPerson = person;
[peoplePicker dismissViewControllerAnimated:YES completion:^{
ABPeoplePickerNavigationController *pp = [[ABPeoplePickerNavigationController alloc] initWithRootViewController:p];
pp.peoplePickerDelegate = self;
[self presentViewController:pp animated:YES completion:nil];
}];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment