2014-09-20 4 views
0

은 AppDelegate에 다음 코드로 처리 푸시 알림을 구문 분석 :iOS 버전에 따라 푸시 알림을 처리하는 방법은 무엇입니까? iOS7에 대한

[application registerForRemoteNotificationTypes: 
UIRemoteNotificationTypeBadge| 
UIRemoteNotificationTypeAlert| 
UIRemoteNotificationTypeSound]; 

registerForRemoteNotificationTypes 그러나 iOS8의에서 지원되지 않습니다, 지금 iOS8의에서 푸시 알림을 처리하는 데 사용되는 새로운 코드는 다음과 같습니다

UIUserNotificationSettings *settings = 
[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | 
UIUserNotificationTypeBadge | 
UIUserNotificationTypeSound 
            categories:nil]; 
[[UIApplication sharedApplication] registerUserNotificationSettings:settings]; 
[[UIApplication sharedApplication] registerForRemoteNotifications]; 

이 새로운 코드를 iOS7 장치와 함께 사용하면 앱이 충돌을 일으켜 코드가 어떤 버전의 휴대 전화인지 확인하고 적절한 푸시 알림 코드를 실행해야합니다. 앱에서이를 확인하고 올바른 것을 사용하려면 어떻게해야합니까?

+0

가능한 [registerForRemoteNotificationTypes : iOS 8.0 이상에서는 지원되지 않습니다.] (http://stackoverflow.com/questions/24454033/registerforremotenotificationtypes-is-not-supported-in-ios-8-0-and-later) –

답변

3

의 중복이 아니라 OS 버전보다, 방법의 유용성을 확인하는 것이 좋습니다.

if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerForRemoteNotifications)]) { 

    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil]; 

    [[UIApplication sharedApplication] registerUserNotificationSettings:settings]; 

    [[UIApplication sharedApplication] registerForRemoteNotifications]; 

} else { 

    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)]; 
} 

배포 대상이 7.0 이상이라고 가정합니다.

0

아마도 registerForRemoteNotificationTypes: is not supported in iOS 8.0 and later

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) 
{ 
    [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]]; 
    [[UIApplication sharedApplication] registerForRemoteNotifications]; 
} 
else 
{ 
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes: 
    (UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)]; 
} 
+1

가장 좋은 방법은 다른 질문에 대한 링크가 포함 된 설명을 추가하는 것입니다. 또는, 더 나은 아직,이 질문을 복제물로 표시하십시오. –

관련 문제