在联系人应用中添加新电话号码

时间:2012-11-15 12:15:36

标签: iphone ios xcode

我有这段代码打开联系人视图控制器:

ABNewPersonViewController *newPersonVC = [[ABNewPersonViewController alloc] init];
               newPersonVC.newPersonViewDelegate = self;
               UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:newPersonVC];
               [self presentModalViewController:nc animated:YES];

我希望能够抓住NSString *phoneNumber = @"(416)-555-5555";并将其放入“家庭电话”字段。我查看了through other answers,我最终没有弹出任何内容或我的应用崩溃了。 Apple的文档也没有多大帮助。

我该如何解决这个问题?任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:2)

NSString *phoneNumber = @"(416)-555-5555";
ABRecordRef newPerson = ABPersonCreate();
CFErrorRef error = NULL;
ABRecordSetValue(newPerson, kABPersonDepartmentProperty,(__bridge  CFTypeRef)@"Department", nil);
ABMutableMultiValueRef phoneNumberMultiValue = ABMultiValueCreateMutable(kABPersonPhoneProperty);
ABMultiValueAddValueAndLabel(phoneNumberMultiValue,(__bridge  CFTypeRef)phoneNumber, kABWorkLabel, nil);
ABRecordSetValue(newPerson, kABPersonPhoneProperty, phoneNumberMultiValue, nil);
NSAssert( !error, @"Something bad happened here." );

ABNewPersonViewController *newPersonVC = [[ABNewPersonViewController alloc] init];
[newPersonVC setDisplayedPerson:newPerson];
newPersonVC.newPersonViewDelegate = self;
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:newPersonVC];
[self presentModalViewController:nc animated:YES];

希望它对你有帮助。