ios - 如何获取联系人 ID
<p><p>我在从通讯簿中获取联系人 ID 时遇到问题。</p>
<p>我的 .m 文件:</p>
<pre><code>-(IBAction)test:(id)sender
{
ABPeoplePickerNavigationController* picker = [ init];
picker.peoplePickerDelegate = self;
;
}
-(void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person
{
;
ABRecordID recordID = ABRecordGetRecordID(person);
label.text = ;
}
-(void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker
{
;
}
</code></pre>
<p>我将 AddressBook 和 AddressBookUI 导入到 .h 文件中。
一切正常,但我得到了错误的 ID。
每次我只得到 ID -1 代表错误。</p>
<p>希望你能帮助我。</p>
<p>非常感谢,
基督徒</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>在 iOS 8 中,它不再在仅显示人员选择器时自动请求地址簿的权限。 (这个想法是,选择器本身并没有真正授予应用程序访问您的联系人的权限,因此不需要权限。当应用程序没有真正获取联系人详细信息时,它提供了低摩擦的 UI。)</p>
<p>但是,就您而言,您确实是在尝试以编程方式检索有关联系人的信息。为此,您确实需要许可。因此,在展示选择器之前,您可能需要请求对地址簿的许可:</p>
<pre><code>ABAuthorizationStatus status = ABAddressBookGetAuthorizationStatus();
if ((status == kABAuthorizationStatusDenied || status == kABAuthorizationStatusRestricted)) {
NSLog(@"User previously denied permission; show error message that they must go to settings and give this app permission");
return;
} else if (status == kABAuthorizationStatusNotDetermined) {
CFErrorRef error;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);
if (!addressBook) {
NSLog(@"unable to open address book: %@", CFBridgingRelease(error));
return;
}
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
if (granted) {
NSLog(@"OK");
} else {
NSLog(@"Not ok");
}
CFRelease(addressBook);
});
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - 如何获取联系人 ID,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/28750625/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/28750625/
</a>
</p>
页:
[1]