2013-09-28 75 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

相关问题