ObjecTips

Swift & Objective-C で iOS とか macOS とか

自作の iOS Framework を watchOS に対応させる

自作Farmework内で watchOS で利用できないAPIを使っていると not available on watchOS のエラーが出て watchOS 向けにビルドできない。
Apple純正の Framework と同じようにヘッダのメソッド定義に __WATCHOS_PROHIBITED を付けて watchOS では使えません宣言をすると ifdef を使ったりせずメソッドをそのままビルド可能になる。

ビルドエラー
// .h
- (void)test:(EKEventStore *)eventStore;

// .m
- (void)test:(EKEventStore *)eventStore
{
    [eventStore commit:nil]; // 'commit:' is unavailable: not available on watchOS
}
OK
// .h
- (void)test:(EKEventStore *)eventStore __WATCHOS_PROHIBITED;

// .m
- (void)test:(EKEventStore *)eventStore
{
    [eventStore commit:nil];
}

クラス自体を watchOS で使えないように宣言する場合はクラス宣言の @interface の前にこれを付けると良い

__WATCHOS_PROHIBITED @interface MyClass : NSObject