我可以在iOS本机联系人中添加应用的自定义链接吗?

时间:2018-06-20 06:31:58

标签: ios contacts

在android中,某些应用程序(如paytm和truecaller)在android联系人列表中显示其自定义链接,我们可以在iOS中实现相同的功能吗?

例如查看屏幕截图

enter image description here

2 个答案:

答案 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];