How can access contact identifier from callKit - ios

I start callkit call with this code:
private func startCallKitCall(call: Call, completion: #escaping (Bool) -> ()){
let handle = CXHandle(type: .phoneNumber, value: call.handle)
let startCallAction = CXStartCallAction(call: call.uuid, handle: handle)
startCallAction.isVideo = call.hasVideo
startCallAction.contactIdentifier = call.uuid.uuidString
let transaction = CXTransaction(action: startCallAction)
requestCallKitTransaction(transaction, completionHandler: { success in
completion(success)
})
}
in contactIdentifier I set the user UUID, then in provider, update the caller remote name:
public func provider(_ provider: CXProvider, perform action: CXStartCallAction) {
let update = CXCallUpdate()
update.remoteHandle = action.handle
update.hasVideo = action.isVideo
update.localizedCallerName = "TestCallName"
self.provider!.reportCall(with: action.callUUID, updated: update)
action.fulfill()
}
I use "TestCallName" as localizedCallerName, now on recent call of my phone, I see a row named "TestCallName", If I click it, I call this method on appdelegate:
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: #escaping ([Any]?) -> Void) -> Bool {
if (userActivity.activityType == "INStartAudioCallIntent"){
guard let interaction = userActivity.interaction else {
return false
}
var personHandle: INPersonHandle?
if let startVideoCallIntent = interaction.intent as? INStartVideoCallIntent {
personHandle = startVideoCallIntent.contacts?[0].personHandle
} else if let startAudioCallIntent = interaction.intent as? INStartAudioCallIntent {
personHandle = startAudioCallIntent.contacts?[0].personHandle
print(startAudioCallIntent.contacts?[0])
}
print(personHandle!.value)
} else if (userActivity.activityType == "INStartVideoCallIntent"){
} else {
print("called userActivity: \(userActivity.activityType), but not yet supported.")
}
return true
}
but the output I obtain is:
Optional(<INPerson: 0x1742a5280> {
contactIdentifier = "<null>";
customIdentifier = "<null>";
displayName = "<null>";
image = "<null>";
nameComponents = "<null>";
personHandle = "<INPersonHandle: 0x17402f0c0> {\n label = \"<null>\";\n type = PhoneNumber;\n value = \"TestCallName\";\n}";
relationship = "<null>";
siriMatches = "<null>";
})
Optional("TestCallName")
i see only the name "TestCallName", but contact identifier is "null", how can I fix this? I see there is also a field name "customIdentifier", can I use this?
thanks!

The contactIdentifier is the ID of the contact in contacts app. The one you set into startCallAction.contactIdentifier means nothing. To call a user you need to use either phone or email or at least name.

Related

at UNNotification I get different userInfo values than set values in UNNotificationRequest Swift

