1

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?

Brian
  • 14,610
  • 7
  • 35
  • 43
theDC
  • 6,364
  • 10
  • 56
  • 98
  • Why do you don't want do initialise the managers?. Could please add more detail. – Shailesh Chandavar Oct 07 '16 at 17:33
  • I want to initialize them in different viewController – theDC Oct 07 '16 at 17:34
  • But you need managers to ask user access permission. without the manager instance you can't ask permissions. – Shailesh Chandavar Oct 07 '16 at 17:35
  • That is my question, do i need them? If yes, can I initialize it only for requesting permission, then reinitialize it when required? – theDC Oct 07 '16 at 17:38
  • Use a Singleton pattern with Class/Type methods instead of instance methods? Settings & app-wide functionality is the typical example for this design pattern when you only want to have 1 of something independent of instances. Set Class/Type variables for permissions that the later camera/location object instances can reference & use. Criticism of this approach is that they're like global variables (using appDelegate), and there are a few ways to construct them that vary between Obj-C and Swift. – mc01 Oct 07 '16 at 17:47
  • Yes you need an instance of `CLLocationManager` to request permission. There is no harm in initializing one, asking for permissions, releasing it and initializing a new one later. You could even initialize multiple `CLLocationManager` simultaneously if you wanted, the system is designed for that to work as well (and the documentation clearly states that this is something that you can do). – deadbeef Oct 07 '16 at 17:53

2 Answers2

2

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/

Shailesh Chandavar
  • 1,614
  • 14
  • 20
  • Great addition of techcrunch article explaining UX considerations for this process. If OP follows that advice, this problem solves itself since requesting permissions will come later & in context at the time those objects would be initialized anyway. – mc01 Oct 07 '16 at 18:03
  • great! One more edit please, could you provide a code to do the same with bluetooth manager? – theDC Oct 07 '16 at 18:23
  • Could you please explain me what are your requirements with bluetooth , what all things you want to do? DCDC – Shailesh Chandavar Oct 07 '16 at 18:38
  • I think these links will help you http://stackoverflow.com/questions/23455317/i-want-to-trigger-ios7-to-ask-users-permission-to-use-bluetooth-and-twitter-acco http://stackoverflow.com/questions/33189011/how-to-initialize-cbcentralmanager-in-swift-when-a-self-reference-is-necessary – Shailesh Chandavar Oct 07 '16 at 19:00
0

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
        }
    }
}
neprocker
  • 170
  • 5