在 android 中,paytm 和 truecaller 等一些应用程序在 android 联系人列表中显示了他们的自定义链接,我们可以在 iOS 中实现相同的功能吗?
例如看截图
这是 Swift 3 添加新联系人的部分副本,其中包含电话和电子邮件信息
创建一个新联系人并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)
}
}
在@Alex Chase 的回答的帮助下,我确实实现了我的要求,这是我的代码。
我添加了联系人,或者我也可以编辑联系人以将 url 添加到我的应用程序中。
/* 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];