如何在Swift中以编程方式将联系人设置为收藏联系人?

时间:2017-06-10 12:04:59

标签: iphone swift contacts cncontact

我正在开发一个应用程序,我需要从设备中获取所有联系人,然后按下按钮将其设置为收藏联系人。我可以在iOS 9和10中使用[CNContact]获取所有联系人。但不知道如何将其设置为最喜欢的联系人。

我们可以将CNContact设置为最喜欢的联系人吗? 我们可以在CNContact中进行更改吗?

1 个答案:

答案 0 :(得分:1)

您可以将收藏夹存储到Realm DB。像这样,

class FavouriteList: Object {
    let favouriteList : List<FavouriteContact> = List<FavouriteContact>()

}
class FavouriteContact: Object {
    dynamic var identifier : String? = ""
    override class func primaryKey() -> String? {
        return "identifier"
    }
}


    // Add Favourite Contact in Realm
    class func add(identifier: String) -> Bool {
        var realm: Realm!
        do {
            realm = try Realm()
            realm.beginWrite()
        } catch {
            print(error.localizedDescription)
        }
        let realmTask: FavouriteList= FavouriteList()
        let favContact: FavouriteContact = FavouriteContact()

        // Check ID Exist or Not
        let idExists: FavouriteContact? = realm.object(ofType: FavouriteContact.self, forPrimaryKey: identifier)
        if idExists?.identifier != nil {
            realm.cancelWrite()
            return false

        } else {
            favContact.identifier = identifier
            realmTask.favouriteList.append(favContact)
            realm.add(realmTask)
        }

        // Realm Commit
        do {
            try realm.commitWrite()
        } catch {
            print("Realm Task Write Error : ", error.localizedDescription)
        }
        return true

    }

    // Remove Favourite Contact

    class func remove(identifier: String) -> Bool {
        var realm: Realm!
        do {
            realm = try Realm()
            realm.beginWrite()
        } catch {
            print(error.localizedDescription)
        }

        // Check ID Exist or Not
        let idExists: FavouriteContact? = realm.object(ofType: FavouriteContact.self, forPrimaryKey: identifier)
        if idExists?.identifier != nil {
            realm.delete(idExists!)
        } else {
            realm.cancelWrite()
            return false
        }
        // Realm Commit
        do {
            try realm.commitWrite()
        } catch {
            print("Realm Task Write Error : ", error.localizedDescription)
        }
        return true
    }

    // Get Favourite List
    class func get(completionHandler: @escaping (_ result: [CNContact]) -> ()) {
        var favourites: [CNContact] = [CNContact]()
        do {
            let realm = try Realm()
            let dataRealmContacts: Results<FavouriteList> = realm.objects(FavouriteList.self)
            for item in dataRealmContacts {
                for contactID in item.favouriteList {
                    if contactID.identifier != nil {
                        favourites.append(getContactFromID(identifier: contactID.identifier!))
                    }
                }
            }
            completionHandler(favourites)
        } catch {
            print(error.localizedDescription)
            completionHandler(favourites)
        }

    }
相关问题