NSNotificationCenter
NSNotificationCenter是专门供程序中不同类间的消息通信的。使用它为我们代码降低耦合。
自定义数据监听
注册监听:
// addObserver 4个参数分别是:接受者对象,接受者处理函数,消息名称,发送者对象(通常设为nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(downloadImage), name: "NotificationName", object: nil)
移除监听:
NSNotificationCenter.defaultCenter().removeObserver(self)
监听函数:
func downloadImage(notification: NSNotification) {
let userInfo = notification.userInfo as! [String: AnyObject]
let value1 = userInfo["val1"] as! String
let value2 = userInfo["val2"] as! Int
// ...
}
发送消息:
NSNotificationCenter.defaultCenter().postNotificationName("NotificationName",
object: self, userInfo: ["val1":"msg1", "val2" : 123])
默认监听
addObserverForName监听方法
let operationQueue = NSOperationQueue.mainQueue()
// queue必须是处理队列NSOperationQueue,usingBlock是响应消息的函数闭包
NSNotificationCenter.defaultCenter().addObserverForName("NotificationName2", object: nil, queue: operationQueue, usingBlock: { /*...*/ })
postNotificationName
系统会发送很多消息,如:
UIApplicationDidEnterBackgroundNotification
UIApplicationWillEnterForegroundNotification
UIApplicationDidFinishLaunchingNotification
...
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(onKeyboardWillShow), name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(onKeyboardWillHide), name: UIKeyboardWillHideNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(onEnterBackground), name: UIApplicationWillResignActiveNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(onEnterForeground), name: UIApplicationDidBecomeActiveNotification, object: nil)
发送不带数据的消息:
NSNotificationCenter.defaultCenter().postNotificationName("NotificationName2", object: self)
发送本地通知栏通知
注册用户消息设置
可以设置在AppDelegate的didFinishLaunchingWithOptions内部
let settings = UIUserNotificationSettings(forTypes: [.Sound, .Alert, .Badge], categories: nil)
application.registerUserNotificationSettings(settings)
这样,安装后首次进入应用系统会提示是否允许接收通知。
发送消息
获取一个UILocalNotification
发送:UIApplication.sharedApplication().presentLocalNotificationNow(notification)
let notification = UILocalNotification()
notification.fireDate = NSDate().dateByAddingTimeInterval(3) // 延迟3秒发送消息
notification.timeZone = NSTimeZone.localTimeZone()
notification.repeatInterval = NSCalendarUnit.Minute // 设置每分钟重复一次
notification.alertTitle = "This is a notification title"
notification.alertBody = "This is a notification body"
notification.alertAction = "OK"
notification.soundName = UILocalNotificationDefaultSoundName // 默认提示音
notification.applicationIconBadgeNumber = 1 // 应用Icon的悬浮数字
// 使用userInfo数据
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(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification)
我们就在下面写我们的处理逻辑
print("didReceiveLocalNotification \(notification.alertTitle)")
let userInfo = notification.userInfo!
let title = userInfo["key"] as! String
let alert = UIAlertController(title: title, message: notification.alertBody, preferredStyle: UIAlertControllerStyle.Alert)
alert.title = title
alert.message = notification.alertBody
let okAction = UIAlertAction(title: "OK!!!", style: UIAlertActionStyle.Default, handler: nil)
alert.addAction(okAction)
self.window?.rootViewController?.presentViewController(alert, animated: true, completion: nil)
清除Icon的通知数字
我们在应用激活状态,就去清除掉数字
应用别激活就会调用:
applicationDidBecomeActive(application: UIApplication)
application.cancelAllLocalNotifications()
application.applicationIconBadgeNumber = 0
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。