答案 0 :(得分:1)
部分重复 Swift 3 add new contact with phone and email information
创建一个新联系人,并在urlAddresses
属性中填充要与新联系人关联的URL字符串数组。
import Contacts
import ContactsUI
...
func addPhoneNumber(phNo : String) {
if #available(iOS 9.0, *) {
let store = CNContactStore()
let contact = CNMutableContact()
let homePhone = CNLabeledValue(label: CNLabelHome, value: CNPhoneNumber(stringValue :phNo ))
contact.phoneNumbers = [homePhone]
contact.urlAddresses = ["myURL1", "myURL2"]
let controller = CNContactViewController(forUnknownContact : contact)
controller.contactStore = store
controller.delegate = self
self.navigationController?.setNavigationBarHidden(false, animated: true)
self.navigationController!.pushViewController(controller, animated: true)
}
}
答案 1 :(得分:0)
我确实通过@Alex Chase的答案达到了要求,这是我的代码。
我添加了联系人,或者我也可以编辑联系人以将网址添加到我的应用中。
/* Add demo contact */
CNContactStore *store = [[CNContactStore alloc] init];
CNMutableContact *contact = [CNMutableContact new];
NSString *url = [@"data_to_be_passed" stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]];
CNLabeledValue *value1 = [[CNLabeledValue alloc] initWithLabel:CNLabelHome value:[CNPhoneNumber phoneNumberWithStringValue:@"9999999999"]];
CNLabeledValue *value2 = [[CNLabeledValue alloc] initWithLabel:@"CRMNEXT" value:[@"openMyApp://" stringByAppendingString:url]];
contact.phoneNumbers = @[value1];
contact.urlAddresses = @[value2];
contact.givenName = @"Harry Potter";
CNSaveRequest *request = [[CNSaveRequest alloc] init];
[request addContact:contact toContainerWithIdentifier:nil];
[store executeSaveRequest:request error:nil];