解決IOS7中讀取不到通訊錄的問題

來源:互聯網
上載者:User

IOS7中讀取不到通訊錄的問題 大部分原因是沒有判斷系統版本來構造ABAddressBookRef


下面是兩個本人程式中的兩個方法,請參考。所有版本中已測試沒有問題。

//擷取通訊錄-(void)GetUserAddressBook{    //擷取通訊錄許可權    ABAddressBookRef ab = NULL;    // ABAddressBookCreateWithOptions is iOS 6 and up.    if (&ABAddressBookCreateWithOptions) {        CFErrorRef error = nil;        ab = ABAddressBookCreateWithOptions(NULL, &error);                if (error) { NSLog(@"%@", error); }    }    if (ab == NULL) {        ab = ABAddressBookCreate();    }    if (ab) {        // ABAddressBookRequestAccessWithCompletion is iOS 6 and up. 適配IOS6以上版本        if (&ABAddressBookRequestAccessWithCompletion) {            ABAddressBookRequestAccessWithCompletion(ab,                                                     ^(bool granted, CFErrorRef error) {                                                         if (granted) {                                                             // constructInThread: will CFRelease ab.                                                             [NSThread detachNewThreadSelector:@selector(constructInThread:)                                                                                      toTarget:self                                                                                    withObject:CFBridgingRelease(ab)];                                                         } else {                                                             //                                                             CFRelease(ab);                                                             // Ignore the error                                                         }                                                     });        } else {            // constructInThread: will CFRelease ab.            [NSThread detachNewThreadSelector:@selector(constructInThread:)                                     toTarget:self                                   withObject:CFBridgingRelease(ab)];        }    }}//擷取到addressbook的許可權-(void)constructInThread:(ABAddressBookRef) ab{    NSLog(@"we got the access right");    CFArrayRef results = ABAddressBookCopyArrayOfAllPeople(ab);    NSMutableArray* contactArray = [[NSMutableArray alloc]init];    for(int i = 0; i < CFArrayGetCount(results); i++)    {        ABRecordRef person = CFArrayGetValueAtIndex(results, i);        //姓        NSString *firstName = (NSString*)CFBridgingRelease(ABRecordCopyValue(person, kABPersonFirstNameProperty));        //姓音標//        NSString *firstNamePhonetic = (NSString*)CFBridgingRelease(ABRecordCopyValue(person, kABPersonFirstNamePhoneticProperty));        //名        NSString *lastname = (NSString*)CFBridgingRelease(ABRecordCopyValue(person, kABPersonLastNameProperty));        //名音標//        NSString *lastnamePhonetic = (NSString*)CFBridgingRelease(ABRecordCopyValue(person, kABPersonLastNamePhoneticProperty));        //公司        NSString *Organization = (NSString*)CFBridgingRelease(ABRecordCopyValue(person, kABPersonOrganizationProperty));        //讀取jobtitle工作//        NSString *jobtitle = (NSString*)CFBridgingRelease(ABRecordCopyValue(person, kABPersonJobTitleProperty));        //讀取department部門        NSString *department = (NSString*)CFBridgingRelease(ABRecordCopyValue(person, kABPersonDepartmentProperty));        //讀取birthday生日        NSDate *birthday = (NSDate*)CFBridgingRelease(ABRecordCopyValue(person, kABPersonBirthdayProperty));        //讀取nickname呢稱        double birthdayString = [birthday timeIntervalSince1970];        NSString *nickname = (NSString*)CFBridgingRelease(ABRecordCopyValue(person, kABPersonNicknameProperty));        //讀取電話多值        NSString* phoneString = @"";        ABMultiValueRef phone = ABRecordCopyValue(person, kABPersonPhoneProperty);        for (int k = 0; k

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.