首页 > 系统 > iOS > 正文

iOS导入手机通讯录

2019-11-09 17:54:38
字体:
来源:转载
供稿:网友

 ABAddressBookRef addressBooks =nil;

    

    if ([[UIDevicecurrentDevice].systemVersionfloatValue] >= 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 = [[TKAddressBookalloc] init];

        //获取个人

        ABRecordRef person =CFArrayGetValueAtIndex(allPeople, i);

        //获取个人名字

        CFTypeRef abName =ABRecordCopyValue(person,kABPersonFirstNamePRoperty);

        CFTypeRef abLastName =ABRecordCopyValue(person,kABPersonLastNameProperty);

        CFStringRef abFullName =ABRecordCopyCompositeName(person);

        NSString *nameString = (__bridgeNSString *)abName;

        NSString *lastNameString = (__bridgeNSString *)abLastName;

        

        if ((__bridgeid)abFullName != nil) {

            nameString = (__bridgeNSString *)abFullName;

        } else {

            if ((__bridgeid)abLastName != nil)

            {

                nameString = [NSStringstringWithFormat:@"%@ %@", 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) {

                    case0: {// Phone number

                        addressBook.tel = (__bridgeNSString*)value;

                        break;

                    }

                    case1: {// Email

                        addressBook.email = (__bridgeNSString*)value;

                        break;

                    }

                }

                CFRelease(value);

            }

            CFRelease(valuesRef);

        }

        

        NSMutableDictionary *jsonDict=[NSMutableDictionarydictionary];

        

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

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

        }

        [jsonDict setObject:nameStringforKey:@"nickname"];

        

        if (addressBook.tel.length >0) {

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

        }

        

        

        

        

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

        [_addressBookTempaddObject:jsonDict];

        

        if (abName)CFRelease(abName);

        if (abLastName)CFRelease(abLastName);

        if (abFullName)CFRelease(abFullName);

    }

    

    

    

    

    NSMutableDictionary *dict = [NSMutableDictionarydictionary];

    if (GETTOKEN) {

        [dict setObject:GETTOKENforKey:@"authToken"];

    }

    

    

    

    NSData *dataFriends = [NSJSONSerializationdataWithJSONObject:self.addressBookTempoptions:NSJSONWritingPrettyPrintederror:nil];

    

    NSString *jsonString = [[NSStringalloc] initWithData:dataFriends

                            

                                                 encoding:NSUTF8StringEncoding];

    

    

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

    

    jsonString = [jsonString stringByTrimmingCharactersInSet:[NSCharacterSetwhitespaceAndNewlineCharacterSet]];

    

    

    NSString *newStr=[ImportContactListViewControllerdelSpaceAndNewline:jsonString];

    

    

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

    

    

    

    NSString *character =nil;

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

        

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

        if ([characterisEqualToString:@"/n"]){

            

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

        }

    }

    

    NSString *character1 =nil;

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

        

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

        if ([characterisEqualToString:@"/r"]){

            

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

        }

    }

    

    

    

    

    

    [dict setObject:muStrforKey:@"item"];

    

    

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

    

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

    [request POSTNewRequest:dicturl:strUrl];

    

    [request block:^(id responSEObject) {

        

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

        [selfGivePromptView:[responseObjectobjectForKey:@"msg"]];

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

        if ([resultisEqualToString:@"1"]) {

            

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

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

                MyPersonCirleViewController *myPersonCirleVC = [[MyPersonCirleViewControlleralloc]init];

                myPersonCirleVC.vcType=@"list";

                self.vcType=@"importSuccess";

                myPersonCirleVC.backStr =@"下一步";

                

                NSUserDefaults *user=[NSUserDefaultsstandardUserDefaults];

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

                [user synchronize];

                

                

                [self.navigationControllerpushViewController:myPersonCirleVCanimated:YES];

            });

        }

    }];

    

}

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

    

    

    

    NSMutableString *mutStr = [NSMutableStringstringWithString:string];

    

    NSRange range = {0,mutStr.length};

    

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

    

    NSRange range2 = {0,mutStr.length};

    

    [mutStr replaceOccurrencesOfString:@"/n"withString:@""options:NSLiteralSearchrange:range2];

    

    return mutStr;

}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表