xcode 请求用户在触发本地通知时显示警报的权限
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24203066/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ask user for permission to show alert when firing local notification
提问by user3737316
I want to show an alert when the local notification is fired, but for that I have to ask for permission, as it says to me when I run the app on my iPhone:
我想在触发本地通知时显示警报,但为此我必须请求许可,正如我在 iPhone 上运行该应用程序时对我说的那样:
Attempting to schedule a local notification {fire date = Friday 13 June 2014 12 h 10 min 27 s Central European Summer Time, time zone = (null), repeat interval = 0, repeat count = UILocalNotificationInfiniteRepeatCount, next fire date = Friday 13 June 2014 12 h 10 min 27 s Central European Summer Time, user info = (null)} with an alert but haven't received permission from the user to display alerts
尝试安排本地通知 {发射日期 = 2014 年 6 月 13 日星期五 12 小时 10 分 27 秒 中欧夏令时,时区 =(空),重复间隔 = 0,重复计数 = UILocalNotificationInfiniteRepeatCount,下一个发射日期 = 2014 年 6 月 13 日星期五12 h 10 min 27 s 中欧夏令时,用户信息 = (null)} 带有警报但尚未获得用户显示警报的许可
How can I do that? Here′s the code as it is now:
我怎样才能做到这一点?这是现在的代码:
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
localNotif.fireDate = [[NSDate date] dateByAddingTimeInterval:timeUntilNotification];
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.alertBody = @"ZEIT!";
localNotif.alertAction = @"Show me the Timer!";
localNotif.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] +1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
回答by u3295047
add this code, it will show a alert view to ask user for permission.
添加此代码,它将显示一个警报视图以请求用户许可。
if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]) {
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeSound|UIUserNotificationTypeBadge
categories:nil]];
}
you can add this code in application:didFinishLaunchingWithOptions; method, so that the app will ask your user when they launch the app, or you can add this code when you set Local Notification, it's up to you.
您可以在 application:didFinishLaunchingWithOptions 中添加此代码;方法,以便应用程序在您的用户启动应用程序时询问您的用户,或者您可以在设置本地通知时添加此代码,这取决于您。
回答by Morkrom
蘇健豪's answer is good.
苏健豪的回答很好。
In Swift it looks like this:
在 Swift 中,它看起来像这样:
let registerUserNotificationSettings = UIApplication.instancesRespondToSelector("registerUserNotificationSettings:")
if registerUserNotificationSettings {
var types: UIUserNotificationType = UIUserNotificationType.Alert | UIUserNotificationType.Sound
UIApplication.sharedApplication().registerUserNotificationSettings(UIUserNotific??ationSettings(forTypes: types, categories: nil))
}
Also see here: Ask for User Permission to Receive UILocalNotifications in iOS 8
回答by Cayke Prudente
//register notifications
if([application respondsToSelector:@selector(registerUserNotificationSettings:)]) //ios 8+
{
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
[application registerForRemoteNotifications];
}
else // ios 7 or less
{
[application registerForRemoteNotificationTypes:UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge];
}
回答by Krishnaveni
In the swift language....
用迅捷的语言......
var type = UIUserNotificationType.Badge | UIUserNotificationType.Alert | UIUserNotificationType.Sound;
var setting = UIUserNotificationSettings(forTypes: type, categories: nil);
UIApplication.sharedApplication().registerUserNotificationSettings(setting);
UIApplication.sharedApplication().registerForRemoteNotifications();
回答by Jaime
Try this for Objective-C
在 Objective-C 中试试这个
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
NSLog(@"didFinishLaunchingWithOptions");
if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]) {
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeSound categories:nil]];
}
return YES;
}