DCDC DCDC - 11 months ago 45
Swift Question

How to request for permission before the actual object is initialized?

How can I request for permissions to location, camera, bluetooth etc. without initializing proper object instances?

I want to ask for permissions/authorizations during app's onboarding, then I'd like to initialize

etc afterwards.

I tried Googling it, but failed to find anything relevant. Is it actually possible?

Answer Source

For permissions like location access ,we need a manager instance to show the location access prompt ( refer :http://nshipster.com/core-location-in-ios-8/). These instances can be used only to request access (if you want them to only request for access) and in the future if you want to access the resource or data we can again use these manager instances.

For Example:

CLLocation manager should be used to access user's location, so in first screen if you just want to ask location permission then can use following code

 CLLocationManager().requestAlwaysAuthorization() //Requesting always permission

And if you want to access user's location in some other screen you can access it as:

  locationManager.startUpdatingLocation() // use delegate methods to handle the values.

So these kind of managers can be initialised only for requesting permission, then can be reinitialised when required.

Here is an article about best way to ask location permissions (same can be applied for other types of permission requests) https://techcrunch.com/2014/04/04/the-right-way-to-ask-users-for-ios-permissions/