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

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?

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

Related

Prevent delete array of object adding again in realm db

I have an api who fetch data and add it in realm, what i want is whenever i delete realm object and refresh the page and called the api. the object that already delete is not showing/adding again in realm db. How to do it? here is my code
class TickerRealmStorage: RealmStorage, TickerStorage {
private var items = RealmSwift.List<Ticker>()
private var temptTicker: Ticker?
func get() -> RealmSwift.List<Ticker> {
return items
}
func save(messages: [Message]) {
messages.forEach { message in
let ticker = Ticker()
ticker.campaignID = message.campaignID
ticker.isRead = message.isRead
ticker.messageBody = message.body
if let temptTicker = temptTicker, ticker.campaignID == temptTicker.campaignID {
try! realm.write {
realm.delete(ticker)
}
}
items.append(ticker)
}
do {
try realm.write {
realm.add(items, update: .modified)
}
} catch {
fatalError("Failed to save tickers")
}
}
func delete(ticker: Ticker) {
temptTicker = ticker
try! realm.write {
realm.delete(ticker)
}
}
}
Should i add a temptItems so i check if the data already delete not adding into list items.

Swift app crashing when attempting to fetch contacts

I am creating an onboarding portion of an app which gets the user's contacts to check which already have the app to add as friends. I'm using the CNContact framework. I have created several methods I'm using to get a full list of the users' contacts, check if they have the app, and enumerate them in a UITableView. However, when the view loads, the app crashes with the error "A property was not requested when contact was fetched." I already make a fetch request with the keys CNContactGivenNameKey, CNContactFamilyNameKey, CNContactPhoneNumbersKey, ad CNContactImageDataKey. I've included all my code here:
import Foundation
import Contacts
import PhoneNumberKit
struct ContactService {
static func createContactArray() -> [CNContact] {
var tempContacts = [CNContact]()
let store = CNContactStore()
store.requestAccess(for: .contacts) { (granted, error) in
if let _ = error {
print("failed to request access to contacts")
return
}
if granted {
let keys = [CNContactGivenNameKey, CNContactFamilyNameKey, CNContactPhoneNumbersKey, CNContactImageDataKey]
let request = CNContactFetchRequest(keysToFetch: keys as [CNKeyDescriptor])
request.sortOrder = CNContactSortOrder.familyName
do {
try store.enumerateContacts(with: request, usingBlock: { (contact, stop) in
tempContacts.append(contact)
})
} catch {
print("unable to fetch contacts")
}
print("created contact list")
}
}
return tempContacts
}
static func createFetchedContactArray(contactArray: [CNContact], completion: #escaping ([FetchedContact]?) -> Void) -> Void {
var temp = [FetchedContact]()
getNumsInFirestore { (nums) in
if let nums = nums {
for c in contactArray {
let f = FetchedContact(cnContact: c, numsInFirebase: nums)
temp.append(f)
}
return completion(temp)
} else {
print("Error retrieving numbers")
}
}
return completion(nil)
}
static func getNumsInFirestore(_ completion: #escaping (_ nums : [String]?) -> Void ) -> Void {
var numsInFirebase = [String]()
let db = FirestoreService.db
db.collection(Constants.Firestore.Collections.users).getDocuments() { (querySnapshot, err) in
if let err = err {
print("Error getting user documents: \(err)")
completion(nil)
} else {
for doc in querySnapshot!.documents {
let dict = doc.data()
numsInFirebase.append(dict[Constants.Firestore.Keys.phone] as! String)
}
completion(numsInFirebase)
}
}
}
static func getPhoneStrings(contact: CNContact) -> [String] {
var temp = [String]()
let cnPhoneNums = contact.phoneNumbers
for n in cnPhoneNums {
temp.append(n.value.stringValue)
}
return temp
}
static func hasBump(contact: CNContact, completion: #escaping (_ h: Bool) -> Void ) -> Void {
let contactNums = ContactService.getPhoneStrings(contact: contact)
ContactService.getNumsInFirestore { (nums) in
if let nums = nums {
return completion(contactNums.contains(where: nums.contains))
} else {
print("Error retrieving numbers from firestore")
return completion(false)
}
}
}
static func anyContactsWithBump(completion: #escaping (_ yes: Bool) -> Void) {
let contacts = createContactArray()
var tempBool = false
let workgroup = DispatchGroup()
for c in contacts {
workgroup.enter()
hasBump(contact: c) { (has) in
if has {
tempBool = true
}
workgroup.leave()
}
}
workgroup.notify(queue: .main) {
completion(tempBool)
}
}
}
Then I call the methods in the view controller class:
import UIKit
import Contacts
import FirebaseDatabase
import Firebase
import FirebaseFirestore
class AddContactsVC: UIViewController {
var fetchedContactsWithBump: [FetchedContact] = []
override func viewDidLoad() {
let cnContacts = ContactService.createContactArray()
var contactsWithBump = [CNContact]()
let workgroup = DispatchGroup()
for contact in cnContacts {
workgroup.enter()
ContactService.hasBump(contact: contact) { (has) in
if has {
contactsWithBump.append(contact)
}
workgroup.leave()
}
}
workgroup.notify(queue: .main) {
print("Number of contacts with Bump: \(contactsWithBump.count)")
ContactService.createFetchedContactArray(contactArray: contactsWithBump) { (fetchedContacts) in
if let fetchedContacts = fetchedContacts {
self.fetchedContactsWithBump = fetchedContacts
} else {
print("Error creating fetchedContacts array.")
}
}
}
I also get the message "Error creating fetchedContacts array" in the console, so I know something is going wrong with that method, I'm just not sure what. Any help is appreciated!
Edit: The exception is thrown at 3 points: 1 at the first line of my FetchedContact init method, which looks like this:
init(cnContact: CNContact, numsInFirebase: [String]) {
if cnContact.imageDataAvailable { self.image = UIImage(data: cnContact.imageData!) }
It also points to the line let f = FetchedContact(cnContact: c, numsInFirebase: nums) in createFetched contact array, and finally at my completion(numsInFirebase) call in getNumsInFirestore.
To start with
let contacts = createContactArray()
will always return an empty array.
This function has a return statement outside the closure so will immediately return an empty array.
Change createContactArray to use a completion handler like the other functions you have to populate contacts from inside the closure.

Swift core data change bool stored

I am making and expanding an app for lists and adding extra data as I go. I have added a bool to mark the item as completed.
I want to change the value of a bool stored in core data. I can add and delete orders but now I'm looking to change properties. Please can I have help on the best way to change these?
I have made a func changeCompleated, but can't work out how to get it to work in my core data manager.
class CoreDataManager {
static let shared = CoreDataManager(moc: NSManagedObjectContext.current)
var moc: NSManagedObjectContext
private init(moc: NSManagedObjectContext) {
self.moc = moc
}
private func fetchOrder(name: String) -> Order? {
var orders = [Order]()
let request: NSFetchRequest<Order> = Order.fetchRequest()
request.predicate = NSPredicate(format: "name == %#", name)
do {
orders = try self.moc.fetch(request)
} catch let error as NSError {
print(error)
}
return orders.first
}
func changeCompleated(name:String, completed: Bool) {
do {
if let order = fetchOrder(name: name) {
self.moc.perform {
}
try self.moc.save()
}
} catch let error as NSError {
print(error)
}
}
func deleteOrder(name: String) {
do {
if let order = fetchOrder(name: name) {
self.moc.delete(order)
try self.moc.save()
}
} catch let error as NSError {
print(error)
}
}
func getAllOrders() -> [Order] {
var orders = [Order]()
let orderRequest: NSFetchRequest<Order> = Order.fetchRequest()
do {
orders = try self.moc.fetch(orderRequest)
} catch let error as NSError {
print(error)
}
return orders
}
func saveOrder(id: UUID, name: String, type: String, qty: Double, urgent: Bool, complete: Bool) {
let order = Order(context: self.moc)
order.id = id
order.name = name
order.type = type
order.qty = qty
order.urgent = urgent
order.complete = complete
do {
try self.moc.save()
} catch let error as NSError {
print(error)
}
}
}
Almost, just add a line to change the value, the perform block is not needed
func changeCompleated(name: String, completed: Bool) {
guard let order = fetchOrder(name: name) else { return }
do {
order.complete = completed
try self.moc.save()
} catch {
print(error)
}
}
And you can also shorten fetchOrder
private func fetchOrder(name: String) -> Order? {
let request: NSFetchRequest<Order> = Order.fetchRequest()
request.predicate = NSPredicate(format: "name == %#", name)
do {
return try self.moc.fetch(request).first
} catch {
print(error)
}
}

How to check Observable count in rxswift?

I have this method return User observable
internal func getUser() -> Observable<User> {
let result = withRealm("getting user") { realm -> Observable<User> in
let realm = try Realm()
let user = realm.objects(User.self).first
return Observable.from(optional: user)
}
return result ?? .empty()
}
Now i want to have another method that returns me if user is loggedin, how it will be done?
private var _isLoggedIn: Observable<User> {
return getUser().count > 0 //error
}
I would suggest moving to enum to get rid of optionals on make it easier to manage:
enum LoginState {
case loggedIn(user: User)
case loggedOut
}
internal func getLoginState() -> Observable<LoginState> {
let user = withRealm("getting user") { realm -> User? in
let realm = try Realm()
return realm.objects(User.self).first
}
if let user = user {
return Observable.just(.loggedIn(user))
} else {
return Observable.just(.loggedOut)
}
}
private var _isLoggedIn: Observable<Bool> {
return getLoginState().map {
switch $0 {
case .loggedIn: return true
case .loggedOut: return false
}
}
}

Swift Using Contacts Framework, search using Identifier to match

Trying unsuccessfully to get a contact match providing contact Identifier. I desire to return contact, then to use the image associated with it. I am getting a nil match. thank you. This code I got from a demo, I'm kinda new to programming
import Contacts
var contact = CNContact()
var contactStore = CNContactStore()
let foundContact = getContactFromID("94AAD3B1-E9E1-48C9-A796-F7EC1014230A")
func getContactFromID(contactID: String) -> CNContact {
AppDelegate.getAppDelegate().requestForAccess { (accessGranted) -> Void in
if accessGranted {
let predicate = CNContact.predicateForContactsWithIdentifiers([contactID])
let keys = [CNContactGivenNameKey, CNContactFamilyNameKey, CNContactEmailAddressesKey, CNContactPhoneNumbersKey, CNContactImageDataKey, CNContactImageDataAvailableKey]
var contacts = [CNContact]()
var message: String!
let contactsStore = AppDelegate.getAppDelegate().contactStore
do {
contacts = try contactsStore.unifiedContactsMatchingPredicate(predicate, keysToFetch: keys)
if contacts.count == 0 {
message = "No contacts were found matching the given name."
}
}
catch {
message = "Unable to fetch contacts."
}
if message != nil {
dispatch_async(dispatch_get_main_queue(), { () -> Void in
Utility.showAlert(nil, message: message)
})
} else {
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.contact = contacts[0]
print("self.contact: \(self.contact)")
})
}
}
}
return self.contact
}
I solved it :), I removed the dispatch_async stuff, works now: here is fixed code.
func getContactFromID(contactID: String) -> CNContact {
AppDelegate.getAppDelegate().requestForAccess { (accessGranted) -> Void in
if accessGranted {
let predicate = CNContact.predicateForContactsWithIdentifiers([contactID])
let keys = [CNContactGivenNameKey, CNContactFamilyNameKey, CNContactEmailAddressesKey, CNContactPhoneNumbersKey, CNContactImageDataKey, CNContactImageDataAvailableKey]
var contacts = [CNContact]()
var message: String!
let contactsStore = AppDelegate.getAppDelegate().contactStore
do {
contacts = try contactsStore.unifiedContactsMatchingPredicate(predicate, keysToFetch: keys)
if contacts.count == 0 {
message = "No contacts were found matching the given name."
}
}
catch {
message = "Unable to fetch contacts."
}
self.contact = contacts[0]
}
}
return self.contact
}