Sergey Mikhanov
Sergey Mikhanov

Reputation: 8960

Creating an app with deployment target of iOS 7.1 and optional iOS 8-only features

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?

Upvotes: 1

Views: 82

Answers (2)

Nicolas Buquet
Nicolas Buquet

Reputation: 3955

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.

Upvotes: 1

Michael Dorner
Michael Dorner

Reputation: 20145

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?

Upvotes: 1

Related Questions