iOS导入手机通讯录

 ABAddressBookRef addressBooks = nil;

    

    if ([[UIDevice currentDevice].systemVersion floatValue] >= 6.0)

        

    {

        addressBooks =  ABAddressBookCreateWithOptions(NULL, NULL);

        

        //获取通讯录权限

        

        dispatch_semaphore_t sema = dispatch_semaphore_create(0);

        

        ABAddressBookRequestAccessWithCompletion(addressBooks, ^(bool granted, CFErrorRef error){dispatch_semaphore_signal(sema);});

        

        dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);

    }

    

    else

        

    {

        addressBooks = ABAddressBookCreate();

        

    }

    

    //获取通讯录中的所有人

    CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBooks);

    //通讯录中人数

    CFIndex nPeople = ABAddressBookGetPersonCount(addressBooks);

    

    //循环,获取每个人的个人信息

    for (NSInteger i = 0; i < nPeople; i++)

    {

        //新建一个addressBook model

        TKAddressBook *addressBook = [[TKAddressBook alloc] init];

        //获取个人

        ABRecordRef person = CFArrayGetValueAtIndex(allPeople, i);

        //获取个人名字

        CFTypeRef abName = ABRecordCopyValue(person, kABPersonFirstNameProperty);

        CFTypeRef abLastName = ABRecordCopyValue(person, kABPersonLastNameProperty);

        CFStringRef abFullName = ABRecordCopyCompositeName(person);

        NSString *nameString = (__bridge NSString *)abName;

        NSString *lastNameString = (__bridge NSString *)abLastName;

        

        if ((__bridge id)abFullName != nil) {

            nameString = (__bridge NSString *)abFullName;

        } else {

            if ((__bridge id)abLastName != nil)

            {

                nameString = [NSString stringWithFormat:@"%@ %@", nameString, lastNameString];

            }

        }

        addressBook.name = nameString;

        addressBook.recordID = (int)ABRecordGetRecordID(person);;

        

        ABPropertyID multiProperties[] = {

            kABPersonPhoneProperty,

            kABPersonEmailProperty

        };

        NSInteger multiPropertiesTotal = sizeof(multiProperties) / sizeof(ABPropertyID);

        for (NSInteger j = 0; j < multiPropertiesTotal; j++) {

            ABPropertyID property = multiProperties[j];

            ABMultiValueRef valuesRef = ABRecordCopyValue(person, property);

            NSInteger valuesCount = 0;

            if (valuesRef != nil) valuesCount = ABMultiValueGetCount(valuesRef);

            

            if (valuesCount == 0) {

                CFRelease(valuesRef);

                continue;

            }

            //获取电话号码和email

            for (NSInteger k = 0; k < valuesCount; k++) {

                CFTypeRef value = ABMultiValueCopyValueAtIndex(valuesRef, k);

                switch (j) {

                    case 0: {// Phone number

                        addressBook.tel = (__bridge NSString*)value;

                        break;

                    }

                    case 1: {// Email

                        addressBook.email = (__bridge NSString*)value;

                        break;

                    }

                }

                CFRelease(value);

            }

            CFRelease(valuesRef);

        }

        

        NSMutableDictionary *jsonDict=[NSMutableDictionary dictionary];

        

        if (nameString .length == 0 || nameString == nil) {

            nameString=@"我的通讯录好友";

        }

        [jsonDict setObject:nameString forKey:@"nickname"];

        

        if (addressBook.tel.length > 0) {

            [jsonDict setObject:[NSString stringWithFormat:@"%@",addressBook.tel] forKey:@"mobile"];

        }

        

        

        

        

        //将个人信息添加到数组中,循环完成后addressBookTemp中包含所有联系人的信息

        [_addressBookTemp addObject:jsonDict];

        

        if (abName) CFRelease(abName);

        if (abLastName) CFRelease(abLastName);

        if (abFullName) CFRelease(abFullName);

    }

    

    

    

    

    NSMutableDictionary *dict = [NSMutableDictionary dictionary];

    if (GETTOKEN) {

        [dict setObject:GETTOKEN forKey:@"authToken"];

    }

    

    

    

    NSData *dataFriends = [NSJSONSerialization dataWithJSONObject:self.addressBookTemp options:NSJSONWritingPrettyPrinted error:nil];

    

    NSString *jsonString = [[NSString alloc] initWithData:dataFriends

                            

                                                 encoding:NSUTF8StringEncoding];

    

    

    jsonString=[jsonString stringByReplacingOccurrencesOfString:@"\r" withString:@""];

    

    jsonString = [jsonString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

    

    

    NSString *newStr=[ImportContactListViewController delSpaceAndNewline:jsonString];

    

    

    NSMutableString *muStr=[NSMutableString stringWithFormat:@"%@",newStr];

    

    

    

    NSString *character = nil;

    for (int i=0; i<muStr.length; i++) {

        

        character = [muStr substringWithRange:NSMakeRange(i, 1)];

        if ([character isEqualToString:@"\n"]){

            

            [muStr deleteCharactersInRange:NSMakeRange(i, 1)];

        }

    }

    

    NSString *character1 = nil;

    for (int i=0; i<muStr.length; i++) {

        

        character1 = [muStr substringWithRange:NSMakeRange(i, 1)];

        if ([character isEqualToString:@"\r"]){

            

            [muStr deleteCharactersInRange:NSMakeRange(i, 1)];

        }

    }

    

    

    

    

    

    [dict setObject:muStr forKey:@"item"];

    

    

    NewNetwork *request = [[ NewNetwork alloc]init];

    

    NSString *strUrl=[NSString stringWithFormat:@"api-user/import-phone-contacts"];

    [request POSTNewRequest:dict url:strUrl];

    

    [request block:^(id responseObject) {

        

        NSLog(@"----------------=%@",[responseObject objectForKey:@"msg"]);

        [self GivePromptView:[responseObject objectForKey:@"msg"]];

        NSString *result  = [ NSString stringWithFormat:@"%@" , [responseObject objectForKey:@"status"]];

        if ([result isEqualToString:@"1"]) {

            

            [self GivePromptView:@"导入手机通讯录成功"];

            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

                MyPersonCirleViewController *myPersonCirleVC = [[MyPersonCirleViewController alloc]init];

                myPersonCirleVC.vcType=@"list";

                self.vcType=@"importSuccess";

                myPersonCirleVC.backStr = @"下一步";

                

                NSUserDefaults *user=[NSUserDefaults standardUserDefaults];

                [user setObject:@"yimport" forKey:@"import"];

                [user synchronize];

                

                

                [self.navigationController pushViewController:myPersonCirleVC animated:YES];

            });

        }

    }];

    

}



+ (NSString *)delSpaceAndNewline:(NSString *)string;{

    

    

    

    NSMutableString *mutStr = [NSMutableString stringWithString:string];

    

    NSRange range = {0,mutStr.length};

    

    [mutStr replaceOccurrencesOfString:@" " withString:@"" options:NSLiteralSearch range:range];

    

    NSRange range2 = {0,mutStr.length};

    

    [mutStr replaceOccurrencesOfString:@"\n" withString:@"" options:NSLiteralSearch range:range2];

    

    return mutStr;

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值