7

I'm not inventing the wheel. In iOS8, to open Settings from inside the app I'm using this code:

BOOL canOpenSettings = (&UIApplicationOpenSettingsURLString != NULL);

if (canOpenSettings)
{
    NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
    [[UIApplication sharedApplication] openURL:url];
}

The code is in a lot of answers and questions in stackoverflow.

The problem came out with Xcode 6.3, I've got a warning saying:

Comparison of address of 'UIApplicationOpenSettingsURLString' not equal to a null pointer is always true

What is interesting is that Apple is using it in their example code:
https://developer.apple.com/library/ios/samplecode/AppPrefs/Listings/RootViewController_m.html

Some idea about how to avoid the warning and still checking if I can open Settings?

Gabriel.Massana
  • 8,165
  • 6
  • 62
  • 81
  • Facing similar issue after last update, haven't yet found any solution to it. And example code you mentioned was last updated on 2014-09-17. Well, i had gone through answers over here, http://stackoverflow.com/q/459743/2713079. But nothing really helped me too. – itsji10dra Apr 10 '15 at 11:05
  • I have posted my question over here, http://stackoverflow.com/q/29560015/2713079 if you want to have a look :) – itsji10dra Apr 10 '15 at 11:16

2 Answers2

14

SOLVED:

The problem is related with the Deployment Target in the App.

screenshot

If the Target is 8.0 or above, the comparison will be always true because you are always over 8.0. So we do not need the if verification:

NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
[[UIApplication sharedApplication] openURL:url];

Another option can be:

NSURL *settings = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
if ([[UIApplication sharedApplication] canOpenURL:settings])
{
    [[UIApplication sharedApplication] openURL:settings];
}
Iulian Onofrei
  • 9,188
  • 10
  • 67
  • 113
Gabriel.Massana
  • 8,165
  • 6
  • 62
  • 81
  • 1
    Oh yes, your problem can be solved this way, but mine is lil more complex. – itsji10dra Apr 10 '15 at 11:07
  • What makes this new warning really annoying is that if you share code between iOS and OS X, as the number of symbols you have to check increases, the probability that you'll be forced to tolerate this warning on one platform to avoid a crash on the other platform approaches 1.... There's probably a way to kill it with a pragma or a compiler setting, but I don't know what setting. – dgatwood Jul 09 '15 at 17:30
  • I'm told that you can disable the warning globally by adding -Wno-tautological-pointer-compare to your compiler flags, or on a one-off basis by wrapping the "&whatever" in parentheses. – dgatwood Jul 13 '15 at 22:18
  • what to do in else behaviour? – kiran Oct 20 '18 at 12:05
1

I believe this is because &UIApplicationOpenSettingsURLString is never nil in this version so you can just directly use the following to launch settings:

NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
[[UIApplication sharedApplication] openURL:url];
Schemetrical
  • 5,506
  • 2
  • 26
  • 43