2016-08-21 4 views
0

名前と電話帳にアクセスしようとして数日間苦しみ、すべて失敗しました。テストアプリケーションで正常に動作する次のコードを使用しますが、プロジェクトワークに追加すると機能しません。変数 "granted"には常に値 "false"があり、エラー "Access Failure"が発生します。これにもかかわらず、プライバシー設定でアクセスを許可するスライダを表示しません... 私は長い間、むしろ奇妙な行動への答えを見つけることができませんでしたしている...アプリケーションが電話帳にアクセスできない

私は任意の助けいただければ幸いです! `

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に 'Privacy - Contacts Usage Description'キーを追加しましたか? iOS 6.0以降でサポートされています。 –

+0

私はiOSを試しました8と9はまだ動作しません –

答えて

0

あなたがコードに続くあなたのアドレス帳に既存の連絡先の情報にアクセスする場合は、あなたを助けるかもしれない:)

ステップ1

#import <AddressBook/AddressBook.h> 
を追加します。

ステップ2:ステップ3

-(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. } 

のviewDidLoad

からメソッドを次呼び出しは:
は、この方法は、あなたの特定の連絡先に必要なすべての情報を提供し、あなたのコード にも、このメソッドを追加します。

- (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

いいえ、あなたのコードからコンタクトを受け取っていません。私のアプリケーションはそれらにアクセスできません。プライバシー設定、アクセスを許可するスイッチはありません... –