Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing for UIApplicationOpenSettingsURLString existence with Swift

Tags:

ios

swift

ios7

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?

like image 445
Tsuharesu Avatar asked Oct 01 '14 19:10

Tsuharesu


2 Answers

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]];
}
like image 105
Keller Avatar answered Sep 19 '22 14:09

Keller


It's not the same as checking for a Constant, but if you know the API version that contains the Constant (in my case, iOS 8), there are some techniques: http://nshipster.com/swift-system-version-checking/

One of them is:

switch UIDevice.currentDevice().systemVersion.compare("8.0.0", options: NSStringCompareOptions.NumericSearch) {
case .OrderedSame, .OrderedDescending:
    println("iOS >= 8.0")
case .OrderedAscending:
    println("iOS < 8.0")
}
like image 23
Tsuharesu Avatar answered Sep 19 '22 14:09

Tsuharesu