Search code examples
iosios7swift

Testing for UIApplicationOpenSettingsURLString existence with Swift


On iOS 8 Apple gave us the possibility to go to the App Settings right from our app, using the Constant UIApplicationOpenSettingsURLString

UIApplication.sharedApplication().openURL(NSURL.URLWithString(UIApplicationOpenSettingsURLString))

There's a code to test if this Constant exists on iOS 7, but it uses ObjC and pointes. Apple did this on their code: https://developer.apple.com/library/ios/samplecode/AppPrefs/Listings/RootViewController_m.html

How can I make something like this using Swift?


Solution

  • In Swift:

    switch UIDevice.currentDevice().systemVersion.compare("8.0.0", options: NSStringCompareOptions.NumericSearch) {
      case .OrderedSame, .OrderedDescending:
        UIApplication.sharedApplication().openURL(NSURL.URLWithString(UIApplicationOpenSettingsURLString))
      case .OrderedAscending:
        //Do Nothing.
    }
    

    In Objective-C:

    if (&UIApplicationOpenSettingsURLString != NULL) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
    }