Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<h3>The quick answer …</h3> <p><br/> As of Swift 2.0, you can use <code>#available</code> in an <code>if</code> or <code>guard</code> to protect code that should only be run on certain systems.</p> <p><code>if #available(iOS 9, *) {}</code> <br/> <br/> <br/> In Objective-C, you need to check the system version and perform a comparison.</p> <p><code>[[NSProcessInfo processInfo] operatingSystemVersion]</code> in iOS 8 and above.</p> <p>As of Xcode 9:</p> <p><code>if (@available(iOS 9, *)) {}</code></p> <p><br/></p> <h2>The full answer …</h2> <p>In Objective-C, and Swift in rare cases, it's better to avoid relying on the operating system version as an indication of device or OS capabilities. There is usually a more reliable method of checking whether a particular feature or class is available.</p> <p><strong>Checking for the presence of APIs:</strong></p> <p>For example, you can check if <code>UIPopoverController</code> is available on the current device using <code>NSClassFromString</code>:</p> <pre><code>if (NSClassFromString(@"UIPopoverController")) { // Do something } </code></pre> <p>For weakly linked classes, it is safe to message the class, directly. Notably, this works for frameworks that aren't explicitly linked as "Required". For missing classes, the expression evaluates to nil, failing the condition:</p> <pre><code>if ([LAContext class]) { // Do something } </code></pre> <p>Some classes, like <code>CLLocationManager</code> and <code>UIDevice</code>, provide methods to check device capabilities:</p> <pre><code>if ([CLLocationManager headingAvailable]) { // Do something } </code></pre> <p><strong>Checking for the presence of symbols:</strong></p> <p>Very occasionally, you must check for the presence of a constant. This came up in iOS 8 with the introduction of <code>UIApplicationOpenSettingsURLString</code>, used to load Settings app via <code>-openURL:</code>. The value didn't exist prior to iOS 8. Passing nil to this API will crash, so you must take care to verify the existence of the constant first:</p> <pre><code>if (&amp;UIApplicationOpenSettingsURLString != NULL) { [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]]; } </code></pre> <p><strong>Comparing against the operating system version:</strong></p> <p>Let's assume you're faced with the relatively rare need to check the operating system version. For projects targeting iOS 8 and above, <code>NSProcessInfo</code> includes a method for performing version comparisons with less chance of error:</p> <pre><code>- (BOOL)isOperatingSystemAtLeastVersion:(NSOperatingSystemVersion)version </code></pre> <p>Projects targeting older systems can use <code>systemVersion</code> on <code>UIDevice</code>. Apple uses it in their <a href="http://developer.apple.com/iphone/library/samplecode/GLSprite/Listings/Classes_EAGLView_m.html" rel="noreferrer">GLSprite</a> sample code.</p> <pre><code>// A system version of 3.1 or greater is required to use CADisplayLink. The NSTimer // class is used as fallback when it isn't available. NSString *reqSysVer = @"3.1"; NSString *currSysVer = [[UIDevice currentDevice] systemVersion]; if ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending) { displayLinkSupported = TRUE; } </code></pre> <p>If for whatever reason you decide that <code>systemVersion</code> is what you want, make sure to treat it as an string or you risk truncating the patch revision number (eg. 3.1.2 -> 3.1).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload