fetch all Contact Phone Numbers in Array swift - swift

i fetched all contacts information exactly like below . At the moment i just fetched a Phone Number from contact . the question is how i can fetch all of Phone Numbers of a contact in String array to use it?
Store Data :
class ContactStruct : NSObject {
let identifier : String
let thumbnailImageData : UIImage?
let givenName : String
let familyName : String
let phoneNumbers : String
let emailAddresses : String
init(identi:String,img:UIImage?,name:String,family:String,phone:String,email:String) {
self.identifier = identi
self.thumbnailImageData = img
self.givenName = name
self.familyName = family
self.phoneNumbers = phone
self.emailAddresses = email
}
fetch method :
class func generateModelArray() -> [ContactStruct]{
let contactStore = CNContactStore()
var contactsData = [ContactStruct]()
let key = [CNContactGivenNameKey,CNContactFamilyNameKey,CNContactImageDataKey,CNContactThumbnailImageDataKey,CNContactPhoneNumbersKey,CNContactEmailAddressesKey,CNLabelPhoneNumberMobile] as [CNKeyDescriptor]
let request = CNContactFetchRequest(keysToFetch: key)
try? contactStore.enumerateContacts(with: request, usingBlock: { (contact, stoppingPointer) in
let givenName = contact.givenName
let familyName = contact.familyName
let emailAddress = contact.emailAddresses.first?.value ?? ""
let phoneNumber = contact.phoneNumbers.first?.value.stringValue ?? ""
let identifier = contact.identifier
var image : UIImage?
if contact.thumbnailImageData != nil{
image = UIImage(data: contact.thumbnailImageData!)!
}
contactsData.append(ContactStruct(identi: identifier, img: image, name: givenName, family: familyName, phone: phoneNumber, email: emailAddress as String))
})
return contactsData
}

you can use this
func extractNumber(data: [ContactStruct]) ->[String]
{
var arrNumbers = [String]()
for number in data
{
arrNumbers.append(number.phoneNumber)
}
return arrNumbers
}
and use like this:
let arrayNumbers = extractNumber(data: contactsData)
dump(arrayNumbers) //<- this line prints on console

Well first of all your phoneNumbers and emailAddresses variables should be an array of String and not String since you can have multiple of those.
More over i would make them a Dictionary like so [String:String] - more on that later.
class ContactStruct: NSObject {
let identifier: String
let thumbnailImageData: UIImage?
let givenName: String
let familyName: String
let phoneNumbers: [String:String] // <----- the new dictionaries
let emailAddresses: [String:String] // <--
init(identifier: String, img: UIImage?, name: String, family: String, phones: [String:String], emails: [String:String]) {
self.identifier = identifier
self.thumbnailImageData = img
self.givenName = name
self.familyName = family
self.phoneNumbers = phones
self.emailAddresses = emails
}
Secondly, when it comes to fetching phone numbers and emails you need to loop through them because they arrive of Type [CNLabeledValue<CNPhoneNumber>] which is an Array of phoneNumbers encased in CNLabledValue, which means that they have a value (phone number) and label which in this instance is type of phone number (i.e home, mobile, fax, esc.. more on that here - from Apple
Documentation.)
This is also why I would use a Dictionary [String:String] so you can keep them in a key-value relationship where the key will be phone type and the value is the phone number.
["mobile": "(494) 232 1134"] for example.
If I were you I would build a simple function to iterate through them and return your Dictionary ready, for example:
private func getPhoneNumbersFromContact(_ contact: CNContact) -> [String:String] {
var numbers: [String:String] = [:]
var i = 1
for number in contact.phoneNumbers {
if number.label != nil {
numbers[number.label!] = number.value.stringValue
} else {
numbers["unknown #\(i)"] = number.value.stringValue
i += 1
}
}
return numbers
}
You can always add your own flavour and style to it.
I would leave the job of writing the getEmailAddressesFromContact function for you :)
So your final generateModelArray Class might look something like this -
class func generateModelArray() -> [ContactStruct]{
let contactStore = CNContactStore()
var contactsData = [ContactStruct]()
let key = [CNContactGivenNameKey,CNContactFamilyNameKey,CNContactImageDataKey,CNContactThumbnailImageDataKey,CNContactPhoneNumbersKey,CNContactEmailAddressesKey,CNLabelPhoneNumberMobile] as [CNKeyDescriptor]
let request = CNContactFetchRequest(keysToFetch: key)
try? contactStore.enumerateContacts(with: request, usingBlock: { (contact, stoppingPointer) in
let givenName = contact.givenName
let familyName = contact.familyName
let emailAddress = getEmailAddressesFromContact(contact) // <--- only changes
let phoneNumber = getPhoneNumbersFromContact(contact) // <--- are here
let identifier = contact.identifier
var image : UIImage?
if contact.thumbnailImageData != nil{
image = UIImage(data: contact.thumbnailImageData!)!
}
contactsData.append(ContactStruct(identifier: identifier, img: image, name: givenName, family: familyName, phone: phoneNumber, email: emailAddress))
})
return contactsData
}

Related

Unpacking Firestore array with objects to a model in swift

I have a project in swift with Firestore for the database. My firestore dataset of a user looks like this. User details with an array that contains objects.
I have a function that gets the specifick user with all firestore data:
func fetchUser(){
db.collection("users").document(currentUser!.uid)
.getDocument { (snapshot, error ) in
do {
if let document = snapshot {
let id = document.documentID
let firstName = document.get("firstName") as? String ?? ""
let secondName = document.get("secondName") as? String ?? ""
let imageUrl = document.get("imageUrl") as? String ?? ""
let joinedDate = document.get("joinedDate") as? String ?? ""
let coins = document.get("coins") as? Int ?? 0
let challenges = document.get("activeChallenges") as? [Challenge] ?? []
let imageLink = URL(string: imageUrl)
let imageData = try? Data(contentsOf: imageLink!)
let image = UIImage(data: imageData!) as UIImage?
let arrayWithNoOptionals = document.get("activeChallenges").flatMap { $0 }
print("array without opt", arrayWithNoOptionals)
self.user = Account(id: id, firstName: firstName, secondName: secondName, email: "", password: "", profileImage: image ?? UIImage(), joinedDate: joinedDate, coins: coins, activeChallenges: challenges)
}
else {
print("Document does not exist")
}
}
catch {
fatalError()
}
}
}
This is what the user model looks like:
class Account {
var id: String?
var firstName: String?
var secondName: String?
var email: String?
var password: String?
var profileImage: UIImage?
var coins: Int?
var joinedDate: String?
var activeChallenges: [Challenge]?
init(id: String, firstName: String,secondName: String, email: String, password: String, profileImage: UIImage, joinedDate: String, coins: Int, activeChallenges: [Challenge]) {
self.id = id
self.firstName = firstName
self.secondName = secondName
self.email = email
self.password = password
self.profileImage = profileImage
self.joinedDate = joinedDate
self.coins = coins
self.activeChallenges = activeChallenges
}
init() {
}
}
The problem is I don't understand how to map the activeChallenges from firestore to the array of the model. When I try : let challenges = document.get("activeChallenges") as? [Challenge] ?? []
The print contains an empty array, but when i do: let arrayWithNoOptionals = document.get("activeChallenges").flatMap { $0 } print("array without opt", arrayWithNoOptionals)
This is the output of the flatmap:
it returns an optional array
System can not know that activeChallenges is array of Challenge object. So, you need to cast it to key-value type (Dictionary) first, then map it to Challenge object
let challengesDict = document.get("activeChallenges") as? [Dictionary<String: Any>] ?? [[:]]
let challenges = challengesDict.map { challengeDict in
let challenge = Challenge()
challenge.challengeId = challengeDict["challengeId"] as? String
...
return challenge
}
This is the same way that you cast snapshot(document) to Account object

Flutter Channel Method String Not Working as URL

I have a Channel Method in my Flutter project which takes a String (url), downloads an image and presents it as part of a new contact.
However, the image won't be displayed for most urls.
This is the code I use to get the image and add to the new contact, before displaying it in a CNContactViewController.
let urlString : String = (arguments["imgUrl"] as? String)!
let url = URL(string: urlString)
let data = try? Data(contentsOf: url!)
newContact.imageData = data
I have tried encoding the url and casting it as an NSString with no luck.
Interesting, if I hard code the url in like below, it works. So what is happening between sending it from my Dart source code, over to Swift?
(URL modified to conceal contents, real URL returns image)
let url = URL(string: "https://23ee7ca4ch9430946b76-4f3bba1a032272305d39a357e474f3b2.ssl.cf1.rackcdn.com/talent/81843293_1556369106.jpeg")
let data = try? Data(contentsOf: url!)
newContact.imageData = data
Update:
Here is the full code...
Dart:
static const platform = const MethodChannel('contacts');
static Future<String> addSystemContact({
String firstName,
String lastName,
String jobTitle,
String company,
String phone,
String email,
String city,
String country,
String profile,
String imgUrl,
}) async {
try {
var result = await platform.invokeMethod(
'addSystemContact',
{
'firstName' : firstName != null && firstName.isNotEmpty ? firstName : "",
'lastName' : lastName != null && lastName.isNotEmpty ? lastName : "",
'jobTitle' : jobTitle,
'company' : company,
'phone' : phone,
'email' : email,
'city' : city,
'country' : country,
'profile' : profile,
'imgUrl': imgUrl,
},
);
return result;
} on PlatformException catch (e) {
return null;
}
}
Swift:
let controller : FlutterViewController = window?.rootViewController as! FlutterViewController
let contactChannel = FlutterMethodChannel(name: "contacts", binaryMessenger: controller)
contactChannel.setMethodCallHandler({(call: FlutterMethodCall, result: #escaping FlutterResult) -> Void in
//ADD SYSTEM CONTACT
//
if (call.method == "addSystemContact") {
let arguments : Dictionary = call.arguments as! Dictionary<String,Any>
guard let firstName : String = arguments["firstName"] as? String else { return result(false); }
guard let lastName : String = arguments["lastName"] as? String else { return result(false); }
//create contact
let newContact = CNMutableContact()
newContact.contactType = CNContactType.person
newContact.givenName = firstName
newContact.familyName = lastName
if let jobTitle : String = arguments["jobTitle"] as? String {
newContact.jobTitle = jobTitle
}
if let company : String = arguments["company"] as? String {
newContact.organizationName = company
}
if let phone : String = arguments["phone"] as? String {
newContact.phoneNumbers = [CNLabeledValue(
label:CNLabelPhoneNumberMain,
value:CNPhoneNumber(stringValue:phone))]
}
if let email : NSString = arguments["email"] as? NSString {
newContact.emailAddresses = [CNLabeledValue(label:CNLabelWork, value:email)]
}
let address = CNMutablePostalAddress()
if let city : String = arguments["city"] as? String {
address.city = city
}
if let country : String = arguments["country"] as? String {
address.country = country
}
newContact.postalAddresses = [CNLabeledValue(label:CNLabelWork, value:address)]
if let profile : String = arguments["profile"] as? String {
newContact.socialProfiles = [CNLabeledValue(label:
CNSocialProfileServiceLinkedIn, value: CNSocialProfile.init(urlString: profile, username: nil, userIdentifier: nil, service: nil))]
}
// Offending code...
//
let urlString : String = (arguments["imgUrl"] as? String)!
let url = URL(string: urlString)
let data = try? Data(contentsOf: url!)
newContact.imageData = data
//
//
//presents nav controller with CNContactViewController
self.displayConactViewController(newContact: newContact, result: result)
}
})
You seem to have a mix of methodologies. Are you passing a single string as the argument, or a map? Unfortunately you don't show the Dart end.
Here are some working examples, adapted from a working plugin.
Single String
Dart end
static void ping(String pong) {
_channel.invokeMethod('ping', pong);
}
Swift end - call.arguments is a String
case "ping":
NSLog("ping")
if let pong = call.arguments as! String {
// do something
} else {
NSLog("ping - Invalid Args")
}
break
String as map member
Dart end
static void foo(String bar, String baz) {
_channel.invokeMethod('foo', <String, dynamic>{
'bar': bar,
'baz': baz,
});
}
Swift end - call.arguments is a Dictionary
case "foo":
NSLog("foo")
if let args = call.arguments as? Dictionary<String, Any>, let bar = args["bar"] as! String {
// do something with bar and/or args["baz"] as! String
} else {
NSLog("foo - Args is not a Dictionary")
}
break

Swift 4 Unwrapping Dictionary from Firebase

Here is the output of "print(dict)"...
["2018-10-17 11:19:51": {
firstname = Brooke;
id = 40vI7hApqkfX75SWsqIR6cdt7xV2;
lastname = Alvarez;
message = hshahyzhshbsbvash;
username = poiii;
}]
["2018-10-17 11:20:31": {
firstname = Trevor;
id = 40vI7hApqkfX75SWsqIR6cdt7xV2;
lastname = Bellai;
message = hey;
username = br9n;
}]
["2018-10-17 11:20:44": {
firstname = Amy;
id = 40vI7hApqkfX75SWsqIR6cdt7xV2;
lastname = Ikk;
message = hey;
username = nine9;
}]
My code...
Database.database().reference().child("recent-msgs").child(uid!).observe(.childAdded) { (snapshot: DataSnapshot) in
if let dict = snapshot.value as? [String: Any] {
print(dict)
// Store data in user.swift model
let firstnameData = dict[0]["firstname"] as! String
let idData = dict["id"] as! String
let lastnameData = dict["lastname"] as! String
let messageData = dict["message"] as! String
let usernameData = dict["username"] as! String
let rankData = dict["rank"] as! String
let propicrefData = dict["propicref"] as! String
let convoinfo = RecentConvo(firstnameString: firstnameData, idString: idData, lastnameString: lastnameData, messageString: messageData, usernameString: usernameData, rankString: rankData, propicrefString: propicrefData)
self.recentconvos.append(convoinfo)
print(self.recentconvos)
self.tableView.reloadData()
}
}
I'm trying to retrieve the dictionary within the first dictionary which is the value to the key which is the date associate with it. For example: 2018-10-17 11:19:51. However I cannot use this exact string to call it because I must do this without the knowledge of that string.
I tried this:
let firstnameData = dict[0]["firstname"] as! String
But it returns an error:
Cannot subscript a value of type '[String : Any]' with an index of type 'Int'
The error noted above is showing up because you were trying to access the element at a certain position (0) from the dictionary. Dictionaries are not ordered lists, and hence won't have a fixed order of elements to be accessed.
The logged dictionary doesn't really look like a dictionary. Assuming that it is a dictionary, and its keys are the date strings, you can use the following code snippet to parse the dictionary.
class RecentConversation {
var id: String?
var firstName: String?
var lastName: String?
var message: String?
var username: String?
var rank: String?
var propicref: String?
init?(dictionary: [String: Any]?) {
guard let dict = dictionary else {
// Return nil in case the dictionary passed on is nil
return nil
}
id = dict["id"] as? String
firstName = dict["firstname"] as? String
lastName = dict["lastname"] as? String
message = dict["message"] as? String
username = dict["username"] as? String
rank = dict["rank"] as? String
propicref = dict["propicref"] as? String
}
}
Usage:
let dateStrings = dict.keys.sorted {
// Sort in chronological order (based on the date string; if you need to sort based on the proper date,
// convert the date string to Date object and compare the same).
//
// Swap the line to $0 > $1 to sort the items reverse chronologically.
return $0 < $1
}
var conversations: [RecentConversation] = []
for date in dateStrings {
if let conversation = RecentConversation(dictionary: (dict[date] as? [String: Any])) {
conversations.append(conversation)
}
}
You were all very helpful, so I would like to start off by saying thank you. I went ahead and applied the method that lionserdar explained. (.allKeys)
// Fetch Recent Messages
func fetchRecentMsgs() {
// Direct to database child
Database.database().reference().child("recent-msgs").child(uid!).observe(.childAdded) { (snapshot: DataSnapshot) in
if let dict = snapshot.value as? NSDictionary {
print(dict)
print(dict.allKeys)
let keys = dict.allKeys
for key in keys {
print(key)
if let nestedDict = dict[key] as? [String: Any] {
print(nestedDict)
let firstnameData = nestedDict["firstname"] as! String
let idData = nestedDict["id"] as! String
let lastnameData = nestedDict["lastname"] as! String
let messageData = nestedDict["message"] as! String
let usernameData = nestedDict["username"] as! String
Worked for me so I hope this will help others too!

Swift Retrieving Data Firebase

I'm trying to retrieve string key which I have saved it in the object
func retrieveData() {
let refAll = Database.database().reference().child("Playground")
refAll.observe(.value) { (snapshot) in
if let snapshotValue = snapshot.value as? [String:Any] {
var playgroundSnapshot = snapshotValue
let playgroundKeys = Array(playgroundSnapshot.keys)
self.playgroundArray.removeAll()
for key in playgroundKeys {
guard
let value = playgroundSnapshot[key] as? [String : Any]
else {
continue
}
let title = value["title"] as! String
let city = value["city"] as! String
let location = value["location"] as! String
let price = value["price"] as! String
let playground = Playground(title: title, price: price, location: location, city: city, availblePlayground: true)
self.playgroundArray.append(playground)
}
self.tabeView.reloadData()
}
}
}
and in the playgroundArray there is key for each object
keySelected = playgroundArray[indexPath.row].key
but I don't know why keySelected is nil even tho playgroundArray has objects
playgroundArray does not contain "key" . You have to set the Playground Struct. Add var key:String? and also add this to init() func.
let playground = Playground(title: title, price: price, location: location, city: city, availblePlayground: true)
to
let playground = Playground(title: title, price: price, location: location, city: city, availblePlayground: true, key: key)
self.playgroundArray.append(playground)

Why am I getting Cannot convert value of type Bool to expected argument type String

Getting several "Cannot convert value of type Bool to expected argument type String" errors. The method for encoding expects a string but it is getting a Bool?
Here is the code. See the attached image for errors.
import Foundation
class Restaurant {
var name = ""
var item = ""
var location = ""
var image = ""
var isVisited = false
var phone = ""
var rating = ""
init(name: String, item: String, location: String, phone: String, image: String, isVisited: Bool) {
self.name = name
self.item = item
self.location = location
self.phone = phone
self.image = image
self.isVisited = isVisited
}
class func makeNewsItem(_ notificationDictionary: [String: AnyObject]) -> Restaurant? {
if let name = notificationDictionary["name"] as? String,
let phone = notificationDictionary["phone"] as? String,
let location = notificationDictionary["location"] as? String {
let date = Date()
let image = ""
let visited = false
let item = ""
let newsItem = Restaurant(name: name, item: item, location: location, phone: phone, image: image, isVisited: visited)
NotificationCenter.default.post(name: Notification.Name(rawValue: RestaurantTableViewController.RefreshNewsFeedNotification), object: self)
return newsItem
}
return nil
}
}
extension Restaurant: NSCoding {
struct CodingKeys {
static var Name = "name"
static var Item = "item"
static var Location = "location"
static var Image = "image"
static var IsVisited:Bool = false
static var Phone = "phone"
static var Rating = "rating"
}
convenience init?(coder aDecoder: NSCoder) {
if let name = aDecoder.decodeObject(forKey: CodingKeys.Name) as? String,
let location = aDecoder.decodeObject(forKey: CodingKeys.Location) as? Date,
let phone = aDecoder.decodeObject(forKey: CodingKeys.Phone) as? String {
let date = Date()
let image = aDecoder.decodeObject(forKey: CodingKeys.Image) as? String
let visited:Bool = aDecoder.decodeBool(forKey: CodingKeys.IsVisited) as? String
let item = aDecoder.decodeObject(forKey: CodingKeys.Item) as? String
self.init(name: name, item: item, location: location, phone: phone, image: image, isVisited: visited)
} else {
return nil
}
}
func encode(with aCoder: NSCoder) {
aCoder.encode(name, forKey: CodingKeys.Name)
aCoder.encode(location, forKey: CodingKeys.Location)
aCoder.encode(phone, forKey: CodingKeys.Phone)
aCoder.encode(item, forKey: CodingKeys.Item)
aCoder.encode(image, forKey: CodingKeys.Image)
aCoder.encode(isVisited, forKey: CodingKeys.IsVisited)
aCoder.encode(rating, forKey: CodingKeys.Rating)
}
}
You canĀ“t add a bool value to the forKey. This has to be a string value, so change it from:
aCoder.encode(isVisited, forKey: CodingKeys.IsVisited)
To:
aCoder.encode(isVisited, forKey: "IsVisited")
Same for:
let visited:Bool = aDecoder.decodeBool(forKey: CodingKeys.IsVisited) as? String
To:
let visited:Bool = aDecoder.decodeBool(forKey: "IsVisited") // note, no need for as? String here