I have a function that sets the UNNotificationRequest with some parameter passed in, I than put those parameters in userInfoso I can get them back and use them when opening the local notification. The print of it shows I'm assigning correct values passed in. The problem is that I get some other values when I print userInfo in applicationDidBecomeActive. I cannot see why this is happening, can you spot what I'm doing wrong here?
This is the functionthat sets the notification and the output prints:
func setRouteNotification(_ date: Date, onWeekdaysForNotify weekdays:[Int], snoozeEnabled:Bool, onSnooze: Bool, soundName: String, routeName: String, index: Int) {
configureCategory()
// Notification content
let routeCheckNotificationContent = UNMutableNotificationContent()
let datesForNotification = correctDate(date, onWeekdaysForNotify: weekdays)
// routeCheckNotificationContent.title = "Hello!! Are you ready to cycle?"
routeCheckNotificationContent.title = "Ciao!! Pronto ad andare?"
// routeCheckNotificationContent.body = "Check route for alerts?"
routeCheckNotificationContent.body = "Controlliamo se ci sono ostacoli lungo il tuo percorso?"
routeCheckNotificationContent.categoryIdentifier = Id.notificationCategory
routeCheckNotificationContent.sound = UNNotificationSound.init(named: soundName + ".mp3")
print(" set notification is: date: \(date), onWeekdaysForNotify : \(weekdays), snoozeEnabled : \(snoozeEnabled), onSnooze: \(onSnooze), soundName: \(soundName), routeName: \(routeName), index: \(index)")
let repeating: Bool = !weekdays.isEmpty
// routeCheckNotificationContent.userInfo = ["snooze" : snoozeEnabled, "index": index, "soundName": soundName, "routeName": routeName, "repeating" : repeating]
routeCheckNotificationContent.userInfo = ["posticipa" : snoozeEnabled, "indice": index, "Suono": soundName, "Titolo percorso" : String(describing: routeName), "repeating" : repeating]
print("routeCheckNotificationContent.userInfo at setting notification is: \(routeCheckNotificationContent.userInfo)")
//repeat weekly if repeat weekdays are selected
//no repeat with snooze notification
if !weekdays.isEmpty && !onSnooze{
}
syncAlarmModel()
var counter = 0
for d in datesForNotification {
if onSnooze {
alarmModel.alarms[index].date = Scheduler.correctSecondComponent(date: alarmModel.alarms[index].date)
}
else {
alarmModel.alarms[index].date = d
}
//trigger
let components = Calendar.current.dateComponents([.weekday,.hour,.minute,.second,], from: d)
let trigger = UNCalendarNotificationTrigger(dateMatching: components, repeats: true)
// Notification Request
let uuid = UUID().uuidString
let routeNotificationRequest = UNNotificationRequest(identifier: "Route Notification Request\(uuid)", content: routeCheckNotificationContent, trigger: trigger)
// Add request
UNUserNotificationCenter.current().add(routeNotificationRequest) { (Error) in
if Error != nil {
// print("something went wrong with adding notification")
}
}
// print("added request\(uuid)")
scheduledNotifications.append(counter)
// print("scheduled notifications are \(scheduledNotifications.count)")
counter = ( counter + 1 )
}
// print(alarmModel.alarms)
// print("dates for notification are: \(datesForNotification)")
}
output prints:
routeCheckNotificationContent.userInfo at setting notification is: [AnyHashable("repeating"): true, AnyHashable("indice"): 0, AnyHashable("Titolo percorso"): "aaa", AnyHashable("Suono"): "Zen", AnyHashable("posticipa"): false]
but in AppDelegate:
func applicationDidBecomeActive(_ application: UIApplication) {
let center = UNUserNotificationCenter.current()
center.getDeliveredNotifications { (receivedNotifications) in
for notification in receivedNotifications {
let content = notification.request.content
print(" Body \(content.body)")
print(" Title \(content.title)")
let rotta = content.userInfo["Titolo percorso"]
print("rotta is: \(rotta)")
print(content.userInfo as NSDictionary)
}
}
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
// audioPlayer?.play()
// alarmScheduler.checkNotification()
}
output prints:
Body Controlliamo se ci sono ostacoli lungo il tuo percorso?
Title Ciao!! Pronto ad andare?
rotta is: Optional(Scegli un percorso)
{
Suono = Suono;
"Titolo percorso" = "Scegli un percorso";
indice = 0;
posticipa = 0;
repeating = 0;
}
Body and Title are correct, but the rest is not..where am I missing connection for those wrong values?
Many thanks as usual
Updates:
I made some new prints from every place that I set those values an they are all correct, but in AppDelegatei get the values from this line:
addEditController.segueInfo = SegueInfo(curCellIndex: alarmModel.count, isEditMode: false, label: "Percorso", mediaLabel: "Suono", mediaID: "", routeLabel: "Scegli un percorso", repeatWeekdays: [], enabled: false, snoozeEnabled: false)
SegueInfo is just a struct that holds values until they are replaced with actual chosen values, and I do update them..
struct SegueInfo {
var curCellIndex: Int
var isEditMode: Bool
var label: String
var mediaLabel: String
var mediaID: String
var routeLabel: String
// var routeId: String
var repeatWeekdays: [Int]
var enabled: Bool
var snoozeEnabled: Bool
}
I finally found where the problem was. I wasn't getting the values in the response. So I removed code from applicationDidBecomeActiveand got the values from didReceive response in the appropriate Action and it's now working as expected.
I hope this will help others.
The entire function is:
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: #escaping () -> Void) {
switch response.actionIdentifier
{
case Id.checkActionIdentifier:
print("Check tapped")
let content = response.notification.request.content
let rotta = content.userInfo["Titolo percorso"] as! String
RoutesArray.routeName = rotta
print("rotta is: \(rotta)")
print(" Body \(content.body)")
print(" Title \(content.title)")
NewMapViewController.checkCounter = 1
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewController : UINavigationController = storyboard.instantiateViewController(withIdentifier: "MainNavigationController") as! UINavigationController
self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()
let vc = initialViewController.viewControllers[0]
vc.performSegue(withIdentifier: "alertToMapSegue", sender: nil)
case Id.waitActionIdentifier:
print("Wait tapped")
default:
print("Other Action")
}
completionHandler()
}

