gpt4 book ai didi

ios - 在我的应用程序中访问 iCloud 联系人时导致 iOS 崩溃

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:40:05 25 4
gpt4 key购买 nike

我想在我的应用程序中访问我的地址簿联系人,我能够成功地从我的地址簿中检索所有联系人,但是如果在我的 iPad 中同步我的 i-cloud 帐户,那么我的地址簿会从我的 i-cloud 更新联系人也,如果现在我访问我的联系人,它会导致崩溃。请帮助我,我完全被困住了不知道该怎么办。我可以轻松访问地址簿联系人,但是一旦它从 i-cloud 同步,然后我获取地址簿,它会导致崩溃并给我严重的错误。

这是我用来获取联系人的代码。

+(NSArray *)getAllContacts
{

CFErrorRef *error = nil;


ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);

__block BOOL accessGranted = NO;
if (ABAddressBookRequestAccessWithCompletion != NULL) { // we're on iOS 6
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
accessGranted = granted;
dispatch_semaphore_signal(sema);
});
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);

}
else { // we're on iOS 5 or older
accessGranted = YES;
}

if (accessGranted) {

#ifdef DEBUG
NSLog(@"Fetching contact info ----> ");
#endif


ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
ABRecordRef source = ABAddressBookCopyDefaultSource(addressBook);
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(addressBook, source, kABPersonSortByFirstName);
CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);
NSMutableArray* items = [NSMutableArray arrayWithCapacity:nPeople];


for (int i = 0; i < nPeople; i++)
{
MContact *contacts = [MContact new];

ABRecordRef person = CFArrayGetValueAtIndex(allPeople, i);

//get First Name and Last Name

contacts.firstName = (__bridge NSString*)ABRecordCopyValue(person, kABPersonFirstNameProperty);

contacts.lastName = (__bridge NSString*)ABRecordCopyValue(person, kABPersonLastNameProperty);

if (!contacts.firstName) {
contacts.firstName = @"";
}
if (!contacts.lastName) {
contacts.lastName = @"";
}


NSMutableArray *contactEmails = [NSMutableArray new];
ABMultiValueRef multiEmails = ABRecordCopyValue(person, kABPersonEmailProperty);

for (CFIndex i=0; i<ABMultiValueGetCount(multiEmails); i++) {
CFStringRef contactEmailRef = ABMultiValueCopyValueAtIndex(multiEmails, i);
NSString *contactEmail = (__bridge NSString *)contactEmailRef;

[contactEmails addObject:contactEmail];
// NSLog(@"All emails are:%@", contactEmails);

}

if([contactEmails count]==0){

}
else{
[contacts setemails:contactEmails];
[items addObject:contacts];

}




#ifdef DEBUG
#endif




}
return items;



} else {
#ifdef DEBUG
NSLog(@"Cannot fetch Contacts :( ");
#endif
return NO;


}


}

最佳答案

您是否在您的代码中准确找到了崩溃追加的位置?如果它在您的“for (int i = 0; i < nPeople; i++)”循环中,可能是您只有 1 个联系人有问题...

PS 1,您应该删除第 7 行:....ABAddressBookCreateWithOptions,因为您需要先检查授权....

PS 2,你真的只需要来自默认来源的联系人吗?如果用户也有来自 Lotus Notes 或其他 gmail 的联系人怎么办....

关于ios - 在我的应用程序中访问 iCloud 联系人时导致 iOS 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22089749/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com