I know in Swift it's possible to make use of #available and objective-c doesn't support that.
Is there still a way to make an os x app which can be run on 10.11 but also supports the NSTouchbar (which starts from 10.12)?
#available
and friends in Swift are a simplification of (and improvement on) the more complex ways of dealing with new features and backward compatibility in ObjC. If you're using ObjC, there isn't a direct equivalent to #available
, so you have to fall back to those old ways. Apple's docs on such can be found in the SDK Compatibility Guide. The gist:
Build your project using the latest Xcode 8.x / macOS 10.12.x (or later) SDK.
Set your project's Base SDK to macOS 10.12 (or later), and its Minimum Deployment Target to 10.11.
This makes the AppKit classes/methods you're looking for "weak linked", so you can check for their presence at run time. For example:
if ([NSTouchBar class]) {
self.touchBar = [[NSTouchBar alloc] init];
self.touchBar.delegate = self;
self.touchBar.defaultItemIdentifiers = @[ /*...*/ ];
//...
}
In macOS versions predating touch bar support, [NSTouchBar class]
returns nil, so none of the touch bar usage happens. In versions with touch bar support, your if
body executes.