Taking so much time when i insert 10k data in coredata ?

In my project concept i need a insert 10k data when user open the application. I integrate core data for storing data but its take 1 to 5 minutes.
Here is my code ?
func inserChatMessage(_ message: String, chatId: String, onCompletion completionHandler:((_ message: ChatMessage) -> Void)?) {
var objMessage: ChatMessage? = nil
if let obj = ChatMessage.createEntity() {
objMessage = obj
}
objMessage?.messageId = ""
objMessage?.message = message
objMessage?.chatId = chatId
objMessage?.senderId = AIUser.current.userId
objMessage?.createAt = Date()
objMessage?.updateAt = Date()
let cManager = CoreDataManager.sharedManager
cManager.saveContext()
if let completionHandler = completionHandler, let objMessage = objMessage {
completionHandler(objMessage)
}
}
Coredata is not a threadsafe. And as per your requirement you need to save large amount of data on app launch. So If you will save those data using main thread, your app will get hanged. So Instead on saving large amount of data on main thread you can save those data on background thread. Coredata is supporting multi threading concept by providing parent child context concept.
I have done same in one of my project and its working fine. Here i have attached code.
func savePersonalMessagesOnBackGroundThread(arrMessages:NSArray,responseData:#escaping () -> Void)
{
print(arrMessages)
let temporaryChatContext = NSManagedObjectContext(concurrencyType: NSManagedObjectContextConcurrencyType.privateQueueConcurrencyType)
temporaryChatContext.parent = self.managedObjectContext
temporaryChatContext.perform({() -> Void in
for i in 0..<arrMessages.count
{
let msgDic = arrMessages[i] as! NSDictionary
_ = self.saveMessageInLocalDB(dictMessage: msgDic, managedObjectContext: temporaryChatContext, onBackground: true)
if i == arrMessages.count - 1 {
do {
try temporaryChatContext.save()
runOnMainThreadWithoutDeadlock {
DLog(message: "Thred \(Thread.isMainThread)")
if(self.managedObjectContext.hasChanges)
{
self.saveContext()
responseData()
}
}
}
catch {
print(error)
}
}
}
})
}
func saveMessageInLocalDB(dictMessage:NSDictionary, managedObjectContext:NSManagedObjectContext,onBackground:Bool) -> Chat
{
var chatObj : Chat! = Chat()
var receiveId: Int32!
var flag:Bool = false
print(dictMessage)
// let predicate = NSPredicate(format:"uniqueId == %# and senderId = %d and receiverId = %d","\(dictMessage.value(forKey:keyuniqueId)!)",Int32(dictMessage.value(forKey:keysenderId) as! Int64),Int32(dictMessage.value(forKey:keyreceiverId) as! Int64))
let predicate = NSPredicate(format:"uniqueId == %#","\(dictMessage.value(forKey:keyuniqueId)!)")
let objContext = managedObjectContext
let fetchRequest = NSFetchRequest<Chat>(entityName: ENTITY_CHAT)
let disentity: NSEntityDescription = NSEntityDescription.entity(forEntityName: ENTITY_CHAT, in: objContext)!
fetchRequest.predicate = predicate
fetchRequest.entity = disentity
do{
let results = try managedObjectContext.fetch(fetchRequest as! NSFetchRequest<NSFetchRequestResult>) as! [Chat]
if(results.count > 0)
{
chatObj = results[0]
chatObj.messageId = Int32(dictMessage.value(forKey:keymessageId) as! Int64)
chatObj.dateOnly = dictMessage.value(forKey:keydateOnly) as! String?
}
else{
//receiveId = Int32(dictMessage.value(forKey:keyreceiverId) as! Int64)
//self.createNewChatObject(dictMessage: dictMessage, receiverId: receiveId, managedObjectContext: managedObjectContext)
chatObj = NSEntityDescription.insertNewObject(forEntityName:ENTITY_CHAT,into: managedObjectContext) as? Chat
if dictMessage[keymessageId] != nil {
chatObj.messageId = dictMessage.value(forKey:keymessageId) as! Int32
}
if(chatObj.message?.length != 0)
{
chatObj.message = dictMessage.value(forKey:keychatMessage) as? String
}
chatObj.messageType = Int32(dictMessage.value(forKey:keymessageType) as! Int64)
chatObj.senderId = Int32(dictMessage.value(forKey:keysenderId) as! Int64)
if(chatObj.senderId != Int32((APP_DELEGATE.loggedInUser?.id!)!))
{
let contactObj = self.getContactByContactId(contactId: Int32(dictMessage.value(forKey:keysenderId) as! Int64))
if(contactObj == nil)
{
_ = self.saveUnknownUserASContact(msgDict: dictMessage as! Dictionary<String, Any>)
}
}
chatObj.receiverId = Int32(dictMessage.value(forKey:keyreceiverId) as! Int64)
chatObj.uniqueId = dictMessage.value(forKey:keyuniqueId) as? String
chatObj.mediaName = dictMessage.value(forKey:keymediaName) as? String
print(NSDate())
if dictMessage[keycreatedDate] != nil {
let utcDate : NSDate = DateFormater.getUTCDateFromUTCString(givenDate: dictMessage.value(forKey:keycreatedDate) as! String)
chatObj.createdDate = utcDate
chatObj.updatedDate = utcDate
}
else
{
chatObj.createdDate = NSDate()
chatObj.updatedDate = NSDate()
}
if(chatObj.senderId == Int32((APP_DELEGATE.loggedInUser?.id)!))
{
chatObj.chatUser = chatObj.receiverId
}
else
{
chatObj.chatUser = chatObj.senderId
}
if dictMessage[keystatus] != nil {
chatObj.status = Bool((dictMessage.value(forKey:keystatus) as! Int64) as NSNumber)
}
switch Int(chatObj.messageType)
{
case MSG_TYPE.MSG_Text.rawValue:
chatObj.cellType = (chatObj.senderId != Int32((APP_DELEGATE.loggedInUser?.id!)!) ? Int32(CELL_TYPE.CELL_TEXT_RECEIVER.rawValue) : Int32(CELL_TYPE.CELL_TEXT_SENDER.rawValue))
case MSG_TYPE.MSG_Image.rawValue:
chatObj.cellType = (chatObj.senderId != Int32((APP_DELEGATE.loggedInUser?.id!)!) ? Int32(CELL_TYPE.CELL_IMAGE_RECEIVER.rawValue) : Int32(CELL_TYPE.CELL_IMAGE_SENDER.rawValue))
self.saveMedia(chatObj: chatObj)
default :
// chatObj.cellType = Int32(CELL_TYPE.CELL_LOAD_MORE.rawValue)
break
}
}
// deviceMake = 1;
if(!onBackground)
{
self.saveContext()
}
}
catch
{
}
return chatObj
}
Using the basic example below I can insert 10k records very quickly. The main thing that has changed here compared to your code is that I loop through and create the entities and then call save() at the very end. So you are performing one write call to the db instead of 10k. You are writing more information in that one call but it is still much quicker.
import UIKit
import CoreData
class ViewController: UIViewController {
lazy var sharedContext: NSManagedObjectContext? = {
return (UIApplication.shared.delegate as? AppDelegate)?.persistentContainer.viewContext
}()
override func viewDidLoad() {
super.viewDidLoad()
if let messages = getMessages(), messages.count > 0 {
printMessages(messages: messages)
} else {
loadChatMessages()
printMessages(messages: getMessages())
}
}
private func printMessages(messages: [Message]?) {
guard let messages = messages else { return }
for message in messages {
print(message.message)
}
}
private func getMessages() -> [Message]? {
let request = NSFetchRequest<Message>(entityName: "Message")
let messages = try? self.sharedContext?.fetch(request)
return messages ?? nil
}
private func loadChatMessages() {
var counter = 1
while counter <= 10000 {
let message = Message(entity: Message.entity(), insertInto: self.sharedContext)
message.message = "This is message number \(counter)"
message.read = false
message.timestamp = Date()
counter = counter + 1
}
do {
try self.sharedContext?.save()
} catch {
print(error)
}
}
}
As mentioned in my comment above, you can improve this further by doing it in the background (see Twinkle's answer for an example of how to switch to a background thread), you can also provide a pre-filled (pre-seeded) core data database that already contains the 10k records with your app. so it doesn't need to load this on initial load.
To do this you would fill the db locally on your dev machine and then copy it to the project bundle. On initial load you can check to see if your db filename exists in the documents folder or not. If it doesn't copy it over from the bundle and then use that DB for core data.

How to extract street, city, etc. from GMSPlace Address Components

I'm using Google Places API for iOS and can successfully retrieve nearby places and present the address as a string. What I'm trying to do is extract address components such as city to store in a database.
The documentation indicates a GMSPlace has an addressComponents property (an array), but I can't seem to figure how to use this property.
The code below provides sets the entire address to the text of a label, but I can't get beyond that:
Edit ---- added code that shows how I'm trying to access Address Components
venueLabel.isHidden = false
venueLabel.text = selectedPlace?.name
addressLabel.isHidden = false
addressLabel.text = selectedPlace?.formattedAddress
let addressComponents = selectedPlace?.addressComponents
for component in addressComponents! {
let city = component["city"] //Error Type GMSPaceAddressComponent has no subscript members
print(city)
}
A safe Swift 4.2 solution:
let name = place.addressComponents?.first(where: { $0.type == "city" })?.name
selectedPlace.addressComponents is a array of GMSAddressComponent which have 2 properties type and name.
In you case it will be like
for component in addressComponents! {
if component.type == "city" {
print(component.name)
}
}
GMSAddressComponent is a class not a dictionary or array that's you are getting this error.
Additional component types can be referred from the link.
Swift 4
var keys = [String]()
place.addressComponents?.forEach{keys.append($0.type)}
var values = [String]()
place.addressComponents?.forEach({ (component) in
keys.forEach{ component.type == $0 ? values.append(component.name): nil}
})
print(values)
For the latest Google Places API (July 2019) this is what could help you.
Actually, Google now putting a couple of types for each element. So the "type" is now deprecated and "types" is new filed.
You can do something like this:
for addressComponent in (self.mySelectedPlace?.addressComponents)! {
for type in (addressComponent.types){
switch(type){
case "country":
var country = addressComponent.name
case "postal_code":
var postCode = addressComponent.name
default:
break
}
}
}
It's working in Swift 5 and iOS 13.3
1. Create a function for showing GMSAutocompleteViewController
func googlePlacesSearchVC() {
let acController = GMSAutocompleteViewController()
acController.delegate = self
// Specify the place data types to return.
let fields: GMSPlaceField = GMSPlaceField(rawValue: UInt(GMSPlaceField.name.rawValue) |
UInt(GMSPlaceField.formattedAddress.rawValue) |
UInt(GMSPlaceField.addressComponents.rawValue))!
acController.placeFields = fields
// Specify a filter.
let filter = GMSAutocompleteFilter()
filter.country = "IN"
acController.autocompleteFilter = filter
acController.secondaryTextColor = .darkGray
acController.primaryTextColor = .lightGray
acController.primaryTextHighlightColor = .black
acController.tableCellBackgroundColor = .whiteThree
acController.tableCellSeparatorColor = .lightGray
// Display the autocomplete view controller.
present(acController, animated: true) {
let views = acController.view.subviews
let subviewsOfSubview = views.first!.subviews
let subOfNavTransitionView = subviewsOfSubview[1].subviews
let subOfContentView = subOfNavTransitionView[1].subviews
let searchBar = subOfContentView[0] as! UISearchBar
searchBar.searchTextField.placeholder = "places_picker_hint_add_address".localized
searchBar.searchBarStyle = .minimal
searchBar.searchTextField.font = UIFont.screenTitle16Pt
searchBar.searchTextField.backgroundColor = UIColor.white
searchBar.searchTextField.leftView?.tintColor = .darkGray
searchBar.delegate?.searchBar?(searchBar, textDidChange: "")
}
}
2. Call that function where ever you need, for ex:-
override func viewDidLoad() {
super.viewDidLoad()
googlePlacesSearchVC()
}
3. Call GMSAutoCompleteViewControllerDelegates Methods.
Here will get all details like Street, City, etc. from GMSPlace Address Components
extension ViewController {
// Handle the user's selection.
func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) {
// Show HouseAndFlat
if place.name?.description != nil {
yourTxtField.text = place.name?.description ?? ""
}
// Show latitude
if place.coordinate.latitude.description.count != 0 {
var latitude = place.coordinate.latitude
}
// Show longitude
if place.coordinate.longitude.description.count != 0 {
selectedLongitude = place.coordinate.longitude
}
// Show AddressComponents
if place.addressComponents != nil {
for addressComponent in (place.addressComponents)! {
for type in (addressComponent.types){
switch(type){
case "sublocality_level_2":
yourTxtField.text = addressComponent.name
case "sublocality_level_1":
yourTxtField.text = addressComponent.name
case "administrative_area_level_2":
yourTxtField.text = addressComponent.name
case "administrative_area_level_1":
yourTxtField.text = addressComponent.name
case "country":
yourTxtField.text = addressComponent.name
case "postal_code":
yourTxtField.text = addressComponent.name
default:
break
}
}
}
}
dismiss(animated: true, completion: nil)
}
func viewController(_ viewController: GMSAutocompleteViewController, didFailAutocompleteWithError error: Error) {
// TODO: handle the error.
print("Error: ", error.localizedDescription)
}
// User canceled the operation.
func wasCancelled(_ viewController: GMSAutocompleteViewController) {
dismiss(animated: true, completion: nil)
}
}
I have found an answer from this link
I am using this extension:
extension Array where Element == GMSAddressComponent {
func valueFor(placeType: String, shortName: Bool = false) -> String? {
// bug in the google or apple framework. This cast must stay.
// Withou it crashing.
guard let array = self as? NSArray else { return nil }
let result = array
.compactMap { $0 as? GMSAddressComponent }
.first(where: {
$0.types
.first(where: { $0 == placeType }) == placeType
})
return shortName ? result?.shortName : result?.name
}
}
Usage:
let place: GMSPlace
// ...
place.addressComponents?.valueFor(placeType: kGMSPlaceTypeRoute, shortName: true)
I will extend #Ramis answer, in any case you want to check for multiple types if one of them exist, e.x
addressComponents?.valueFor(placeTypes: kGMSPlaceTypePostalTown, kGMSPlaceTypeLocality)
implementation
extension Array where Element == GMSAddressComponent {
func valueFor(placeTypes: String..., shortName: Bool = false) -> String? {
let array = self as NSArray
let result = array
.compactMap { $0 as? GMSAddressComponent }
.first(where: {
placeTypes.contains($0.types.first(where: { placeTypes.contains($0) }) ?? "")
})
return shortName ? result?.shortName : result?.name
}
}
I could not find this anywhere for Objective-C, for those who follow behind me:
for (int i = 0; i < [place.addressComponents count]; i++)
{
NSLog(#"name %# = type %#", place.addressComponents[i].name, place.addressComponents[i].type);
}
for ( GMSAddressComponent * component in place.addressComponents) {
if ( [component.type isEqual: #"locality"] )
{
NSLog(#"Current city %# ", component.name);
}
}
var keys = [String]()
place.addressComponents?.forEach{keys.append($0.type)}
var values = [String]()
place.addressComponents?.forEach({ (component) in
keys.forEach{ component.type == $0 ? values.append(component.name): nil}
print("All component type \(component.type)")
if component.type == "locality"{
print("This is city name \(component.name)")
}
})
print(values)
I found this, I hope I help you
let arrays : NSArray = place.addressComponents! as NSArray
for i in 0..<arrays.count {
let dics : GMSAddressComponent = arrays[i] as! GMSAddressComponent
let str : NSString = dics.type as NSString
if (str == "country") {
print("Country: \(dics.name)")
self.pais = dics.name
}
else if (str == "administrative_area_level_2") {
print("City: \(dics.name)")
self.ciudad = dics.name
}
print("City: (String(describing: place.addressComponents?.first(where: {$0.type == "locality"})?.name))")
print("Postal Code: (String(describing: place.addressComponents?.first(where: {$0.type == "postal_code"})?.name))")
print("State: (String(describing: place.addressComponents?.first(where: {$0.type == "administrative_area_level_1"})?.name))"
Use this description for work like this.
https://developers.google.com/maps/documentation/places/ios-sdk/place-data-fields

Mapping two kind of responses with Alamofire and AlamofireObjectMapper (iOS - swift)

So, I'm making an iOS app the search a movie on imdb database (using omdb api) and the user could save the favorites.
To search by imdbTitle the request url is "https://www.omdbapi.com/?s=(imdbTitle)&type=movie", if imdbTitle = "arq", for example, the response is:
{
Response = True;
Search = (
{
Poster = "https://images-na.ssl-images-amazon.com/images/M/MV5BMjAxODQ2MzkyMV5BMl5BanBnXkFtZTgwNjU3MTE5OTE#._V1_SX300.jpg";
Title = ARQ;
Type = movie;
Year = 2016;
imdbID = tt5640450;
},
{
Poster = "N/A";
Title = Arq;
Type = movie;
Year = 2011;
imdbID = tt2141601;
},
{
Poster = "N/A";
Title = "A.R.Q.";
Type = movie;
Year = 2015;
imdbID = tt3829612;
}
);
totalResults = 3;
}
But for save the movie I have to search by imdbID, url: "https://www.omdbapi.com/?i=(imdbID)", if imdbID is the one from the movie searched, imdbID = tt3829612, and the response is:
{
Actors = "Robbie Amell, Rachael Taylor, Shaun Benson, Gray Powell";
Awards = "N/A";
BoxOffice = "N/A";
Country = "USA, Canada";
DVD = "16 Sep 2016";
Director = "Tony Elliott";
Genre = "Sci-Fi, Thriller";
Language = English;
Metascore = "N/A";
Plot = "Trapped in a lab and stuck in a time loop, a disoriented couple fends off masked raiders while harboring a new energy source that could save humanity.";
Poster = "https://images-na.ssl-images-amazon.com/images/M/MV5BMjAxODQ2MzkyMV5BMl5BanBnXkFtZTgwNjU3MTE5OTE#._V1_SX300.jpg";
Production = Netflix;
Rated = "N/A";
Ratings = (
{
Source = "Internet Movie Database";
Value = "6.4/10";
},
{
Source = "Rotten Tomatoes";
Value = "60%";
}
);
Released = "16 Sep 2016";
Response = True;
Runtime = "88 min";
Title = ARQ;
Type = movie;
Website = "N/A";
Writer = "Tony Elliott";
Year = 2016;
imdbID = tt5640450;
imdbRating = "6.4";
imdbVotes = "17,481";
}
So, my Movie class has to have the following attributes: poster, title, runtime, genre, director, actors, plot, released, imdbID and imdbRating (to show this on my UI View, except the imdbID, of course)
I'm a beginner and I'm too confused by all those things (never worked with API data before).
Anyways, after a lot of search I found that there is a way to get this response as an Array using AlamofireObjectMapper.
I already have my request functions:
func searchMoviesOnJson(imdbTitle: String, completionHandler: #escaping (Dictionary<String, Any>?) -> ()) {
let urlByName: String = "https://www.omdbapi.com/?s=\(imdbTitle)&type=movie"
//returns a list of movies that contains the title searched
//------------------------------------------------
Alamofire.request(urlByName).responseJSON {
response in
switch response.result {
case .success(let value):
let moviesJSON = value
completionHandler(moviesJSON as? Dictionary<String, Any>)
case .failure(_):
completionHandler(nil)
}
}
//------------------------------------------------
}
func getMovieFromJson(imdbID: String, completionHandler: #escaping (Dictionary<String, String>) -> ()) {
let urlById: String = "https://www.omdbapi.com/?i=\(imdbID)"
//returns a single movie information
//------------------------------------------------
Alamofire.request(urlById).responseJSON {
response in
if let moviesJSON = response.result.value {
completionHandler(moviesJSON as! Dictionary<String, String>)
}
}
//------------------------------------------------
}
So, could anyone explain to me (like you'll explain to a child) how can I do this mapping? I created a Movie.swift and in this file I'll put my class Movie and class MovieDAO.
How can I implement this two classes using AlamofireObjectMapper and who I have to change in my request methods?
Update:
class Movie {
let poster: String?
let title: String?
let runtime: String?
let genre: String?
let director: String?
let actors: String?
let plot: String?
let released: String?
let imdbID: String?
let imdbRating: String?
init(poster: String?, title: String?, runtime: String?, genre: String?, director: String?, actors: String?, plot: String?, released: String?, imdbID: String?, imdbRating: String?) {
//checking if is nil
if let isPoster = poster {
self.poster = isPoster
} else {
self.poster = nil
}
if let isTitle = title {
self.title = isTitle
} else {
self.title = nil
}
if let isGenre = genre {
self.genre = isGenre
} else {
self.genre = nil
}
if let isRuntime = runtime {
self.runtime = isRuntime
} else {
self.runtime = nil
}
if let isDirector = director {
self.director = isDirector
} else {
self.director = nil
}
if let isActors = actors {
self.actors = isActors
} else {
self.actors = nil
}
if let isPlot = plot {
self.plot = isPlot
} else {
self.plot = nil
}
if let isReleased = released {
self.released = isReleased
} else {
self.released = nil
}
if let isImdbID = imdbID {
self.imdbID = isImdbID
} else {
self.imdbID = nil
}
if let isImdbRating = imdbRating {
self.imdbRating = isImdbRating
} else {
self.imdbRating = nil
}
}
}
UPDATE 2
I declare the variable movieDetail as you said to do, but the xcode showed an error and suggesting that I could add the parameters, so I did and its not solved.
You will have to parse the JSON received:
For e.g. if json received is:
{
Response = True;
Search = (
{
Poster = "https://images-na.ssl-images-amazon.com/images/M/MV5BMjAxODQ2MzkyMV5BMl5BanBnXkFtZTgwNjU3MTE5OTE#._V1_SX300.jpg";
Title = ARQ;
Type = movie;
Year = 2016;
imdbID = tt5640450;
},
{
Poster = "N/A";
Title = Arq;
Type = movie;
Year = 2011;
imdbID = tt2141601;
},
{
Poster = "N/A";
Title = "A.R.Q.";
Type = movie;
Year = 2015;
imdbID = tt3829612;
}
);
totalResults = 3;
}
As you have mentioned, if you have stored the json response in NSDictionary
You can access the values of Search results as
var moviesArray = [Movie]() //array of your Movie class
var imdbIdArray = [String]()
if let searchResults = moviesJSON["Search"] as? NSArray {
for searchResult in searchResults {
let movieResult = searchResult as! Dictionary<String,String>
let movieDetail = Movie()
movieDetail.title = movieResult["Title"]
movieDetail.type = movieResult["Type"]
//similarly you can do it for other params as well
moviesArray.append(movieDetail)
let imdbId = movieResult["imdbId"]
imdbIdArray.append(imdbId)
}
}
class Movie: NSObject {
var title = String()
var type = String()
}
Now moviesArray consist of list of movies returned in the result set.

Fetch email address from CNContactPicker in Swift 2.0 Contact Framework iOS

I'm trying to fetch picked email address from new contact framework iOS 9 but I cannot find right solution. Phone number and name working fine.
func contactPicker(picker: CNContactPickerViewController, didSelectContactProperty contactProperty: CNContactProperty) {
let phoneNumberString: String
let emailString: String
let contact = contactProperty.contact
let contactName = CNContactFormatter.stringFromContact(contact, style: .FullName) ?? ""
let propertyName = CNContact.localizedStringForKey(contactProperty.key)
let message = "Picked \(propertyName) for \(contactName)"
if(propertyName == "Phone") {
let phoneNumber = contactProperty.value as! CNPhoneNumber
//print(contact.givenName)
phoneNumberString = phoneNumber.stringValue ?? ""
inputPhone.text = phoneNumberString.regexPatern("[0-9]+").joinWithSeparator(" ")
}
if(propertyName == "Email") {
I need email address //print(email contact)
}
inputName.text = contact.givenName
inputSurname.text = contact.familyName
// Display only a person's phone, email, and birthdate
let displayedItems = [CNContactPhoneNumbersKey, CNContactEmailAddressesKey, CNContactBirthdayKey]
picker.displayedPropertyKeys = displayedItems
}
You can get mail id like below code : Swift 2.0
#available(iOS 9.0, *)
func contactPicker(picker: CNContactPickerViewController,
didSelectContact contact: CNContact)
{
if contact.isKeyAvailable(CNContactPhoneNumbersKey)
{
let con = contact.mutableCopy() as! CNMutableContact
//Email
print(con.emailAddresses[0].valueForKey("labelValuePair")?.valueForKey("value"))
//MobileNo
print(con.phoneNumbers[0].valueForKey("labelValuePair")?.valueForKey("value")?.valueForKey("stringValue"))
}
else
{
print("No phone numbers are available")
}
}
Below is working fine in Swift 3.0
#available(iOS 9.0, *)
func contactPicker(_ picker: CNContactPickerViewController, didSelect contacts: [CNContact]) {
if contacts[0].isKeyAvailable(CNContactPhoneNumbersKey)
{
let con = contacts[0].mutableCopy() as! CNMutableContact
let firstName = con.value(forKey: "givenName") as! String
let lastName = con.value(forKey: "familyName") as! String
let valPairs = (con.phoneNumbers[0].value(forKey: "labelValuePair") as AnyObject)
let value = valPairs.value(forKey: "value") as AnyObject
//Mobile No
print(value.value(forKey: "stringValue"))
//Mail
let mailPair = (con.emailAddresses[0].value(forKey: "labelValuePair") as AnyObject)
print(mailPair.value(forKey: "value"))
}
else
{
print("No phone numbers are available")
}
}

Resources