1

For my app, I want to support for iOS7 and iOS8. I have set the deployment target to iOS7, however Xcode does not highlight to me methods which are only available on iOS8 (causing the app to crash in iOS7, example using [NSString containsString]).

In Android Studio, if you have set the minSDK, it shows you a warning if you are using methods that are only added in newer versions. I have tried searching around, but I can't really seem to find anything useful. I feel I might be missing something basic here.

Justin Leo
  • 1,975
  • 3
  • 15
  • 14
  • 1
    AFAIK, there's no way to do that, you kinda have to know what's been added with every release to know if it's safe to use. About your specific example, you can use `NSString.rangeOfString:(NSString *)`, and check if the location is equal to `NSNotFound` to know if a string contains another string. This will work on iOS7 as well as iOS8 – EmilioPelaez Jul 27 '15 at 04:04
  • Wow, considering that Apple emphasises a lot on app performance and stability, I find it amazing that they don't have something as basic as this. I would think that a lot of app crashes on older iOS might be caused by something as simple as this. Thanks for the suggestion for NSString containsString. However I think my app has other iOS8 only codes, but now I have no way of finding out. By the way I think you should post your comment as an answer though. – Justin Leo Jul 27 '15 at 04:09
  • Apple emphasizes and talks a lot of things, but they really don't put much effort into making things easy for developers. This is very much one of those situations where you have to do ugly code if you want to use something new. When a new iOS comes out, nothing is updated to the old ones and they are forgotten. And you really have to check every method to see it's actually available on your target when coding. Wouldn't be hard for Apple to make an analysis tool, but... – Sami Kuhmonen Jul 27 '15 at 05:00

5 Answers5

2
if ( [object respondsToSelector:@selector(containsString:)] )
    NSLog ( [object containsString:@"abc"]?@"WOOT!":@"darn..." )
iAdjunct
  • 2,739
  • 1
  • 18
  • 27
  • Thanks for the suggestion for this, but I want to detect iOS8 only methods for all of my code. I can't possibly add respondToSelector in my entire code. – Justin Leo Jul 27 '15 at 04:11
  • Well, there isn't really an elegant way to avoid doing this. You could theoretically twiddle the classes and ADD those methods in, but realistically you have to decide to not call methods that aren't there somewhere. – iAdjunct Jul 27 '15 at 04:16
1

This is how you do it:

if ([string respondsToSelector:@selector(containsString:)]) {
    //Do your iOS 8 only code.
}
Enrico Susatyo
  • 19,372
  • 18
  • 95
  • 156
  • Thanks for the suggestion for this, but I want to detect iOS8 only methods for all of my code. I can't possibly add respondToSelector in my entire code. – Justin Leo Jul 27 '15 at 04:11
0

There is no way to know if you are using a new function without support of old iOS version. The only way is to check the availability of them in the documentation :(.

In order to write a code that changes between iOS versions add global variable or define:

#define IS_UNDER_IOS_8 ([[[UIDevice currentDevice] systemVersion] floatValue] < 8.0)

use it like this:

if (IS_UNDER_IOS8) {

}
else {

}
Yedidya Reiss
  • 5,316
  • 2
  • 17
  • 19
0

You could just replace

[stringA containsString:stringB]

with

[stringA rangeOfString:stringB].location != NSNotFound

This will check if the location is equal to NSNotFound to know if a string contains another string.

JonyFang
  • 5
  • 2
0

Probably one can try to compile the project with earlier version of base SDK. Then it should show the code that does not work in earlier iOS versions. Though XCode includes only latest SDK. So needed SDK needs to be downloaded and copied to appropriate directory. Something like that: https://stackoverflow.com/a/12523971/550656

Community
  • 1
  • 1
jesse
  • 650
  • 5
  • 19