加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

多值网址地址簿iphone

发布时间:2020-12-14 19:03:05 所属栏目:百科 来源:网络整理
导读:我想在地址簿中创建一个新联系人. 当您想要存储更多URL(Web地址到某些社交网络)时,会出现此问题. 我的代码在iOS6的模拟器中完美运行.但在使用iOS6的真实iPhone中,存储除网址之外的所有值. 我一直在找几天找不到解决办法,如果有人能提供帮助,我将非常感激. 我
我想在地址簿中创建一个新联系人.
当您想要存储更多URL(Web地址到某些社交网络)时,会出现此问题.
我的代码在iOS6的模拟器中完美运行.但在使用iOS6的真实iPhone中,存储除网址之外的所有值.
我一直在找几天找不到解决办法,如果有人能提供帮助,我将非常感激.

我的代码:

-(void) addContactToAddressBook:(ABAddressBookRef) iPhoneAddressBook 
{

    CFErrorRef error = NULL;

    ABRecordRef newPerson = ABPersonCreate();

    //Name and phone number
    ABRecordSetValue(newPerson,kABPersonFirstNameProperty,(__bridge CFStringRef)_nameField.text,&error);
    ABMutableMultiValueRef multiPhone = ABMultiValueCreateMutable(kABMultiStringPropertyType);
    ABMultiValueAddValueAndLabel(multiPhone,(__bridge CFStringRef)_phoneField.text,kABPersonPhoneMainLabel,NULL);
    ABRecordSetValue(newPerson,kABPersonPhoneProperty,multiPhone,nil);

    //Email value
    ABMutableMultiValueRef emailMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);
    ABMultiValueAddValueAndLabel(emailMultiValue,(__bridge CFStringRef)_emailField.text,kABWorkLabel,kABPersonEmailProperty,emailMultiValue,nil);
    CFRelease(emailMultiValue);

    //URL values
    ABMutableMultiValueRef urlMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);
    ABMultiValueAddValueAndLabel(urlMultiValue,(__bridge CFStringRef)_FacebookField.text,(CFStringRef)@"Facebook",NULL);
    ABMultiValueAddValueAndLabel(urlMultiValue,(__bridge CFStringRef)_twitterField.text,(CFStringRef)@"Twitter",(__bridge CFStringRef)_linkedinField.text,(CFStringRef)@"Linkedin",(__bridge CFStringRef)_googleField.text,(CFStringRef)@"Google+",kABPersonURLProperty,urlMultiValue,nil);
    CFRelease(urlMultiValue);

    ABAddressBookAddRecord(iPhoneAddressBook,newPerson,&error);

    ABAddressBookSave(iPhoneAddressBook,&error);
    if (error != NULL)
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Contact not saved" delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:nil,nil];
        [alert show];
    }else{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Contact saved" message:@"Your contact was successfully saved" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil,nil];
        [alert show];
    }
}

谢谢!

解决方法

根据Apple网站上的文档(向下滚动到页面中间的隐私),必须先授予对地址簿的访问权限,然后才能以编程方式访问.这就是我最终做的事情.

-(void)requestPermission
{
        ABAddressBookRef iPhoneAddressBook = ABAddressBookCreateWithOptions(NULL,NULL);

        if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
            ABAddressBookRequestAccessWithCompletion(iPhoneAddressBook,^(bool granted,CFErrorRef error) {
                if (granted) {
                    // First time access has been granted,add the contact
                    [self addContactToAddressBook:iPhoneAddressBook];
                } else {
                    // User denied access
                    // Display an alert telling user the contact could not be added
                }
            });
        }
        else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {
            // The user has previously given access,add the contact
            [self addContactToAddressBook:iPhoneAddressBook];
        }
        else {
            // The user has previously denied access
            // Send an alert telling user to change privacy setting in settings app
        }

    }

-(void)addContactToAddressBook:(ABAddressBookRef)iPhoneAddressBook
{
    CFErrorRef error = NULL;
    ABRecordRef newPerson = ABPersonCreate();

    //Name and phone number
    ABRecordSetValue(newPerson,nil);
    CFRelease(multiPhone);

    //Email value
    ABMutableMultiValueRef emailMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);
    ABMultiValueAddValueAndLabel(emailMultiValue,nil);
    CFRelease(urlMultiValue);

    // ...
    // Set other properties
    // ...
    ABAddressBookAddRecord(iPhoneAddressBook,&error);
    CFRelease(newPerson);
    CFRelease(iPhoneAddressBook);
    if (error != NULL)
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Contact not saved" delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:nil,nil];
        [alert show];
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读