1

I'm creating an app that works with CloudKit framework (iOS 8-only) but still want to keep compatibility with iOS 7.1, with no CloudKit functionality, of course. Apple documentation recommends checking for optional classes like this:

if ([CKRecordID class]) {
    // iOS 8 code
} else {
    // iOS 7 code
}

This works. On the other hand, if I write

@interface Foo : NSObject
@property (nonatomic) CKRecordID *recordID;
@end

anywhere in the code, the app will crash on iOS 7 when loading the Foo class. How can I define properties with those optional classes?

Sergey Mikhanov
  • 8,880
  • 9
  • 44
  • 54
  • u can check device system version is >8.0 or not and u can go for it....! This is not the one you want but it is just alternative..! – Vidhyanand May 05 '15 at 10:52
  • use if condition like this if([UIDevice currentDevice].systemVersion.floatValue >= 8.0f]){ //ios 8 code } else { //ios 7 or lower version code} – Deepak Thakur May 05 '15 at 11:07
  • 1
    http://www.cocoanetics.com/2012/09/target-conditionals-and-availability/ – TonyMkenu May 05 '15 at 11:10

2 Answers2

1

You could use the forward declaration

@class CKRecordID;

but you will need runtime checks for the iOS version, such as

[[NSProcessInfo processInfo] operatingSystemVersion]

Other solutions for detecting the iOS version are shown here or here.

But how about two different builds for different iOS versions?

Community
  • 1
  • 1
Michael Dorner
  • 17,587
  • 13
  • 87
  • 117
1

You can make your property recordId of type id or NSObject.

And when you need to access this property (after checking that your runtime is iOS8+), you cast it to CKRecordID class.

Nicolas Buquet
  • 3,880
  • 28
  • 28