Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if not available methods are used if deployment target < base sdk?

I would like to know how you check that your code do not call not available methods when the deployment target is inferior to base SDK ?

It is possible to run the application on a device with the SDK equal to deployment target, but I search a way more 'automatic'. Any idea ?

Regards, Quentin

like image 673
Quentin Avatar asked Dec 14 '10 09:12

Quentin


2 Answers

The easiest way to do this is to use the __IPHONE_OS_VERSION_MAX_ALLOWED preprocessor define.

You do this by adding

__IPHONE_OS_VERSION_MAX_ALLOWED=__IPHONE_4_2

or something similar to your "Preprocessor Macros" option in Build Settings of your target. You can look up versions available in <Availability.h>.

Unfortunately if you add this define it will cause mismatch errors with your precompiled header. So, to fix that you need to turn off the "Precompile Prefix Header" option in your build settings as well.

Once you do this you'll get a bunch of errors for classes that don't exist on your targeted SDK (for instance NSOrderedSet doesn't exist in iOS 4.2). If you're trying to go back pre-iOS 4 you'll probably get so many errors that the compiler bails--I don't know of a workaround for this. In any case, ignore the errors about missing classes in the UIKit headers, and go to the bottom of the error list; there you should find an error for each time you use a method or class that isn't included in the SDK pointed to by __IPHONE_OS_VERSION_MAX_ALLOWED. Make sure each of these methods is enclosed in an

if( [targetObject respondsToSelector:@selector(thePossiblyMissingSelector:)]

and you should be safe. Classes that may be missing should be tested as well

if ([NSOrderedSet class] != nil)

These settings aren't something you want to accidentally forget to flip back however. To make this an automatic option for testing, do the following:

  1. Create a new build configuration called something like "Old SDK Testing".
  2. Define __IPHONE_OS_VERSION_MAX_ALLOWED and the precompiled head option only for this configuration (hit the disclosure arrow beside each line in Build Settings to access per configuration settings).
  3. Duplicate your current Scheme and set its name to something like "Old SDK Check".
  4. Set the Build Configuration of the Run item in this new scheme to the build configuration you created in step 1.
  5. Select the new Scheme and build.

Notes:

  • I make no guarantee that this will catch any/all of your issues.
  • Anything outside of UIKit will not be caught by this check.
  • This is not a substitute for testing your code on the versions of iOS you plan to support.
like image 121
Ben Lachman Avatar answered Oct 15 '22 00:10

Ben Lachman


use NSClassFromString();

Class cls = NSClassFromString(@"YourClass");
if (cls == nil)

is this you are looking for?

like image 2
Kapil Choubisa Avatar answered Oct 15 '22 01:10

Kapil Choubisa