Skip to content

Instantly share code, notes, and snippets.

@vigneshr89
Last active September 24, 2016 15:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vigneshr89/befe6a99e7979772e449 to your computer and use it in GitHub Desktop.
Save vigneshr89/befe6a99e7979772e449 to your computer and use it in GitHub Desktop.
Partial API Availability check in Xcode
Problem:
Xcode does not create warnings when API introduced after "Deployment Target SDK Version" are used in the source code.
Some background information:
The APIs that are introduced after the deployment target version are weak imported by the llvm clang compiler in the binary to manage backward compatibility i.e, target device will run the new weak imported API if it is available (Think how you will access a weak variable in swift).
Hence if proper care is not taken, app will break at runtime.
In iOS and Mac OSX SDKs API availabilities are marked with a macro(ex:NS_AVAILABLE_IOS) defined in runtime header(NSObjRuntime.h) file.
So back in old times, the issue was addressed by macro overrides in preprocessing headers in xcode. But everytime xcode was updated a new macro had to be overridden.
The temporary solution mentioned above was completely broken in recent xcode that supports modules (That’s is pretty much all the Xcode versions we used from 2014).
Solution:
1. Use “Deploymate” like tool to identify such issues.
2. Make your tool with the clang client APIs. There are tutorials, but it is not that easy.
Luckily, back in Mar 2015 partial availability warning is added (Thanks ) in the open source LLVM clang master code, but it didn't find its way to the newest XCode.
To add the new clang and partial warning with xcode,
0. Get the clang
You can either build from clang master repo use a release after the partial warning changes are integrated or use the release after the changes are available here.
1. Go to build setting and add a user defined setting "CC"
2. For the Debug configuration give the path of the downloaded clang file. Eg: (/Users/allforIOS/Downloads/clang+llvm-3.7.0-x86_64-apple-darwin/bin/clang)
3. Add -Wpartial-availability in the Warning Linker Flags.
To test the solution,
Set deployment target to 8 or lesser, annotate any method with NS_AVAILABLE_IOS(9_0) and build. Voila!.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment