Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/121.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
请求用户权限以在iOS 8中接收UILocalNotifications_Ios_Cocoa Touch_Notifications_Ios8_Uilocalnotification - Fatal编程技术网

请求用户权限以在iOS 8中接收UILocalNotifications

请求用户权限以在iOS 8中接收UILocalNotifications,ios,cocoa-touch,notifications,ios8,uilocalnotification,Ios,Cocoa Touch,Notifications,Ios8,Uilocalnotification,我已使用以下方法在应用程序代理中设置本地通知: - (void)applicationDidEnterBackground:(UIApplication *)application { UILocalNotification *notification = [[UILocalNotification alloc]init]; [notification setAlertBody:@"Watch the Latest Episode of CCA-TV"]; [notifi

我已使用以下方法在应用程序代理中设置本地通知:

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    UILocalNotification *notification = [[UILocalNotification alloc]init];
    [notification setAlertBody:@"Watch the Latest Episode of CCA-TV"];
    [notification setFireDate:[NSDate dateWithTimeIntervalSinceNow:5]];
    [notification setTimeZone:[NSTimeZone defaultTimeZone]];
    [application setScheduledLocalNotifications:[NSArray arrayWithObject:notification]];
}
当我运行应用程序,然后退出时,我收到一个错误,上面写着:

2014-06-07 11:14:16.663 CCA-TV[735:149070]试图安排本地通知 日期=2014年6月7日星期六太平洋夏令时11:14:21 区域=美国/洛杉矶(PDT)偏移-25200(日光),重复 间隔=0,重复计数=UILocalNotificationInfiniteRepeatCount, 下一次火灾日期=2014年6月7日星期六太平洋日光11:14:21 时间,用户信息=(null)}发出警报,但尚未收到 用户显示警报的权限


如何获得显示警报所需的权限?

由于iOS 8需要请求用户的权限才能显示应用程序中的通知,这适用于远程/推送和本地通知。在斯威夫特,你可以这样做

Swift 2.0的更新

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
    // Override point for customization after application launch.
    if(UIApplication.instancesRespondToSelector(Selector("registerUserNotificationSettings:")))
    {
        let notificationCategory:UIMutableUserNotificationCategory = UIMutableUserNotificationCategory()
        notificationCategory.identifier = "INVITE_CATEGORY"
        notificationCategory.setActions([replyAction], forContext: UIUserNotificationActionContext.Default)

        //registerting for the notification.
        application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes:[.Sound, .Alert, .Badge], categories: nil))
    }
    else
    {
       //do iOS 7 stuff, which is pretty much nothing for local notifications.
    }
    return true
}
Swift 3.2

if(UIApplication.instancesRespond(to: #selector(UIApplication.registerUserNotificationSettings(_:)))){
     let notificationCategory:UIMutableUserNotificationCategory = UIMutableUserNotificationCategory()
     notificationCategory.identifier = "INVITE_CATEGORY"
     notificationCategory.setActions([replyAction], forContext: UIUserNotificationActionContext.Default)

     //registerting for the notification.
        application.registerUserNotificationSettings(UIUserNotificationSettings(types:[.sound, .alert, .badge], categories: nil))
}
else{
        //do iOS 7 stuff, which is pretty much nothing for local notifications.
    }
Objective C语法也非常相似

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]){
        [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
    }
    // Override point for customization after application launch.
    return YES;
}
要检查当前注册的通知类型,可以使用UIApplication类的方法

- (UIUserNotificationSettings *)currentUserNotificationSettings
因此,如果用户对你的应用说不,那么这个函数应该返回一个没有任何类型的设置


我已经编写了一个关于此的教程,您可以看到。

将此代码放在视图控制器中,您将首先对通知进行编程(如果您在启动时对其进行编程,那么它将是
应用程序:didFinishLaunchingWithOptions:
):

迅速:

if(UIApplication.instancesRespondToSelector(Selector("registerUserNotificationSettings:"))) {
    UIApplication.sharedApplication().registerUserNotificationSettings(UIUserNotificationSettings(forTypes: .Alert | .Sound, categories: nil))
}

根据系统版本号进行测试的解决方案是次优的,并且容易出错

我也遇到了同样的问题。似乎在iOS 8中,我们需要执行额外的步骤,通常在内部完成:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { /*...*/ }
如果要保持向后兼容,可以使用此代码:

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000
    if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)])
    {
        [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil]];
    }
#endif

系统将记住该决定,并且只询问一次。

对于Objective-C,请尝试以下方法:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:    (NSDictionary *)launchOptions
{
// are you running on iOS8?
if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) 
  {
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge|UIUserNotificationTypeAlert|UIUserNotificationTypeSound) categories:nil];
    [application registerUserNotificationSettings:settings];
  } 
else // iOS 7 or earlier
  {
    UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
    [application registerForRemoteNotificationTypes:myTypes];
  }
}
对于Swift:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
// Override point for customization after application launch.
 if(UIApplication.instancesRespondToSelector(Selector("registerUserNotificationSettings:")))
 {
    application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: UIUserNotificationType.Sound | UIUserNotificationType.Alert | UIUserNotificationType.Badge, categories: nil))
 }
 else
 {
    //
 }
return true
}

**iOS8的三按钮操作本地通知+

//巴顿:我拿了,以后再提醒,跳过它**

        let completeAction = UIMutableUserNotificationAction()
        completeAction.identifier = "COMPLETE_TODO"
        completeAction.title = "I TOOK IT"
        completeAction.activationMode = .Background
        completeAction.destructive = true
        completeAction.authenticationRequired = false

        let remindAction = UIMutableUserNotificationAction()
        remindAction.identifier = "REMIND_TODO"
        remindAction.title = "REMIND LATER"
        remindAction.activationMode = .Background
        remindAction.destructive = false
        //  remindAction.authenticationRequired = false

        let skipAction = UIMutableUserNotificationAction()
        skipAction.identifier = "SKIP_TODO"
        skipAction.title = "SKIP IT"
        skipAction.activationMode = .Background
        skipAction.destructive = false
        skipAction.authenticationRequired = false


        let todoCategory = UIMutableUserNotificationCategory()
        todoCategory.identifier = "TODO_CATEGORY"
        todoCategory.setActions([completeAction, remindAction, skipAction], forContext: .Default)
        todoCategory.setActions([completeAction,remindAction,skipAction], forContext: .Minimal)


        if application.respondsToSelector("isRegisteredForRemoteNotifications")
        {

            let categories = NSSet(array: [todoCategory,todoVideoCategory])
            let types:UIUserNotificationType = ([.Alert, .Sound, .Badge])

            let settings:UIUserNotificationSettings = UIUserNotificationSettings(forTypes: types, categories: categories as? Set<UIUserNotificationCategory>)

            application.registerUserNotificationSettings(settings)
            application.registerForRemoteNotifications()

        }

    }
let completeAction=UIMutableUserNotificationAction()
completeAction.identifier=“完成任务”
completeAction.title=“我接受了”
completeAction.activationMode=.Background
completeAction.destromical=true
completeAction.authenticationRequired=false
让remindAction=UIMutableUserNotificationAction()
remindAction.identifier=“提醒\u待办事项”
remindAction.title=“以后提醒”
remindAction.activationMode=.Background
提醒动作.破坏性=错误
//remindAction.authenticationRequired=false
让skipAction=UIMutableUserNotificationAction()
skipAction.identifier=“跳过待办事项”
skipAction.title=“跳过它”
skipAction.activationMode=.Background
skipAction.destromical=false
skipAction.authenticationRequired=false
让ToLocategory=UIMutableUserNotificationCategory()
todoCategory.identifier=“TODO\u类别”
ToLocateGory.setActions([completeAction,remindAction,skipAction],forContext:.默认值)
ToLocategory.setActions([completeAction,remindAction,skipAction],forContext:.最小值)
if application.respondsToSelector(“isRegisteredForRemoteNotifications”)
{
let categories=NSSet(数组:[ToLocategory,ToDovideoCategority])
let类型:UIUserNotificationType=([.Alert、.Sound、.Badge])
let设置:UIUserNotificationSettings=UIUserNotificationSettings(FORTYPE:类型,类别:类别为?集)
application.registerUserNotificationSettings(设置)
应用程序.注册表项更改()
}
}

我想应用程序已经拒绝了该权限一次,您可以尝试从设置中启用。但是顺便说一句,UILocalNotification不需要用户权限。请尝试
registerUserNotificationSettings
。如果是iOS 8,此线程将回答您的问题。但是,请看一看——如果用户拒绝许可,您以后如何通过编程确定这一点?@satheeshwaran当我使用这段代码时,它与带iOS8的模拟器一起工作良好。我希望我的应用程序的部署目标从iOS7开始。因此,当我在iOS7设备上运行此代码时,会出现以下错误:
dyld:Symbol not found:\u OBJC\u CLASS\u$\ u UIUserNotificationSettings
。Swift中是否有其他方法要求用户获得权限以便在iOS7中工作?请提供帮助。@Raghav UIUserNotificationSettings仅在iOS 8中可用,您面临的是正确的行为。您不应该在iOS 7.-1中使用此选项来检查iOS设备的版本。答案有更好的方法来做到这一点。@derpoliuk为每个人的利益更新了答案,好吗?我会使用
application.respondsToSelector(选择器(“registerUserNotificationSettings”)
if([application respondsToSelector:@Selector(registerUserNotificationSettings:))
这仅仅是因为您在
应用程序中使用它:使用选项完成启动:
它提供了一个方便的
应用程序
对象:)如果([[UIDevice currentDevice].systemVersion floatValue]<10),最好使用此代码检查iOS版本
        let completeAction = UIMutableUserNotificationAction()
        completeAction.identifier = "COMPLETE_TODO"
        completeAction.title = "I TOOK IT"
        completeAction.activationMode = .Background
        completeAction.destructive = true
        completeAction.authenticationRequired = false

        let remindAction = UIMutableUserNotificationAction()
        remindAction.identifier = "REMIND_TODO"
        remindAction.title = "REMIND LATER"
        remindAction.activationMode = .Background
        remindAction.destructive = false
        //  remindAction.authenticationRequired = false

        let skipAction = UIMutableUserNotificationAction()
        skipAction.identifier = "SKIP_TODO"
        skipAction.title = "SKIP IT"
        skipAction.activationMode = .Background
        skipAction.destructive = false
        skipAction.authenticationRequired = false


        let todoCategory = UIMutableUserNotificationCategory()
        todoCategory.identifier = "TODO_CATEGORY"
        todoCategory.setActions([completeAction, remindAction, skipAction], forContext: .Default)
        todoCategory.setActions([completeAction,remindAction,skipAction], forContext: .Minimal)


        if application.respondsToSelector("isRegisteredForRemoteNotifications")
        {

            let categories = NSSet(array: [todoCategory,todoVideoCategory])
            let types:UIUserNotificationType = ([.Alert, .Sound, .Badge])

            let settings:UIUserNotificationSettings = UIUserNotificationSettings(forTypes: types, categories: categories as? Set<UIUserNotificationCategory>)

            application.registerUserNotificationSettings(settings)
            application.registerForRemoteNotifications()

        }

    }