ios 讀取通訊錄

來源:互聯網
上載者:User

標籤:

1.擷取通訊錄列表

+(NSMutableDictionary*)getAddressPeopleArray{    BOOL granted = [AddressEngine getAccessGranted];//    NSMutableArray*addArray=[NSMutableArray arrayWithCapacity:0];    NSMutableDictionary * addrDic = [NSMutableDictionary dictionaryWithCapacity:0];    if (granted)    {        ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);        ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {        });        NSArray *array = (__bridge NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);        // NSLog(@"kaishi");        for (int i = 0; i<array.count;i++)        {            ABRecordRef person = (__bridge ABRecordRef)([array objectAtIndex:i]);            ABMutableMultiValueRef ref = ABRecordCopyValue(person, kABPersonPhoneProperty);//讀取電話 格式111-2222-2222            addressPeople * people=[[addressPeople alloc]init];            NSInteger nCount = ABMultiValueGetCount(ref);//單個人電話數量            if ( nCount>5 || nCount<1 )            {                continue;            }                        addressPeoKey*aKey=[[addressPeoKey alloc]init];            aKey.count=nCount;            aKey.phoneSet=[NSMutableSet setWithCapacity:0];                        people.phnum=[NSMutableArray arrayWithCapacity:0];                        for(int i = 0 ;i < nCount;i++)            {                NSString *phoneNO=[AddressEngine trimIlegalChar:[self trimIlegalChar:(__bridge NSString *)(ABMultiValueCopyValueAtIndex(ref, i))]];                if (phoneNO!=nil&&![phoneNO isEqualToString:@""])                {                    if ([phoneNO hasPrefix:@"+86"])                    {                        phoneNO=[phoneNO substringFromIndex:3];                    }                    [people.phnum addObject:phoneNO];                                        [aKey.phoneSet addObject:phoneNO];                }            }            people.name=(__bridge NSString *)ABRecordCopyCompositeName(person);//名字            if (people.name==nil)            {                people.name=@"姓名不詳";            }            if (people.name.length>0)            {                NSString *firstChar = [people.name substringToIndex:1];                const char * cString = [firstChar UTF8String];                if (cString!=NULL)                {                    if (strlen(cString) == 3)                    {                        // 名字是漢字開頭                        NSMutableString *ms = [[NSMutableString alloc] initWithString:people.name];                        CFStringTransform((__bridge CFMutableStringRef)ms, 0, kCFStringTransformToLatin, NO);                        //                    CFStringTransform((__bridge CFMutableStringRef)ms, 0, kCFStringTransformStripDiacritics, NO);                        people.pinyinName=[NSString stringWithString:ms];                        people.pinyinName=[AddressEngine trimIlegalChar:people.pinyinName];                        //                    NSLog(@"pinyin:%@",people.pinyinName);                    }else                    {                        people.pinyinName=[people.name lowercaseString];                    }                }else                {                    people.name=[people.phnum objectAtIndex:0];                    people.pinyinName=@"#FX";                }            }else            {                people.name=[people.phnum objectAtIndex:0];                people.pinyinName=@"";            }            NSInteger rec=ABRecordGetRecordID(person);            people.recordID=[NSString stringWithFormat:@"%ld",rec];                                    people.remark=(__bridge NSString *)(ABRecordCopyValue(person, kABPersonNoteProperty));//備忘            if (!people.remark)            {                people.remark=@"";            }            if (people.remark.length>0)            {                NSString*re=people.remark;                people.remark=[AddressEngine cleanQuotesWithStr:re];            }            people.isChoose=NO;            people.isMark=NO;                        [addrDic setObject:people forKey:aKey];//            [addArray addObject:people];        }        //        NSLog(@"jieshu");    }    else    {        UIAlertView*alert=[[UIAlertView alloc]initWithTitle:@"許可權受限"                                                 message:@"設定-手機梵訊-啟用通訊錄許可權"                                                delegate:nil                                       cancelButtonTitle:@"確定"                                       otherButtonTitles:nil];        [alert show];    }//    return addArray;    return addrDic;}

2.判斷是否有許可權

+(BOOL)getAccessGranted{    ABAddressBookRef addressBook = NULL;    __block BOOL accessGranted = NO;        if (&ABAddressBookRequestAccessWithCompletion != NULL)    {        addressBook = ABAddressBookCreateWithOptions(NULL, NULL);        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    {        accessGranted = YES;    }    return accessGranted;}

 

ios 讀取通訊錄

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.