摘要:是专门供程序中不同类间的消息通信的。使用它为我们代码降低耦合。
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
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/16156.html
摘要:手势事件点击手势拖动手势缩放手势擦碰手势旋转手势长按手势添加和移除手势下面代码简单演示各个手势用法点击手势点击次数捏手势旋转手势滑动手势默认为右滑拖动手势与滑动手势冲突,二者选其一长按手势放大缩小状态变化触控事件通 手势事件 UITapGestureRecognizer(点击手势) UIPanGestureRecognizer(拖动手势) UIPinchGestureRecogniz...
摘要:需要设置如下是个第三方的语言的网络开发工具包,基于,但是更娇便于使用。 NSURLSession var imageView = UIImageView(frame: CGRectMake(40, 40, 200, 200)) var curTime = NSDateFormatter() override func viewDidLoad() { super.viewDid...
摘要:下面例子来示范使用相机拍照并显示出我们的照片。分别绑定然后添加下面代码到,来使用相机拍照先要判断相机是否可用允许拍摄图片后编辑继承,为我们实现了拍照后的响应分别是点击完成后处理照片,点击取消。 UIImagePickerController 是系统提供的用来获取图片和视频的接口。 下面例子来示范使用相机拍照并显示出我们的照片。 先设置ViewController继承UIImagePic...
摘要:检测网络状态可以用的例子里的类,拷贝到我们的项目里,然后导给用。另外,上也有个版本的将里面的扒过来,加入我们的工程中。然后如下使用连接类型连接类型移动网络连接类型没有网络连接当前连接为当前连接为蜂窝数据当前无连接 检测网络状态可以用Apple-Reachability的例子里的Reachability类(objective-c),拷贝到我们的项目里,然后导给Swift用。 另外,git...
摘要:默认情况下,每个沙盒含有个文件夹和。创建文件或创建文件夹文件检查移动重命名拷贝目录下所有文件会遍历到所有子目录删除文件 FileManager IOS应用只能访问自己应用目录下的文件。默认情况下,每个沙盒含有3个文件夹:Documents, Library 和 tmp。Library包含Caches、Preferences目录。 let homeDir = NSHomeDirector...
阅读 2288·2023-04-25 17:37
阅读 881·2021-11-24 10:29
阅读 3074·2021-11-17 09:33
阅读 1852·2021-09-26 09:47
阅读 3416·2021-09-09 11:57
阅读 522·2021-08-10 09:41
阅读 2029·2019-08-30 15:55
阅读 2616·2019-08-30 15:54