1

I currently have a locationManager (not CLLocationManager) singleton that is getting my location when the app starts and that manages the rest of the location services in the app.

When getting initialized the location manager if the state is denied triggers an alert prompting to change the authorization state. Since it is a nsobject I can't present it, and I've been looking for other options but most are hacks. Is it actually possible to present it this way?

Thanks in advance

Jake Ortiz
  • 503
  • 9
  • 20

2 Answers2

5

I'm sure some will argue about whether or not you SHOULD. With that being said, you COULD by doing this:

[[(<#YourAppDelegate#> *)[UIApplication sharedApplication].delegate window].rootViewController presentViewController:<#(UIViewController *)#> animated:<#(BOOL)#> completion:<#^(void)completion#>];

It should be noted that if you're using a Mac app as opposed to iOS, multiple windows could be present and you may want to use the key window. This could occasionally be a system window so it's something you should be aware of. Here's a post explaining the differences:

https://stackoverflow.com/a/21698751/2611971

If you'd prefer to present on your keyWindow, you could use this:

[[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:<#(UIViewController *)#> animated:<#(BOOL)#> completion:<#^(void)completion#>];
Community
  • 1
  • 1
Logan
  • 52,262
  • 20
  • 99
  • 128
  • `window` may be `nil` in some cases. See http://stackoverflow.com/a/21698751/1445366 – Aaron Brager Dec 03 '14 at 18:10
  • @AaronBrager - That seems to pertain more to Mac Apps, however I think it's relevant, so I did add an addendum linking to that thread as well as a demonstration showing the `keyWindow` approach. Anything else you think should be there? – Logan Dec 03 '14 at 18:34
  • I'm going to try your method Logan and write back. What are the scenarios that code has given you problems? – Jake Ortiz Dec 04 '14 at 01:33
  • How do you set key window property? – Jake Ortiz Dec 04 '14 at 02:08
  • If you're using storyboards, I'm pretty sure it's set automatically. Otherwise on your window call 'makeKeyAndVisible' – Logan Dec 04 '14 at 03:10
  • Ok, it worked but it doesn't appear if I have any modal view controller present – Jake Ortiz Dec 04 '14 at 12:48
  • If your app doesn't use storyboards, and you never set the `window` property, it'll be `nil`, even in iOS apps. – Aaron Brager Dec 04 '14 at 15:01
  • @AaronBrager - I see what you were saying now. I haven't used storyboards in a while. Will `keyWindow` be nil in that situation? Jake, you're saying that if you've presented a VC modally then this doesn't present on top of it? You could check the `.presentedViewController` on your rootVC and if that exists, present on that vc instead. – Logan Dec 04 '14 at 15:08
  • As long as you've called `makeKeyAndVisible` on a window (or used a storyboard), `keyWindow` will not be `nil`. – Aaron Brager Dec 04 '14 at 17:46
  • However, some views are presented in separate windows, like UIAlertView, UIActionSheet, and SVProgressHUD. So `keyWindow` might not be the window you want either. – Aaron Brager Dec 04 '14 at 17:46
  • It may work Logan, gonna try it. Although of what can I see it could get problems if there are several modals presented by modals – Jake Ortiz Dec 04 '14 at 20:50
1

You can set your main view controller object as an observer for a AuthorizationDenied notification via Notification Center. And post that notification when it's denied, and your view controller will get notified.

In your View Controller loading:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(authorizationDenied:) name:@"AuthorizationDenied" object:nil];

In your singleton class:

[[NSNotificationCenter defaultCenter] postNotificationName:@"AuthorizationDenied" object:nil];
ugur
  • 824
  • 1
  • 7
  • 15