2013-09-28 46 views
0

在我的iPad應用程序(Xcode 5,iOS 7,ARC,Storyboards)中,我試圖讀取地址簿並獲取所有條目。不幸的是,我收到以下錯誤:ABAddressBookCreateWithOptions導致崩潰(無法識別的選擇器)

SalonBook[2225:a0b] -[__NSCFString count]: unrecognized selector sent to instance 0xb335e70

這是我從SO複製的代碼(爲什麼重新發明輪子?)。

-(IBAction)importClientData:(UIButton *)sender { 

NSMutableArray *allContacts = [[NSMutableArray alloc] init]; 
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL); 
CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(addressBook); 
for(int i = 0;i<ABAddressBookGetPersonCount(addressBook);i++) 
{ 
    NSMutableDictionary *aPersonDict = [[NSMutableDictionary alloc] init]; 
    ABRecordRef ref = CFArrayGetValueAtIndex(people, i); 
    NSString *fullName = (__bridge NSString *) ABRecordCopyCompositeName(ref); 
    if (fullName) { 
     [aPersonDict setObject:fullName forKey:@"fullName"]; 

     // collect phone numbers 
     NSMutableArray *phoneNumbers = [[NSMutableArray alloc] init]; 
     ABMultiValueRef phones = ABRecordCopyValue(ref, kABPersonPhoneProperty); 
     for(CFIndex j = 0; j < ABMultiValueGetCount(phones); j++) { 
      NSString *phoneNumber = (__bridge NSString *) ABMultiValueCopyValueAtIndex(phones, j); 
      [phoneNumbers addObject:phoneNumber]; 
     } 
     [aPersonDict setObject:phoneNumbers forKey:@"phoneNumbers"]; 

     // collect emails - key "emails" will contain an array of email addresses 
     ABMultiValueRef emails = ABRecordCopyValue(ref, kABPersonEmailProperty); 
     NSMutableArray *emailAddresses = [[NSMutableArray alloc] init]; 
     for(CFIndex idx = 0; idx < ABMultiValueGetCount(emails); idx++) { 
      NSString *email = (__bridge NSString *)ABMultiValueCopyValueAtIndex(emails, idx); 
      [emailAddresses addObject:email]; 
     } 
     [aPersonDict setObject:emailAddresses forKey:@"emails"]; 
     // if you want to collect any other info that's stored in the address book, it follows the same pattern. 
     // you just need the right kABPerson.... property. 

     [allContacts addObject:aPersonDict]; 
    } 
    else { 
     // Note: I have a few entries in my phone that don't have a name set 
     // Example one could have just an email address in their address book. 
    } 
} 

}

更新:下面是完整的堆棧跟蹤:

0 CoreFoundation 0x2e335f53 + 130 
1 libobjc.A.dylib 0x3899e6af objc_exception_throw + 38 
2 CoreFoundation 0x2e3398e7 + 202 
3 CoreFoundation 0x2e3381d3 + 706 
4 CoreFoundation 0x2e287598 _CF_forwarding_prep_0 + 24 
5 AddressBook 0x2d9274fd ABCCopyUserLanguage + 60 
6 AddressBook 0x2d914e3d ABCIsSortDataValid + 28 
7 AddressBook 0x2d914d6d ABCCreateAddressBookWithDatabaseDirectoryAndForceInProcessMigrationInProcessLinkingAndResetSortKeys + 1120 
8 AddressBook 0x2d928d17 ABAddressBookCreateWithDatabaseDirectory + 74 
9 AddressBook 0x2d928e67 ABAddressBookCreateWithOptionsAndPolicy + 146 
10 AddressBook 0x2d9290c9 ABAddressBookCreateWithOptions + 88 
11 SalonBook 0x000e35db -[CustomerSetupController importClientData:] + 118 
12 UIKit 0x30adbf3f + 90 
13 UIKit 0x30adbedf + 30 
14 UIKit 0x30adbeb9 + 44 
15 UIKit 0x30ac7b3f + 374 
16 UIKit 0x30adb92f + 590 
17 UIKit 0x30adb601 + 528 
18 UIKit 0x30ad668d + 832 
19 UIKit 0x30aaba25 + 196 
20 UIKit 0x30aaa221 + 7096 
21 CoreFoundation 0x2e30118b + 14 
22 CoreFoundation 0x2e30065b + 206 
23 CoreFoundation 0x2e2fee4f + 622 
24 CoreFoundation 0x2e269ce7 CFRunLoopRunSpecific + 522 
25 CoreFoundation 0x2e269acb CFRunLoopRunInMode + 106 
26 GraphicsServices 0x32f37283 GSEventRunModal + 138 
27 UIKit 0x30b0ba41 UIApplicationMain + 1136 
28 SalonBook 0x000b6335 main + 116 
29 libdyld.dylib 0x38ea6ab7 + 2 

不管我用什麼樣的代碼(我已經嘗試了上面的代碼的幾個變化)他們都在相同的呼叫(-ABAddressBookCreateWithOptions)上崩潰。由於我從多個示例中複製了相同的代碼,因此我希望至少這一行能夠正常工作。不是!爲什麼?

+0

用完整的錯誤信息更新您的問題。 – rmaddy

+0

嗨馬迪...很高興看到你在這一個! :D(q已更新)SD – SpokaneDude

+0

該錯誤不是調用「ABAddressBookCreateWithOptions」。哪一行代碼導致了這個問題?發佈的代碼沒有顯示對「count」方法的調用。查看調試器中的堆棧跟蹤並確定哪一行代碼導致了問題。 – rmaddy

回答

2

您是否打電話給ABAddressBookRequestWithCompletion以獲得用戶訪問地址簿的權限尚不清楚。如果您不針對iOS 6或更新版本的應用執行此操作,則不會允許您訪問地址簿。這就是說,AddressBook API不應該以這種方式崩潰。您可能需要通過bugreport.apple.com向蘋果報告錯誤。

+0

我添加了代碼以獲得用戶的許可,但我從來沒有那麼遠......將它發送到星期一的bugreport ...感謝您的幫助... – SpokaneDude

相關問題