在iOS 8中要求获得本地通知的权限,但仍然拥有iOS 7应用程序支持 [英] Ask for Permission for Local Notifications in iOS 8, but still have the App Support iOS 7
问题描述
我有一个使用本地通知的应用。在iOS 7中,一切正常,但在iOS 8中,应用程序需要请求用户显示通知的权限。要在iOS 8中请求权限,我正在使用:
I have an app which uses local notifications. In iOS 7 everything works fine, but in iOS 8 the app needs to ask for user permission to display notifications. To ask for permission in iOS 8 I'm using:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
}
它在Xcode 6和iOS 8中运行正常。当我打开它时在Xcode 5中的项目,错误是语义问题。 使用未声明的标识符'UIUserNotificationSettings'。
It works fine in Xcode 6 and in iOS 8. When I open the same project in Xcode 5, the error is a Semantic Issue. "Use of undeclared identifier 'UIUserNotificationSettings'."
如何让应用程序与iOS 7及更高版本配合使用? 8,并在两个版本上正常通知。
How can I get the app to work with iOS 7 & 8, and have the notifications work properly on both versions.
推荐答案
以下答案做了一些假设:
The following answer makes a few assumptions:
- 当使用Xcode 6时,应用必须使用iOS 8的Base SDK正确构建,并且在使用Xcode 5时必须使用iOS 7的Base SDK正确构建。
- 应用程序必须支持iOS 7(或更早版本)的部署目标,无论Base SDK和Xcode版本如何。
代码:
- (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
}
所有这些都包括在内在Apple SDK兼容性指南。
All of this is covered in the Apple SDK Compatibility Guide.
这篇关于在iOS 8中要求获得本地通知的权限,但仍然拥有iOS 7应用程序支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!