如何使用目标c将多个电话号码保存到电话中?

问题描述 投票:0回答:1

[我正在尝试创建一个简单的应用,在其中我将获得一个JSON,其中包含姓名,电话号码和电子邮件等联系方式。因此,当我单击按钮时,我希望所有这些号码都保存在手机中。

我尝试解析JSON并将联系人保存代码放入循环中进行保存,但是我看不到新联系人

我的JSON格式

[ {
        "Name": "name1",
        "mobile": 8634003462503653464,
        "email": "[email protected]"
    },
    {
        "Name": "name2",
        "mobile": 912343576655686,
        "email": "[email protected]"
    }
]

代码

- (IBAction)button:(id)sender {


NSString *filePath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"json"];
NSData *data = [NSData dataWithContentsOfFile:filePath];
NSMutableArray *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
//NSLog(@"%@", json);

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

// create contact

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

for(NSDictionary *dict in json){
    //NSLog(@"%@", dict);
    //NSLog(@"name %@",dict[@"Name"]);

    NSString *finalname = [NSString stringWithFormat:@"%@ Marketing",dict[@"Name"]];
    contact.familyName = dict[@"Name"];
    contact.givenName = finalname;
    CNLabeledValue *homePhone = [CNLabeledValue labeledValueWithLabel:CNLabelHome value:[CNPhoneNumber phoneNumberWithStringValue:[NSString stringWithFormat:@"+91%@",dict[@"mobile"]]]];
    contact.phoneNumbers = @[homePhone];


    CNContactViewController *controller = [CNContactViewController viewControllerForUnknownContact:contact];
    controller.contactStore = store;
    controller.delegate = self;
    [self.navigationController pushViewController:controller animated:TRUE];
}


}

因此,我在JSON中有500多个联系人列表,我试图通过单击按钮将所有这些联系人保存在手机中

ios objective-c contacts
1个回答
0
投票

您可以通过以下方式更新联系人。您不需要CNContactViewController

CNSaveRequest * saveRequest = [[CNSaveRequest alloc]init];
[saveRequest updateContact:contact];
NSError *updateError = nil;
[store executeSaveRequest:saveRequest error:&updateError];
© www.soinside.com 2019 - 2024. All rights reserved.