[英]How to save contacts on local ios device using swift language?
I call my addContacts function(see code) in viewWillAppear. 我在viewWillAppear中调用我的addContacts函数(参见代码)。
What I want is that when I run the code, the values that I provided in firstName and lastName strings gets stored as a contact in the built-in iOS contacts app. 我想要的是,当我运行代码时,我在firstName和lastName字符串中提供的值将作为联系人存储在内置的iOS联系人应用程序中。
It is working in the simulator, but it isn't coming under the right section. 它正在模拟器中运行,但是不在正确的部分中。 For example if I pass "David" and "Ginger" as firstName and lastName strings, it adds the contact "David Ginger" in the app but it comes under '#' section, and not under "D" section(or even "G" section since by default sorting in contacts app is through surname). 例如,如果我将“ David”和“ Ginger”作为firstName和lastName字符串传递,它将在应用程序中添加联系人“ David Ginger”,但它位于“#”部分,而不是“ D”部分(甚至“ G”下) ”部分,因为默认情况下,联系人应用中的排序是通过姓氏进行的)。 Moreover, it doesn't work at all on my iPhone. 而且,它根本无法在我的iPhone上运行。 I even gave a button which will implement the function when clicked, but it doesn't work. 我什至给了一个按钮,单击该按钮即可实现该功能,但不起作用。
PS - I'm trying to develop an iOS in-built contacts app like application. PS-我正在尝试开发类似应用程序的iOS内置联系人应用程序。
I searched a lot on the internet and considering what's mentioned on apple's developer website, my function is pretty much correct. 我在互联网上搜索了很多内容,考虑到苹果开发者网站上提到的内容,我的功能非常正确。 I couldn't find anything helpful on the internet. 我在互联网上找不到任何有用的信息。
addContacts() {
let store = CNContactStore()
let storeMutable = CNMutableContact()
storeMutable.namePrefix = firstName
storeMutable.nameSuffix = lastName
let saveRequest = CNSaveRequest()
saveRequest.add(storeMutable, toContainerWithIdentifier: nil)
try! store.execute(saveRequest)
}
//calling this function in viewWillAppear
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
fetchContacts()
addContacts()
}
I want my app to save the contact permanently on the local device. 我希望我的应用将联系人永久保存在本地设备上。 Just like the built-in contacts app. 就像内置的联系人应用程序一样。
my function is pretty much correct 我的功能很正确
No, it isn't. 不,不是。 You have misunderstood the CNContact properties. 您误解了CNContact属性。 Your contact has no name; 您的联系人没有姓名; therefore it is not sorted among contacts that do have a name. 因此,不就是有名字的接触之中分类。 So start by giving it a name! 所以先给它起个名字吧! Change 更改
storeMutable.namePrefix = firstName
storeMutable.nameSuffix = lastName
To 至
storeMutable.givenName = firstName
storeMutable.familyName = lastName
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.