"创建新联系人"和"添加到现有联系人" for CNContactViewController()

时间:2016-08-28 14:16:23

标签: ios swift contacts cncontact

使用ABAddressBook,当我希望用户能够选择"创建新联系人"和"添加到现有联系人"对于他们之前没见过的联系人,我会创建并呈现ABUnknownPersonViewController

我无法在CNContacts框架中复制此功能。在我看来,CNContactViewController(forUnknownContact: contact)可以工作,但不幸的是,这只会让用户发送消息"或"分享联系人。"

如何在CNContacts中允许用户将联系人保存为地址簿(新联系人或现有联系人的一部分?)

func presentContact() {

    let status = CNContactStore.authorizationStatusForEntityType(.Contacts)

    switch status {
    case .Authorized: ()
    case .NotDetermined: requestAccess()
    case .Denied, .Restricted: accessDenied()
    }

    print("authorized? \(status == .Authorized)") //prints "authorized? true"

    let unknown = CNContactViewController(forUnknownContact: contact!)

    unknown.delegate = self

    self.navigationController?.pushViewController(unknown, animated: false)

}

即使我尝试请求访问权限,用户仍然无法保存联系人。

3 个答案:

答案 0 :(得分:14)

您一直没有显示您的真实代码,因此无法为您提供帮助。所以我失去了兴趣。我只会向您展示我的真实代码,让您学习它并思考我正在做什么和您正在做什么之间的区别。这是实际的工作代码;你们也这样做:

let con = CNMutableContact()
con.givenName = "Johnny"
con.familyName = "Appleseed"
con.phoneNumbers.append(CNLabeledValue(
    label: "woods", value: CNPhoneNumber(stringValue: "555-123-4567")))
let unkvc = CNContactViewController(forUnknownContact: con)
unkvc.message = "He knows his trees"
unkvc.contactStore = CNContactStore()
unkvc.delegate = self
unkvc.allowsActions = false
self.navigationController?.pushViewController(unkvc, animated: true)

enter image description here

答案 1 :(得分:2)

代码中缺少的是将contactStore变量的unknown属性设置为CNContactStore的句柄。

[...]

unknown.contactStore = CNContactStore()

[...]

答案 2 :(得分:2)

CNContactStore *store = [[CNContactStore alloc] init];
CNMutableContact *contact = [[CNMutableContact alloc] init];
CNPhoneNumber * number  = [[CNPhoneNumber alloc] initWithStringValue:@"1234567890"];
CNLabeledValue * labelValue = [[CNLabeledValue alloc]initWithLabel:CNLabelPhoneNumberMobile value:number];
NSMutableArray<CNLabeledValue *> *phoneNumbers = [NSMutableArray new];
[phoneNumbers addObject:labelValue];
contact.phoneNumbers = phoneNumbers;
CNContactViewController *controller = [CNContactViewController viewControllerForNewContact:contact];

controller.contactStore = store;
controller.delegate = self;

[self presentViewController:[[UINavigationController alloc] initWithRootViewController:controller] animated:YES completion:nil];

此代码适用于“创建新联系人”,对于“添加到现有联系人”,我们将必须使用CNContactPickerViewController

CNContactPickerViewController * picker = [[CNContactPickerViewController alloc] init];
                                    picker.delegate = self;
                                    [self presentViewController:picker animated:YES completion:nil];

和委托方法中的

- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:(CNContact *)contact
{
    CNContactStore *store = [[CNContactStore alloc] init];
    CNMutableContact *existingContact = [(CNMutableContact*)contact mutableCopy];
    CNPhoneNumber * number  = [[CNPhoneNumber alloc] initWithStringValue:@"1234567890"];
    CNLabeledValue * labelValue = [[CNLabeledValue alloc]initWithLabel:CNLabelPhoneNumberMobile value:number];
    NSMutableArray<CNLabeledValue *> *phoneNumbers = [NSMutableArray new];
    [phoneNumbers addObject:labelValue];
    [phoneNumbers addObjectsFromArray:existingContact.phoneNumbers];
    existingContact.phoneNumbers = phoneNumbers;
    CNContactViewController *controller = [CNContactViewController viewControllerForNewContact:existingContact];

    controller.contactStore = store;
    controller.delegate = self;

    dispatch_async(dispatch_get_main_queue(), ^
    {
        [self presentViewController:[[UINavigationController alloc] initWithRootViewController:controller] animated:YES completion:nil];
    });

}

尽管它将显示“完成”按钮,而不是“更新”,但是它肯定会执行该功能,这是iPhone在Contacts App中的默认行为。