ios - iOS8中的通讯录授权

标签 ios objective-c ios8 abaddressbook

自从我在 iOS 8 上更新后,我无法获得访问地址簿的权限。

在 iOS 7 中,系统询问用户是否允许我的应用程序访问他的地址簿,但自 iOS8 以来,没有任何请求。每次我在启动“MyGetAddressesOfContacts”函数之前检查 ABAddressBookGetAuthorizationStatus 时,状态等于 kABAuthorizationStatusNotDetermined。 就像你在这里看到的:

if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusDenied)
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No permission" message:@"This App has no permition to access your contacts. Please check it in your privacy settings of your device." delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
    [alert show];

    return nil;
}

// This AlertView pops up every time!
if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined)
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Why this App needs your contacts" message:@"In the following your device will ask you whether this App is allowed to access your contacts. [...]" delegate:self cancelButtonTitle:@"I understand" otherButtonTitles: nil];

    [alert show];
}

检查完成后,我将启动 MyGetAddressesOfContacts 函数,如下所示:

ABAddressBookRef addressBook = ABAddressBookCreate();
NSArray *contactArr = (NSArray *)CFBridgingRelease(ABAddressBookCopyArrayOfAllPeople(addressBook));
NSMutableDictionary *dPersons = [[NSMutableDictionary alloc] init];

for (int i = 0; i < [contactArr count]; i++)
{
    ABRecordRef person = (ABRecordRef)CFBridgingRetain([contactArr objectAtIndex:i]);
    NSString *firstName = (__bridge_transfer NSString*)ABRecordCopyValue(person, kABPersonFirstNameProperty);
    NSString *lastName = (__bridge_transfer NSString*)ABRecordCopyValue(person, kABPersonLastNameProperty);
    NSString *sPersonName = [NSString stringWithFormat:@"%@ %@", firstName, lastName];

    ABMultiValueRef address = ABRecordCopyValue(person, kABPersonAddressProperty);
    NSString *sAddress;

    for(CFIndex j = 0; j < ABMultiValueGetCount(address); j++)
    {
        CFDictionaryRef addressDict = ABMultiValueCopyValueAtIndex(address, j);

        CFStringRef streetValue = CFDictionaryGetValue(addressDict, kABPersonAddressStreetKey);
        CFStringRef cityValue = CFDictionaryGetValue(addressDict, kABPersonAddressCityKey);
        CFStringRef countryValue = CFDictionaryGetValue(addressDict, kABPersonAddressCountryKey);

        sAddress = [NSString stringWithFormat:@"%@ %@, %@", streetValue, cityValue, countryValue];
        [dPersons setObject:sAddress forKey: [NSString stringWithFormat:@"%@%d %ld", @"AddressFromNameID", i, j]];
    }

    [dPersons setObject:sPersonName forKey: [NSString stringWithFormat:@"%@ %d", @"NameWithID", i]];
}

return dPersons;

希望有人能帮忙。

最佳答案

创建 AB 后 ...

ABAddressBookRef addressBook = ABAddressBookCreate();

调用以下...

ABAddressBookRequestAccessWithCompletion(addressBook, nil);

这将提示用户允许您的应用程序访问“联系人”;作为第二个参数,您可能会得到结果回调并做出相应的行为。请引用Apple documentation

关于ios - iOS8中的通讯录授权,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25863904/

相关文章:

ios - 首次从应用商店安装应用时,Firebase 动态链接不起作用

objective-c - 如何使用 MPMoviePlayerController 播放视频

ios - Xcode 升级后如何使用 iOS7 运行我的 iOS 模拟器

ios - 具有透明图像背景的 UIButton 未绘制透明

ios - 在 -[NSURLResponse allHeaderFields] 中崩溃

objective-c - 在 UIView 中嵌入 ZBar 相机

ios - Core Data iOS 8 Today Widget 问题

ios - 使用 swift 在 iOS 8 应用程序中显示游戏中心挑战而不是排行榜

ios - 修复自定义相机应用程序 iOS 的方向

objective-c - 如何创建 NSIndexPath :indexPathWithIndexes:length: 所需的 "indexes"