【问题标题】:ABNewPersonViewController using contact frameworkABNewPersonViewController 使用联系人框架
【发布时间】:2017-10-19 21:52:20
【问题描述】:
- (void)addContact {
    // Creating new entry
    ABAddressBookRef addressBook = ABAddressBookCreate();
    ABRecordRef person = ABPersonCreate();

    NSString *phone = self.data.phone;
    NSString *cellphone = self.data.cellPhone;
    NSString *fax = self.data.fax;
    NSString *email = self.data.email;
    NSString *firstName = self.data.firstName;
    NSString *lastName = self.data.lastName;

    ABRecordSetValue(person, kABPersonFirstNameProperty, (__bridge CFTypeRef)(firstName) , nil);
    ABRecordSetValue(person, kABPersonLastNameProperty, (__bridge CFTypeRef)(lastName), nil);
    ABRecordSetValue(person, kABPersonJobTitleProperty, (__bridge CFTypeRef)(title), nil);
    ABRecordSetValue(person, kABPersonDepartmentProperty, (__bridge CFTypeRef)(dept), nil);
    ABRecordSetValue(person, kABPersonOrganizationProperty, (__bridge CFTypeRef)(org), nil);
    ABRecordSetValue(person, kABPersonNoteProperty, (__bridge CFTypeRef)(unit), nil);

    // Adding phone numbers
    ABMutableMultiValueRef phoneNumberMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);
    ABMultiValueAddValueAndLabel(phoneNumberMultiValue, (__bridge CFTypeRef)(cellphone), (CFStringRef)@"iPhone", NULL);
    ABMultiValueAddValueAndLabel(phoneNumberMultiValue, (__bridge CFTypeRef)(phone), (CFStringRef)@"Work", NULL);
    ABMultiValueAddValueAndLabel(phoneNumberMultiValue, (__bridge CFTypeRef)(fax), (CFStringRef)@"Fax", NULL);
    ABRecordSetValue(person, kABPersonPhoneProperty, phoneNumberMultiValue, nil);
    CFRelease(phoneNumberMultiValue);

    // Adding emails
    ABMutableMultiValueRef emailMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);
    ABMultiValueAddValueAndLabel(emailMultiValue, (__bridge CFTypeRef)(email), (CFStringRef)@"Work", NULL);
    ABRecordSetValue(person, kABPersonEmailProperty, emailMultiValue, nil);
    CFRelease(emailMultiValue);

    // Adding person to the address book
    ABAddressBookAddRecord(addressBook, person, nil);
    CFRelease(addressBook);

    ABNewPersonViewController *c = [[ABNewPersonViewController alloc] init];
    [c setNewPersonViewDelegate:self];
    [c setDisplayedPerson:person];
    CFRelease(person);
    [self.navigationController pushViewController:c animated:YES];
}

使用联系人框架的类似代码是什么?如何将人员 (ABRecordRef person = ABPersonCreate();) 的详细信息加载到 *contact (CNMutableContact *contact = [CNMutableContact new];) 中?

【问题讨论】:

    标签: ios objective-c xcode9


    【解决方案1】:

    我建议使用ContactsUI 框架而不是旧的AddressBookUI 框架。所以,不要使用ABNewPersonViewController,而是使用CNContactViewController

    @import ContactsUI;
    
    @interface ViewController () <CNContactViewControllerDelegate>
    @end
    
    @implementation ViewController
    
    - (IBAction)didTapButton:(id)sender {
        CNMutableContact *contact = [[CNMutableContact alloc] init];
        [contact setGivenName:@"John"];
        [contact setFamilyName:@"Doe"];
        ...
    
        CNContactViewController *controller = [CNContactViewController viewControllerForNewContact:contact];
        controller.delegate = self;
    
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:controller];
        nav.view.backgroundColor = [UIColor blueColor];
        [self presentViewController:nav animated:true completion: nil];
    }
    
    - (void)contactViewController:(CNContactViewController *)viewController didCompleteWithContact:(CNContact *)contact {
        [viewController.navigationController dismissViewControllerAnimated:true completion:nil];
    }
    
    @end
    

    现在,使用 Contacts 和 ContactsUI 框架,而不是已弃用的 AddressBook 和 AddresssBookUI 框架。

    【讨论】:

    • 是 [self presentViewController:nav animated:true completion: nil];将有完成和取消选项?有什么方法可以将人员对象中的所有数据复制到联系人对象中,如何将数据加载到上述代码的联系人对象中?
    • controller.delegate = self;返回错误从不兼容类型“QuickContactsViewController *const __strong”分配给“id _Nullable”
    • 现在它显示数据但它不显示取消和完成按钮。
    • 我已经添加到@interface QuickContactsViewController : UIViewController 但同样的错误
    • 我想保留两者,这样我就可以支持 IOS 7 和 IOS 10。看起来像取消和完成按钮显示,但它是透明的。如果我点击右上角和左上角页面成功关闭。
    猜你喜欢
    • 1970-01-01
    • 2016-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-08
    • 1970-01-01
    • 1970-01-01
    • 2016-06-06
    相关资源
    最近更新 更多