2016-08-21 35 views
0

我忍受了好几天,试图访问名称和电话号码簿,全部失败。我使用了以下代码,该代码在测试应用程序中成功运行,但是当我将它添加到项目工作中时,它不起作用。 “授予”变量不断具有值 - “错误”,并且出现“访问失败”错误。尽管如此,在隐私设置不显示滑块允许访问... 我早已找不到的答案,一个相当奇怪的行为...应用程序无法访问电话簿

我会感谢任何帮助!`

CNContactStore *store = [[CNContactStore alloc] init]; 
[store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) { 
    if (granted == YES) { 

     NSMutableArray *contacts = [NSMutableArray array]; 

     NSArray *keys = @[CNContactFamilyNameKey, CNContactGivenNameKey, CNContactPhoneNumbersKey, CNContactImageDataKey]; 
     NSString *containerId = store.defaultContainerIdentifier; 
     NSPredicate *predicate = [CNContact predicateForContactsInContainerWithIdentifier:containerId]; 
     NSError *error; 
     NSArray *cnContacts = [store unifiedContactsMatchingPredicate:predicate keysToFetch:keys error:&error]; 
     if (error) { 
      NSLog(@"error fetching contacts %@", error); 
     } else { 
      for (CNContact *contact in cnContacts) { 

       TSContact *newContact = [[TSContact alloc] init]; 
       newContact.firstName = contact.givenName; 
       newContact.lastName = contact.familyName; 
       UIImage *image = [UIImage imageWithData:contact.imageData]; 
       newContact.image = image; 
       for (CNLabeledValue *label in contact.phoneNumbers) { 
        NSString *phone = [label.value stringValue]; 
        if ([phone length] > 0) { 
         [contacts addObject:phone]; 
        } 
       } 
      } 
     } 
    } else { 
     NSLog(@"Error = %@", error.localizedDescription); 
    } 
}]; 
+0

什么是您的应用程序的部署目标(目标iOS版本6.0的东西)? – theFool

+0

您是否在info.plist中添加了“隐私 - 联系人使用说明”键?它支持iOS 6.0及更高版本。 –

+0

我试过iOS ios 8和9仍然不起作用 –

回答

0

如果你想操作以访问现有的联系人的信息,在你的地址簿中的代码可以帮助你:)

步骤1:添加

#import <AddressBook/AddressBook.h> 

步骤2:呼叫从viewDidLoad中

-(void)phonenumber 
{ 
    self.navigationController.navigationBarHidden = true; 
    ABAuthorizationStatus status = ABAddressBookGetAuthorizationStatus(); 

    if (status == kABAuthorizationStatusDenied || status == kABAuthorizationStatusRestricted) { 
     // if you got here, user had previously denied/revoked permission for your 
     // app to access the contacts, and all you can do is handle this gracefully, 
     // perhaps telling the user that they have to go to settings to grant access 
     // to contacts 

     [[[UIAlertView alloc] initWithTitle:nil message:@"This app requires access to your contacts to function properly. Please visit to the \"Privacy\" section in the iPhone Settings app." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show]; 
     return; 
    } 

    CFErrorRef error = NULL; 
    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error); 

    if (!addressBook) { 
     NSLog(@"ABAddressBookCreateWithOptions error: %@", CFBridgingRelease(error)); 
     return; 
    } 

    ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) { 
     if (error) { 
      NSLog(@"ABAddressBookRequestAccessWithCompletion error: %@", CFBridgingRelease(error)); 
     } 

     if (granted) { 
      // if they gave you permission, then just carry on 
      [self listPeopleInAddressBook:addressBook]; 
     } else { 
      // however, if they didn't give you permission, handle it gracefully, for example... 
      dispatch_async(dispatch_get_main_queue(), ^{ 
       // BTW, this is not on the main thread, so dispatch UI updates back to the main queue 
       [[[UIAlertView alloc] initWithTitle:nil message:@"This app requires access to your contacts to function properly. Please visit to the \"Privacy\" section in the iPhone Settings app." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show]; 
      }); 
     } 
     CFRelease(addressBook); 
    }); 
    // Do any additional setup after loading the view. 
} 

步骤3以下方法:也加入这个方法将你的代码 这种方法可以让你的特定联系人的所有必需的信息。

- (void)listPeopleInAddressBook:(ABAddressBookRef)addressBook 
{ 
      //Run UI Updates 
      NSArray *allPeople = CFBridgingRelease(ABAddressBookCopyArrayOfAllPeople(addressBook)); 
      NSInteger numberOfPeople = [allPeople count]; 

      for (NSInteger i = 0; i < numberOfPeople; i++) { 
       ABRecordRef person = (__bridge ABRecordRef)allPeople[i]; 
       //From Below code you can get what you want. 
       NSString *firstName = CFBridgingRelease(ABRecordCopyValue(person, kABPersonFirstNameProperty)); 
       NSString *lastName = CFBridgingRelease(ABRecordCopyValue(person, kABPersonLastNameProperty)); 
       NSLog(@"Name:%@ %@", firstName, lastName); 

       ABMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty); 
       NSString *phoneNumber = CFBridgingRelease(ABMultiValueCopyValueAtIndex(phoneNumbers, 0)); 
       NSLog(@"phone:%@", phoneNumber); 
       NSLog(@"============================================="); 
      } 

} 
+0

我在您的应用程序中添加了您的代码,不幸的是,每次应用程序启动时都会显示访问警报。当访问联系人的设置过渡,并没有显示滑块:( –

+0

你有你想要我的代码? 我没有得到什么问题,你面临的?@СашаЦвигун –

+0

不,我有没有收到您的代码的联系人我的应用程序没有访问他们的隐私设置,没有开关允许访问... –