如何在swift中设置最喜欢的联系人?

时间:2023-01-17 14:21:25

I am developing an application in which i need to fetch all the contacts from device and then set it to favorite contact on button press. I am able to fetch all contacts using [CNContact] in iOS 9 and 10. But don't know how to set it as a favorite contact.

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

Can we set CNContact as a favorite contact? Can we make changes in CNContact?

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

Any help is appreciable.

任何帮助都是明显的。

1 个解决方案

#1


1  

You can store Favourites to Realm DB. Like This,

您可以将最喜欢的存储到领域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)
        }

    }

#1


1  

You can store Favourites to Realm DB. Like This,

您可以将最喜欢的存储到领域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)
        }

    }