native icon indicating copy to clipboard operation
native copied to clipboard

[ffigen] ObjC version compatibility runtime check

Open liamappelbe opened this issue 3 years ago • 5 comments
trafficstars

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>
}

liamappelbe avatar Apr 07 '22 22:04 liamappelbe

cc @brianquinlan

liamappelbe avatar Apr 08 '22 00:04 liamappelbe

Cool, reading those annotations and generating version checking code is a cool idea!

dcharkes avatar Apr 08 '22 07:04 dcharkes

platform.version

Isn't that the Dart version instead of the OS version?

dcharkes avatar Jul 18 '24 07:07 dcharkes

Oops, yeah. Probably meant to use operatingSystemVersion, but I don't think we want to try parsing that.

liamappelbe avatar Jul 19 '24 01:07 liamappelbe

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.

liamappelbe avatar Aug 13 '24 02:08 liamappelbe