Ask for User Permission to Receive UILocalNotifications in iOS 8

前端 未结 5 676
自闭症患者
自闭症患者 2020-11-29 15:54

I have set up local notifications in the App Delegate Using this:

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    UILocalNotificati         


        
5条回答
  •  庸人自扰
    2020-11-29 16:17

    Put this code in the view controller where you will first program the notifications (if you program them at launch, then it will be application:didFinishLaunchingWithOptions:):

    if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]) {
        [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeSound categories:nil]];
    }
    

    In Swift:

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

    The solutions that test against system version number are sub-optimal and error-prone.

提交回复
热议问题