native
native copied to clipboard
[ffigen] ObjC version compatibility runtime check
Not all APIs are available on all versions of iOS. There are annotations in Objective C for this, so we can turn that into version checks, and throw user friendly exceptions rather than crashing. Suggestion from Brian:
- (NSURLSessionWebSocketTask *)webSocketTaskWithURL:(NSURL *)url protocols:(NSArray<NSString *>*)protocols API_AVAILABLE(macos(10.15), ios(13.0), watchos(6.0), tvos(13.0));
Could translate into:
webSocketTaskWithURL(NSURL url, NSArray<NSString> protocols) {
if (platform.isIOS) {
if (platform.version < 13.0) {
throw UnsupportedError("webSocketTaskWithURL requires: macos > 10.15, ios >= 13.0, watchos >= 6.0 or tvos >= 13.0 [is ios ${platform.version}.")
}
} else ...
<do the real work>
}
cc @brianquinlan
Cool, reading those annotations and generating version checking code is a cool idea!
platform.version
Isn't that the Dart version instead of the OS version?
Oops, yeah. Probably meant to use operatingSystemVersion, but I don't think we want to try parsing that.
We're now parsing those API version annotations (#1403). So the only remaining issue is how do we get the OS version at runtime. Since a check like that is OS specific and requires native calls, it might make sense to add a native function to package:objective_c to do this on iOS and macOS. If OS version getters are available on every mobile and desktop platform, and always returns an answer that fits in a semver, then we could instead add this to dart:ffi or package:ffi.