模拟添加联系人到通讯录

- (void)viewDidLoad {

    [super viewDidLoad];

    //创建数据

    NSArray *arrName=@[@"小伙",@"小红",@"小白"];

    NSArray *arrPhone=@[@"18736777750",@"18991325325",@"12345678901"];

    NSString *doc=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];

     NSString *fileName=[doc stringByAppendingString:@"contact.db"];

    FMDatabase *fmdb=[FMDatabase databaseWithPath:fileName];

    if ([fmdb open]) {

        BOOL result=[fmdb executeUpdate:@"create table if not exists contact(id integer PRIMARY KEY AUTOINCREMENT,name text,phone,text)"];

        if (result) {

            NSLog(@"创建成功");

        }else

        {

            NSLog(@"fail");

        }

    }

    //插入数据

//    for (int i=0; i<arrName.count; i++) {

//        [fmdb executeUpdate:@"insert into contact(name,phone)values(?,?)",arrName[i],arrPhone[i]];

//    }

    //查出所有数据

    NSMutableArray *arr1=[NSMutableArray new];

    NSMutableArray *arr2=[NSMutableArray new];

    FMResultSet *resultSet=[fmdb executeQuery:@"select *from contact"];

    while ([resultSet next]) {

        NSString *name=[resultSet stringForColumn:@"name"];

        NSString *phone=[resultSet stringForColumn:@"phone"];

        [arr1 addObject:name];

        [arr2 addObject:phone];

        

        

    }

    NSLog(@"==%@%@",arr1 ,arr2);

    

    self.view.backgroundColor=[UIColor whiteColor];

    

//    //创建联系人

//    CNMutableContact *contact=[[CNMutableContact alloc]init];

//    contact.imageData=UIImagePNGRepresentation([UIImage imageNamed:@"page_2_b.png"]);

//    contact.givenName=@"画";

//    contact.familyName=@"friend";

//    //创建联系人

//    CNMutableContact *contact1=[[CNMutableContact alloc]init];

//    contact1.imageData=UIImagePNGRepresentation([UIImage imageNamed:@"page_2_b.png"]);

//    contact1.givenName=@"🌹";

//    contact1.familyName=@"Ed";

    CNMutableContact *contact1;

    for (int i=0; i<arrName.count; i++) {

        contact1=[[CNMutableContact alloc]init];

        contact1.imageData=UIImagePNGRepresentation([UIImage imageNamed:@"page_2_b.png"]);

        contact1.givenName=arr1[i];

        contact1.familyName=@"aaaaaa";

        //设置联系人邮箱

        CNLabeledValue *homeEmail=[CNLabeledValue labeledValueWithLabel:CNLabelHome value:@"1361226414@qq.com"];

        CNLabeledValue *workEmail=[CNLabeledValue labeledValueWithLabel:CNLabelWork value:@"1361226414@qq.com"];

        contact1.emailAddresses=@[homeEmail,workEmail];

        //设置联系人电话

        contact1.phoneNumbers=@[[CNLabeledValue labeledValueWithLabel:CNLabelPhoneNumberiPhone value:[CNPhoneNumber phoneNumberWithStringValue:arr2[i]]]];

        

        //创建添加联系人请求

        CNSaveRequest *saveRequest=[[CNSaveRequest alloc]init];

        //添加联系人

        

        [saveRequest addContact:contact1 toContainerWithIdentifier:nil];

        // [saveRequest addContact:contact1 toContainerWithIdentifier:nil];

        

        //存取联系人的上下文桥梁

        

        CNContactStore *store=[[CNContactStore alloc]init];

        [store executeSaveRequest:saveRequest error:nil];

    }

 

posted @ 2016-05-20 15:26  尸者禾火雨相  阅读(198)  评论(0编辑  收藏  举报