xml地图|网站地图|网站标签 [设为首页] [加入收藏]
python学习记录,SWIFT推送之本地推送
分类:编程

SWIFT推送之本地推送(UILocalNotification)

本地推送通知是通过实例化UILocalNotification实现的。要实现本地化推送可以在AppDelegate.swift中添加代码实现,本事例是一个当App进入后台时推送一条消息给用户。

1.首先在didFinishLaunchingWithOptions方法内添加代码,IOS8推送消息首先要获得用户的同意,在初次安装App时会提示用户是否允许程序推送消息,此方法是App第一次运行的时候被执行一次,每次从后台激活时不执行该方法.

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

        if (UIDevice.currentDevice().systemVersion as NSString).floatValue >= 8 {

            //APService.registerForRemoteNotificationTypes(

                //UIUserNotificationType.Badge.rawValue |

                //UIUserNotificationType.Sound.rawValue |

                //UIUserNotificationType.Alert.rawValue,

                //categories: nil)

            

            application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes:

                UIUserNotificationType.Badge |

                UIUserNotificationType.Sound |

                UIUserNotificationType.Alert, categories: nil))

        

        }

        

        APService.setupWithOption(launchOptions)

        return true

    }

 2.有几个方法要说一下,

   1.func applicationWillResignActive(application: UIApplication){} 当App既将进入后台、锁屏、有电话进来时会触发此事件

   2.func applicationDidEnterBackground(application: UIApplication) {} 当App进入后台时触发此事件

   3.func applicationWillEnterForeground(application: UIApplication) {} 当App从后台即将回到前台时触发此事件

   4.func applicationDidBecomeActive(application: UIApplication) {}当App变成活动状态时触发此事件

   5.func applicationWillTerminate(application: UIApplication) {} 当App退出时触发此方法,一般用于保存某些特定的数据

此时在applicationDidEnterBackground方法内写入以下代码:

func applicationDidEnterBackground(application: UIApplication) {

        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.

        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

        UIApplication.sharedApplication().cancelAllLocalNotifications()

        

        var notification = UILocalNotification()

        //notification.fireDate = NSDate().dateByAddingTimeInterval(1)

        //setting timeZone as localTimeZone

        notification.timeZone = NSTimeZone.localTimeZone()

        notification.repeatInterval = NSCalendarUnit.CalendarUnitDay

        notification.alertTitle = "This is a local notification"

        notification.alertBody = "Hey,It‘s great to see you again"

        notification.alertAction = "OK"

        notification.soundName = UILocalNotificationDefaultSoundName

        //setting app‘s icon badge

        notification.applicationIconBadgeNumber = 1

        

        var userInfo:[NSObject : AnyObject] = [NSObject : AnyObject]()

        userInfo["kLocalNotificationID"] = "LocalNotificationID"

        userInfo["key"] = "Attention Please"

        notification.userInfo = userInfo

        

        //UIApplication.sharedApplication().scheduleLocalNotification(notification)

        //UIApplication.sharedApplication().presentLocalNotificationNow(notification)

        application.presentLocalNotificationNow(notification)

    }

 此时将按Home键将App切换到后台时会有一条推送消息,App角标变为了“1”

 

 

3.当用户点击消息时会触发didReceiveLocalNotification事件,在这个事件内写些代码:

func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {

        UIApplication.sharedApplication().cancelAllLocalNotifications()

        let userInfo = notification.userInfo!

        let title = userInfo["key"] as! String

        

        var alert = UIAlertView()

        alert.title = title

        alert.message = notification.alertBody

        alert.addButtonWithTitle(notification.alertAction!)

        alert.cancelButtonIndex = 0

        alert.show()

            

        //APService.showLocalNotificationAtFront(notification, identifierKey: nil)

    }

 

4.当程序处于活动状态的时候清除ICON的角标

func applicationDidBecomeActive(application: UIApplication) {

        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

        //setting the desk top application icon‘s badge as zero

        //application.applicationIconBadgeNumber = 0

        application.cancelAllLocalNotifications()

        application.applicationIconBadgeNumber = 0

        

    }

python安装:

Swift是苹果于2014年发布的新开发语言,运行于Mac OS和iOS平台,开发基于苹果平台的应用程序。在使用Swift语言的时候,我们经常会被 as、as!、as?三种类型转换操作符混淆,这里总结了三种操作符的用法。

1、下载python,版本号3.6以上,本次学习使用3.6.3,操作系统winduws7 64位

1. as

2、安装步骤:

(1)从派生类转换为基类,向上转型(upcasts)

  1)选中“Add Python 3.6 to PATH”,进行安装

// 定义人员基类
class Person {
    var name : String

    init(_ name: String){
        self.name = name
    }
}

// 定义学生类
class Student : Person {
}

// 定义教师类
class Teacher : Person {
}

// 处理人员对象的函数(或工厂模式处理操作等)
func showPersonName(_ people : Person){
    let name = people.name
    print("这个人的名字是: (name)")
}

// 定义一个学生对象 tom
var tom = Student("Tom");

// 定义一个教师对象 kevin
var kevin = Student("Kevin Jakson");

// 先把学生对象向上转型为一般的人员对象
let person1 = tom as Person
let person2 = kevin as Person

// 再调用通用的处理人员对象的showPersonName函数
showPersonName(person1)
showPersonName(person2)

图片 1

运行结果:
这个人的名字是: Tom
这个人的名字是: Kevin Jakson

  2)安装过程如下: 

(2)消除二义性,数值类型转换

本文由澳门新葡亰手机版发布于编程,转载请注明出处:python学习记录,SWIFT推送之本地推送

上一篇:没有了 下一篇:Swift基础语法,的一些新特性
猜你喜欢
热门排行
精彩图文