Nazrul Islam
Nazrul Islam

Reputation: 768

How to set contact as a favorite contact programmatically in Swift?

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.

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

Upvotes: 2

Views: 936

Answers (1)

Satish Babariya
Satish Babariya

Reputation: 3096

You can store Favourites to Realm DB. Like This,

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)
        }

    }

Upvotes: 1

Related Questions