19

Is there a way to have Xcode tell me when I'm calling a method that isn't available in the SDK of the minimum supported target?

For example, the method [NSURLConnection sendAsynchronousRequest:queue:completionHandler:]. This method is available on iOS5 and up. But my application's minimum target is iOS4.

If I use that method (sendAsync), I'd like Xcode to tell me that that method isn't available for the minimum target I'm trying to support.

I've tried putting __IPHONE_OS_VERSION_MAX_ALLOWED=40000 in the preprocessor settings, but that just triggers a bunch of Apple SDK errors that aren't helpful. (Probably because my active SDK is iOS5.1)

Is the only solution to get ahold of old SDKs and install them in Xcode?

Are there any easier solutions?

gngrwzrd
  • 5,902
  • 4
  • 43
  • 56
  • I'm not sure if I understand the question. You always use the latest SDK even if you target an older version. In fact, Apple will not accept your app in App Store if you don't use the latest SDK. But as long as you only use the subset of APIs that are available for the target version, you're golden. – Svein Halvor Halvorsen May 28 '12 at 21:58
  • right, but if there are people who are installing the app on iOS4, I need to make sure I don't use any iOS5+ APIs. So I'm looking for a way to let xcode warn me instead of having to look at every line of code and double check that it's iOS 4 safe. – gngrwzrd May 28 '12 at 22:40
  • Maybe [this Apple techdoc](http://developer.apple.com/library/mac/#documentation/DeveloperTools/Conceptual/cross_development/Configuring/configuring.html#//apple_ref/doc/uid/10000163i-CH1-SW2) maybe of help? – Svein Halvor Halvorsen May 29 '12 at 00:42
  • Looks to me like there is no way to do this. Any APIs that were introduced in an SDK after your deployment target are weak linked. But because the base SDK does have the method in question, you get no complaints from Xcode because it's just weak linking the method. Which is exactly my problem, I'd like to have Xcode "help" me and give me warnings about methods that are weak linked, so I can make sure to put the necessary respondsToSelector: checks around them, versus me having to manually look through the codebase and find them manually. – gngrwzrd May 29 '12 at 03:37
  • Not exactly a solution in XCode but might be useful for some of you: Use AppCode. It warns you about methods that are not part of the versions your app supports (e.g. using an iOS 9 method in an iOS 8 + 9 app). – marsbear Aug 04 '16 at 07:56
  • Possible duplicate of [Is there a way for XCode to warn about new API calls?](http://stackoverflow.com/questions/4676000/is-there-a-way-for-xcode-to-warn-about-new-api-calls) – JosephH Apr 05 '17 at 08:58

3 Answers3

7

There is unfortunately no standard way of doing this. By setting the target OS to a lower number than the base SDK, Xcode will weakly link the libraries and frameworks. When doing that Xcode will not warn you for using methods that may not be available on the target OS.

You could temporarily set the base SDK lower, but that might not always work. Since you want to ignore most of the errors and warnings produced (because they are only called conditionally in your code path), and many warnings and errors are dependant on other error that you may need to resolve before the compiler will give any meaningful output.

I do not think there exist any static analysis tools for this, neither from Apple nor third party.

Svein Halvor Halvorsen
  • 2,474
  • 1
  • 16
  • 14
6

After doing some research, reading the Apple Doc about it, and trying a number of things. The solution is downloading an old Xcode DMG from Apple, grab the .pkg file for the same SDK as your deployment target and install it in your version of Xcode. Here's how:

  1. Download older Xcode.dmg from Apple
  2. Open the DMG
  3. In Terminal, go into packages: "cd /Volumes/[DMG]/Packages; open ."
  4. Find the SDK you want, something like iPhoneSDK_4.0.pkg
  5. Install that package, but change the install directory to /Applications/Xcode/Contents/Developer
  6. Restart Xcode if it was open.

Now that you have the same SDK as your deployment target, set your BaseSDK to the same. When you build you'll get warnings about missing methods. Your project may or may not successfully build with an older BaseSDK in a new version of Xcode, but that doesn't matter - you've just found the method calls you need to wrap in a feature check with respondsToSelector:.

gngrwzrd
  • 5,902
  • 4
  • 43
  • 56
2

As of Xcode 7.3, the compiler can now generate these warnings for you. All you need to do is set the -Wpartial-availability warning flag in the Build Settings, as described in this answer.

deltacrux
  • 1,186
  • 11
  • 18
  • this is still working in 2020 beware that `-Wunguarded-availability` won't warn about incorrect API usage – Thomas Apr 08 '20 at 05:30