dan*_*ler 32 xcode objective-c ios
我有一个使用本地通知的应用程序.在iOS 7中,一切正常,但在iOS 8中,应用程序需要请求用户显示通知的权限.要在iOS 8中请求权限我正在使用:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
}
Run Code Online (Sandbox Code Playgroud)
它在Xcode 6和iOS 8中工作正常.当我在Xcode 5中打开相同的项目时,错误是语义问题."使用未声明的标识符'UIUserNotificationSettings'."
如何让应用程序与iOS 7和8一起使用,并使这两个版本的通知正常工作.
rma*_*ddy 62
以下答案做了一些假设:
码:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// None of the code should even be compiled unless the Base SDK is iOS 8.0 or later
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000
// The following line must only run under iOS 8. This runtime check prevents
// it from running if it doesn't exist (such as running under iOS 7 or earlier).
if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
}
#endif
}
Run Code Online (Sandbox Code Playgroud)
所有这些都包含在Apple SDK兼容性指南中.
小智 11
我简单地实施了 -
if([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]) {
[[UIApplication sharedApplication] registerForRemoteNotifications];
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert) categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
} else {
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge];
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
35949 次 |
最近记录: |