2016-08-21 73 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

不,我有沒有收到您的代碼的聯繫人我的應用程序沒有訪問他們的隱私設置,沒有開關允許訪問... –