导出通讯录

因为手机要越狱,用91等导出的不是txt格式的文件,所以简单的写了个导出的工具,因为只是自己用,没考虑内存等问题。

在Info.plist中增加下面项,并设为YES,在iTunes中就可以看到生成的文件了。

Application supports iTunes file sharing


导出的代码:

#import <AddressBook/AddressBook.h>

#import <AddressBookUI/AddressBookUI.h>



-(void)exportAction{

    ABAddressBookRef addressBook;

    CFErrorRef error = nil;

    addressBook = ABAddressBookCreateWithOptions(NULL,&error);

    ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {

        // callback can occur in background, address book must be accessed on thread it was created on

        dispatch_async(dispatch_get_main_queue(), ^{

            if(!error && granted){

                

                NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

                NSString *basePath = [paths objectAtIndex:0];

                NSString *path = [NSString stringWithFormat:@"%@/Contacts.txt",basePath];

                

                

                CFArrayRef peopleRef = ABAddressBookCopyArrayOfAllPeople(addressBook);

                

                 [@"开始" writeToFile:path atomically:YES encoding:NSUnicodeStringEncoding error:nil];

                NSFileHandle *myHandle = [NSFileHandle fileHandleForUpdatingAtPath:path ];

                

                int i = 0;

                

                NSArray *people = (__bridge_transfer NSArray *)peopleRef;

                for (id record in people) {

                    

                    i += 1;

                    

                    [myHandle seekToEndOfFile];

                    [myHandle writeData:[ [NSString stringWithFormat:@"\n\n%d",i] dataUsingEncoding:NSUnicodeStringEncoding]];

                    

  

                    ABRecordRef recordRef = (__bridge_retained ABRecordRef)record;

                    

                    NSString* firstName = (__bridge_transfer NSString *)ABRecordCopyValue(recordRef, kABPersonFirstNameProperty);

                    [myHandle seekToEndOfFile];

                    [myHandle writeData:[[NSString stringWithFormat:@"\nfirstName:%@",firstName] dataUsingEncoding:NSUnicodeStringEncoding]];

                    

                    

                    NSString* lastName = (__bridge_transfer NSString *)ABRecordCopyValue(recordRef, kABPersonLastNameProperty);

                    [myHandle seekToEndOfFile];

                    [myHandle writeData:[[NSString stringWithFormat:@"\nlastName:%@",lastName] dataUsingEncoding:NSUnicodeStringEncoding]];

                    

                    

                    NSLog(@"firstName:%@",firstName);

                    

                    

                    ABMultiValueRef telefonos =ABRecordCopyValue(recordRef, kABPersonPhoneProperty);

                    NSString* mobile=@"";

                    NSString* mobileLabel;

                    

                    for(CFIndex i = 0; i < ABMultiValueGetCount(telefonos); i++) {

                        mobileLabel = (NSString*)CFBridgingRelease(ABMultiValueCopyLabelAtIndex(telefonos, i));

                        mobile = ( NSString*)CFBridgingRelease(ABMultiValueCopyValueAtIndex(telefonos, i));

                        

                        [myHandle seekToEndOfFile];

                        [myHandle writeData:[[NSString stringWithFormat:@"\n%@:%@",mobileLabel,mobile] dataUsingEncoding:NSUnicodeStringEncoding]];

                    }

                    if (telefonos)

                    {

                        CFRelease(telefonos);

                    }

                }

                

                

                CFRelease(addressBook);

            }

        });

    });

    

    

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值