菜鸟教程小白 发表于 2022-12-12 13:14:10

ios - ABPersonSocialProfile 崩溃应用


                                            <p><p>我在向 ABRecordRef 添加新的社交资料时遇到问题。它总是在 ABAdressBookSave 上返回崩溃
": 无法识别的选择器发送到实例"</p>

<pre><code>ABMultiValueRef social = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);
if(contact.socialTwitter != nil)
    ABMultiValueAddValueAndLabel(social, (__bridge CFTypeRef)([NSDictionary dictionaryWithObjectsAndKeys:
                                                               (NSString*)kABPersonSocialProfileServiceTwitter, kABPersonSocialProfileServiceKey,
                                                               (__bridge CFStringRef)contact.socialTwitter, kABPersonSocialProfileUsernameKey,
                                                               nil]), kABPersonSocialProfileServiceTwitter, NULL);


ABRecordSetValue(record, kABPersonSocialProfileProperty, social, &amp;error);
CFRelease(social);
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我在保存新联系人时遇到了同样的问题。看来您无法像这样保存所有属性。下面的代码对我有用。 </p>

<pre><code>ABRecordRef aRecord = ABPersonCreate();
            CFErrorRefanError = NULL;

            // Username
            ABRecordSetValue(aRecord, kABPersonFirstNameProperty, username, &amp;anError);

            // Phone Number.
            ABMutableMultiValueRef multi = ABMultiValueCreateMutable(kABMultiStringPropertyType);
            ABMultiValueAddValueAndLabel(multi, (CFStringRef)usercontact, kABWorkLabel, NULL);
            ABRecordSetValue(aRecord, kABPersonPhoneProperty, multi, &amp;anError);
            CFRelease(multi);

            // Company
            ABRecordSetValue(aRecord, kABPersonDepartmentProperty, usercompany, &amp;anError);

            // email
            NSLog(useremail);
            ABMutableMultiValueRef multiemail = ABMultiValueCreateMutable(kABMultiStringPropertyType);
            ABMultiValueAddValueAndLabel(multiemail, (CFStringRef)useremail, kABWorkLabel, NULL);
            ABRecordSetValue(aRecord, kABPersonEmailProperty, multiemail, &amp;anError);
            CFRelease(multiemail);

            // website
            NSLog(userwebsite);
            ABMutableMultiValueRef multiweb = ABMultiValueCreateMutable(kABMultiStringPropertyType);
            ABMultiValueAddValueAndLabel(multiweb, (CFStringRef)userwebsite, kABWorkLabel, NULL);
            ABRecordSetValue(aRecord, kABPersonURLProperty, multiweb, &amp;anError);
            CFRelease(multiemail);

            // Function
            ABRecordSetValue(aRecord, kABPersonJobTitleProperty, userrole, &amp;anError);

            if (anError != NULL)
                NSLog(@\&#34;error while creating..\&#34;);

            CFStringRef personname, personcompind, personemail, personfunction,personwebsite, personcontact;

            personname = ABRecordCopyValue(aRecord, kABPersonFirstNameProperty);
            personcompind = ABRecordCopyValue(aRecord, kABPersonDepartmentProperty);
            personfunction = ABRecordCopyValue(aRecord, kABPersonJobTitleProperty);
            personemail = ABRecordCopyValue(aRecord, kABPersonEmailProperty);
            personwebsite = ABRecordCopyValue(aRecord, kABPersonURLProperty);
            personcontact= ABRecordCopyValue(aRecord, kABPersonPhoneProperty);

            ABAddressBookRef addressBook;
            CFErrorRef error = NULL;
            addressBook = ABAddressBookCreate();

            BOOL isAdded = ABAddressBookAddRecord (addressBook, aRecord, &amp;error);

            if(isAdded){

                NSLog(@\&#34;added..\&#34;);
            }
            if (error != NULL) {
                NSLog(@\&#34;ABAddressBookAddRecord %@\&#34;, error);
            }
            error = NULL;

            BOOL isSaved = ABAddressBookSave (addressBook, &amp;error);

            if(isSaved) {

                NSLog(@\&#34;saved..\&#34;);
                UIAlertView *alertOnChoose = [ initWithTitle:@\&#34;Phone added successfully to your addressbook\&#34; message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:@\&#34;Ok\&#34;, nil];
                ;
                ;
            }

            if (error != NULL) {

                NSLog(@\&#34;ABAddressBookSave %@\&#34;, error);
                UIAlertView *alertOnChoose = [ initWithTitle:@\&#34;Unable to save this time\&#34; message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:@\&#34;Ok\&#34;, nil];
                ;
                ;
            }

            CFRelease(aRecord);
            CFRelease(personname);
            CFRelease(personcompind);
            CFRelease(personcontact);
            CFRelease(personemail);
            CFRelease(personfunction);
            CFRelease(personwebsite);
            CFRelease(addressBook);
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - ABPersonSocialProfile 崩溃应用,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/17976266/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/17976266/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - ABPersonSocialProfile 崩溃应用