How to keep location service request alert box stays appear - ios

I'm having strange problem with UITableViewController. I'm requesting location service authroization inside viewDidLoad method, and I could see alertbox appear. However, this alertbox stays appear for couple of seconds then disappears. Why is this happening?
override func viewDidLoad() {
let locationManager = CLLocationManager()
locationManager.requestAlwaysAuthorization()
}
I tried this code inside the UIViewController that loads this UITableViewController and encountered different problem. This time, only push notification service request alertbox is displayed and location service request is entirely ignored. I'm guessing it is because push notification service request is not from me but from ios, and my location service request got overwritten by the ios notification request. It's my assumption correct or is there any explination for this behaviour?

Your location manager is going out of existence because it is a local variable. Make it a persistent property:
let locationManager = CLLocationManager()
override func viewDidLoad() {
// ...
}
Also I'm not sure whether it's a good idea to do this in viewDidLoad. The view is not yet in the interface; in fact, there may be no interface yet.
But there are a lot of other things wrong with your code. You may already have authorization, or authorization may already have been denied, in which case there is no point requesting it. In general authorization requests are a much more elaborate business than your simple-minded code makes out.

Related

How to detect result from authorization dialog Core Motion?

I'm working on an app that utilizes Core Motion. During the initial onboarding of the app, it asks the user for permission, and we basically want to update the UI based on the response of that popup (Allowed/Denied). Where for notifications and location services this seems easy to do, it doesn't like a request permission API exists for Core Motion, instead it just triggers the popup when starting updating on a manager like we do now:
let motionManager = CMMotionActivityManager()
motionManager.startActivityUpdates(to: OperationQueue.main) {
// do stuff
}
Ideally I want to be able to detect a change in CMMotionActivityManager.authorizationStatus(), but so far haven't been able to come up with a working solution other than implement a timer that checks this property, which I don't feel is a particularly nice solution.
I tried making either authorizationStatus() or the manager as a whole an observable using Combine but that doesn't seem to trigger any updates.
If the user has denied permission, she cannot then grant permission without going to the Settings app. So you don't need to check on a timer. It should suffice to check at app launch and on notifications applicationWillEnterForeground and didBecomeActiveNotification.

Delegate methods for typing indicator on Twilio chat not being called

Working on a Swift chat application.
On one device, I join a channel with unique name "private:chat", and then I call channel.typing() on the channel (as is mention in docs).
On another device, I again create a client with a delegate set to self:
TwilioChatClient.chatClient(withToken: token, properties: nil, delegate: self) { (result, chatClient) in
self.client = chatClient
}
And then I implemented typingStartedOn delegate method to listen to typing on the channel.
func chatClient(_ client: TwilioChatClient, typingStartedOn channel: TCHChannel, member: TCHMember) {
print(">>>>>>>>>>>>> typing on \(channel.uniqueName) by \(member.identity)")
}
However, the delegate method does not get called, even though I confirmed that the channel.typing() gets called by the other account on the other device. The docs inline code in JS, but based on iOS chat demo app I came to conclusion that in Swift the delegate methods are the way to go.
Any idea what might be going on here?
EDIT: Some (haven't tested all) of the other delegate methods, such as synchronizationStatusUpdated, are getting called.
So after all, the issue was not in the iOS client, but in our implementation of the access token server.
As #rbeiter noted in his comment on GitHub issue, Twilio does not send typing indicator if the user identity is the same on both devices. I was setting the identity properly on both devices to keep it different, but our access token server ignored the identity parameter and used a hardcoded value. Thus from Twilio viewpoint, the same user was on both devices. Fixing the access token server fixed the problem.

avoid using location while app is in the background

I am using the google google places api or an iOS app and was wondering how to avoid having to ask for allowing "always on" location (NSLocationAlwaysUsageDescription). I do not think my app needs to access the location while in the background (is there a reason to?)
My google places api call is in a callback in viewDidLoad. Should I put it elsewhere in order to only ask for less intrusive permissions?
Thanks
There are 2 methods on CLLocationManager that request the location access. In order to request location access while the app is running you need to call requestWhenInUseAuthorization.
For example:
let locationManager = CLLocationManager()
locationManager.requestWhenInUseAuthorization()
I'm assuming you were following this guide where it show location access like this:
locationManager.requestAlwaysAuthorization()
That method does request the access to always use your device's location, even while the app is in the background.
Hope that helps!

When is the best time to call a webservice and parse the response in iOS?

I have a REST webservice call. Is it good to call the service in one of the methods of the app delegate, or do I need to call and parse the data in the respective view controller?
It really depends on what you need.
Don't forget that your app will sometimes go background - then what?
In my app, I'm connecting to my server in
func applicationDidBecomeActive(application: UIApplication)
and disconnecting in
func applicationWillResignActive(application: UIApplication)
But it really depends on you. You can also call your service from your controller (check that you are not blocking the UI...) in func viewDidLoad() if you need it ones, or func viewDidAppear() if you need it to be changed every time the view appears.
You can also use observer-reporter pattern, where you register some object to listen for events from notification centre, and when you got the data from you web service, you just post a notification about it. The result will be a function call that will manipulate you view as you wish...
Hope that helps a bit.

ios 8.0+: CLLocationManager does not ask for location permission properly

There is a question Stackoverflow question talking about this but I tried adding the string in Info.plist and asked for locManager.request..InUse.... When I launch the app, I can see the alert view poping up but before I do anything(allow or do not allow), it gets dismissed and there is a warning message printed out in debug console.
a basic code skeleton is like the following:
let locManager = CLLocationManager()
locManager.requestwheninuseauthorization()
// code to ask the map to track user location

Resources