如何获得iphone的联系人详细信息,并使该联系人的CSV文件 [英] How to get contacts detail of iphone and make CSV file of that contact

查看:134
本文介绍了如何获得iphone的联系人详细信息,并使该联系人的CSV文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在iPhone中获取联系人详细信息,包括名字,姓氏,电话号码,电话号码类型,电子邮件地址,电子邮件地址类型等信息。

I want to get contact details in an iPhone with information like First Name, Last Name, Phone Number, Phone Number Type, Email Address, Email Address Type etc..

任何人都可以帮助我吗?

Can anyone help me with that?

我想在特定iPhone的联系人详细信息中创建.csv文件。我要获取iPhone通讯录数据。

I want to make a .csv file out of the contact details in a particular iPhone. I want to fetch iPhone address book data.

推荐答案

以下是获取iPhone通讯录的所有信息的代码...

Following is the code to get all informations of iPhone contact book...

    -(void)collectContacts
    {
        NSMutableDictionary *myAddressBook = [[NSMutableDictionary alloc] init];
        ABAddressBookRef addressBook = ABAddressBookCreate();
        CFArrayRef people  = ABAddressBookCopyArrayOfAllPeople(addressBook);
        for(int i = 0;i<ABAddressBookGetPersonCount(addressBook);i++)
        {
            ABRecordRef ref = CFArrayGetValueAtIndex(people, i);

            // Get First name, Last name, Prefix, Suffix, Job title 
            NSString *firstName = (NSString *)ABRecordCopyValue(ref,kABPersonFirstNameProperty);
            NSString *lastName = (NSString *)ABRecordCopyValue(ref,kABPersonLastNameProperty);
            NSString *prefix = (NSString *)ABRecordCopyValue(ref,kABPersonPrefixProperty);
            NSString *suffix = (NSString *)ABRecordCopyValue(ref,kABPersonSuffixProperty);
            NSString *jobTitle = (NSString *)ABRecordCopyValue(ref,kABPersonJobTitleProperty);

            [myAddressBook setObject:firstName forKey:@"firstName"];
            [myAddressBook setObject:lastName forKey:@"lastName"];
            [myAddressBook setObject:prefix forKey:@"prefix"];
            [myAddressBook setObject:suffix forKey:@"suffix"];
            [myAddressBook setObject:jobTitle forKey:@"jobTitle"];

            NSMutableArray *arPhone = [[NSMutableArray alloc] init];
            ABMultiValueRef phones = ABRecordCopyValue(ref, kABPersonPhoneProperty);
            for(CFIndex j = 0; j < ABMultiValueGetCount(phones); j++)
            {       
                CFStringRef phoneNumberRef = ABMultiValueCopyValueAtIndex(phones, j);   
                NSString *phoneLabel =(NSString*) ABAddressBookCopyLocalizedLabel (ABMultiValueCopyLabelAtIndex(phones, j));
                NSString *phoneNumber = (NSString *)phoneNumberRef; 
                NSMutableDictionary *temp = [[NSMutableDictionary alloc] init];
                [temp setObject:phoneNumber forKey:@"phoneNumber"];
                [temp setObject:phoneLabel forKey:@"phoneNumber"];
                [arPhone addObject:temp];
                [temp release];
            }
            [myAddressBook setObject:arPhone forKey:@"Phone"];
            [arPhone release];            

            CFStringRef address;
            CFStringRef label;
            ABMutableMultiValueRef multi = ABRecordCopyValue(ref, kABPersonAddressProperty);    
            for (CFIndex i = 0; i < ABMultiValueGetCount(multi); i++) 
            {           
                label = ABMultiValueCopyLabelAtIndex(multi, i);
                CFStringRef readableLabel = ABAddressBookCopyLocalizedLabel(label);             
                address = ABMultiValueCopyValueAtIndex(multi, i);   
                CFRelease(address);
                CFRelease(label);
            } 

            ABMultiValueRef emails = ABRecordCopyValue(ref, kABPersonEmailProperty);
            NSMutableArray *arEmail = [[NSMutableArray alloc] init];
            for(CFIndex idx = 0; idx < ABMultiValueGetCount(emails); idx++)
            {
                CFStringRef emailRef = ABMultiValueCopyValueAtIndex(emails, idx);
                NSString *strLbl = (NSString*) ABAddressBookCopyLocalizedLabel (ABMultiValueCopyLabelAtIndex (emails, idx));
                NSString *strEmail_old = (NSString*)emailRef;
                NSMutableDictionary *temp = [[NSMutableDictionary alloc] init];
                [temp setObject:strEmail_old forKey:@"strEmail_old"];
                [temp setObject:strLbl forKey:@"strLbl"];
                [arEmail addObject:temp];
                [temp release];
            }
            [myAddressBook setObject:arEmail forKey:@"Email"];
            [arEmail release];
        }
        [self createCSV:myAddressBook];
    }

    -(void) createCSV :(NSMutableDictionary*)arAddressData
    {   
        NSMutableString *stringToWrite = [[NSMutableString alloc] init];
        [stringToWrite appendString:[NSString stringWithFormat:@"%@,",[arAddressData valueForKey:@"firstName"]]];
        [stringToWrite appendString:[NSString stringWithFormat:@"%@,",[arAddressData valueForKey:@"lastName"]]];
        [stringToWrite appendString:[NSString stringWithFormat:@"%@,",[arAddressData valueForKey:@"jobTitle"]]];
        //[stringToWrite appendString:@"fname, lname, title, company, phonetype1, value1,phonetype2,value,phonetype3,value3phonetype4,value4,phonetype5,value5,phonetype6,value6,phonetype7,value7,phonetype8,value8,phonetype9,value9,phonetype10,value10,email1type,email1value,email2type,email2value,email3type,email3‌​value,email4type,email4value,email5type,email5value,website1,webs‌​ite2,website3"]; 
        NSMutableArray *arPhone = (NSMutableArray*) [arAddressData valueForKey:@"Phone"];
        for(int i = 0 ;i<[arPhone count];i++)
        {
            NSMutableDictionary *temp = (NSMutableDictionary*) [arPhone objectAtIndex:i];
            [stringToWrite appendString:[NSString stringWithFormat:@"%@,",[temp valueForKey:@"phoneNumber"]]];
            [stringToWrite appendString:[NSString stringWithFormat:@"%@,",[temp valueForKey:@"phoneNumber"]]];
            [temp release];
        }
        NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
        NSString *documentDirectory=[paths objectAtIndex:0];
        NSString *strBackupFileLocation = [NSString stringWithFormat:@"%@/%@", documentDirectory,@"ContactList.csv"];
        [stringToWrite writeToFile:strBackupFileLocation atomically:YES encoding:NSUTF8StringEncoding error:nil];
    }

这篇关于如何获得iphone的联系人详细信息,并使该联系人的CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