在swift中注册iOS 7通知 [英] Registering for iOS 7 notifications in swift

查看:89
本文介绍了在swift中注册iOS 7通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够在iOS 7和iOS 8上收到通知,我已经成功设置了ios8,但是这个iOS 7我得到的错误只是lldb就行了var mySettings .. 。从我读过的内容来看,你是如何在iOS 7上注册它的,但它似乎不起作用!

I want to be able to have notifications on iOS 7 and iOS 8, I've set up it with ios8 successfully but this iOS 7 I get an error of just "lldb" nothing else on the line "var mySettings...". From what I've read this is how you are meant to register it on iOS 7 but it doesn't seem to work!

   func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> bool{

    //check if its on ios8

    var deviceVersion :  NSString = UIDevice.currentDevice().systemVersion



    if deviceVersion.floatValue >= 8.0 {

     //I've set up the iOS 8 notifications here and that all works.

    }else{



        var types : UIUserNotificationType = UIUserNotificationType.Badge | UIUserNotificationType.Alert;



        var mySettings : UIUserNotificationSettings = UIUserNotificationSettings(forTypes: types, categories: nil)



        UIApplication.sharedApplication().registerUserNotificationSettings(mySettings)



    }





    return true

}


推荐答案

您的问题是:

UIApplication.sharedApplication()。registerUserNotificationSettings(mySettings)

UIApplication.sharedApplication().registerUserNotificationSettings(mySettings)

ios 7不支持此功能。你应该实现这样的东西:

This is not supported in ios 7. You should implement something like this:

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
{
   //do ios7 stuff here. If you are using just local notifications then you dont need to do anything. for remote notifications:
application.registerForRemoteNotificationTypes(UIUserNotificationType.Sound | UIUserNotificationType.Alert | UIUserNotificationType.Badge)
}
return true
}

这篇关于在swift中注册iOS 7通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