1. ホーム
  2. オブジェクティブC

[解決済み] registerForRemoteNotificationTypes: は iOS 8.0 以降でサポートされていません。

2022-04-12 10:34:36

質問

iOS 8.xでプッシュ通知に登録しようとすると、エラーが発生します。

application.registerForRemoteNotificationTypes(UIRemoteNotificationType.Alert | UIRemoteNotificationType.Badge | UIRemoteNotificationType.Sound)

以下のようなエラーが発生します。

registerForRemoteNotificationTypes: is not supported in iOS 8.0 and later.

何か新しい方法はないでしょうか?iOS 7.xでこのSwiftアプリを実行すると動作するのですが。

EDIT

iOS 7.xで条件コードを入れると、次のようになります(SystemVersionの条件か、#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000)。

dyld: Symbol not found: _OBJC_CLASS_$_UIUserNotificationSettings

解決方法は?

ご指摘の通り、iOSのバージョンによって異なる方法を使用する必要があります。もしあなたのチームがXcode 5(iOS 8のセレクタを知らない)とXcode 6の両方を使用している場合、次のように条件付きコンパイルを使用する必要があります。

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000
if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
    // use registerUserNotificationSettings
} else {
    // use registerForRemoteNotificationTypes:
}
#else
// use registerForRemoteNotificationTypes:
#endif

Xcode 6しか使っていない場合は、これだけで我慢してください。

if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
    // use registerUserNotificationSettings
} else {
    // use registerForRemoteNotificationTypes:
}

ここにあるのは、iOS 8で通知許可の取得方法が変更されたためです。A UserNotification は、リモートまたはローカルのいずれからであっても、ユーザーに表示されるメッセージです。表示するためには許可を得る必要があります。これは、WWDC 2014のビデオで説明されています "iOSの通知の新機能"