theDC
theDC

Reputation: 6484

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 CLLocationManager etc afterwards.

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

Upvotes: 1

Views: 734

Answers (2)

Shailesh Chandavar
Shailesh Chandavar

Reputation: 1614

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/

Upvotes: 2

neprocker
neprocker

Reputation: 170

For each action do the similar approach

 let status = PHPhotoLibrary.authorizationStatus()
switch status {
case .authorized: 

case .denied, .restricted :

//handle denied status
case .notDetermined:
    // ask for permissions
    PHPhotoLibrary.requestAuthorization() { (status) -> Void in
        switch status {
        case .authorized:

        // as above
        case .denied, .restricted: 

        // as above
        case .notDetermined: break
            // won't happen but still
        }
    }
}

Upvotes: 0

Related Questions