iPhone开发 Swift - NSNotification 通知

Swift创建Notification通知

  1. 创建一个SingleView Application
  2. 打开AppDelegate.swift,在方法

application(application:UIApplication,didFinishLaunchingWithOptions launchOptions: NSDictionary?)

中输入代码:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {

//设置Notification 的类型

let types:UIUserNotificationType = UIUserNotificationType.Alert | UIUserNotificationType.Badge

//设置Notification的设置项,其中categories参数用来设置Notification的类别

let mySettings: UIUserNotificationSettings = UIUserNotificationSettings(forType: types, categories: nil);

//注册UserNotification

UIApplication.sharedApplication().registerUserNotifiationSettings(mySettings)

return true

}

  1. 配置不同的Actions,在方法

application(application:UIApplication,didFinishLaunchingWithOptions launchOptions: NSDictionary?)内的代码开始部分输入以下代码

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {

//define Actions

var firstAction: UIMutableUserNotificationAction = UIMutableUserNotificationAction();

// The unique identifier for this action

fistAction.identifier = “FIRST_ACTION”;

// The localized title to display for this action

firstAction.title = “First Action”;

//define action’s activationMode, // How the application should be activated in response to the action

firstAction.activationMode = UIUserNotificationActivationMode.Background// 当点击的时候不启动程序,在后台处理

//define action’s destructive // Whether this action should be indicated as destructive when displayed.

firstAction.destructive = true

//define authentication // Whether this action is secure and should require unlocking before being performed. If the activation mode is UIUserNotificationActivationModeForeground, then the action is considered secure and this property is ignored.

firstAction.authenticationRequired = false//不需要用户解锁手机即可以处理该Notification

var secondAction:UIMutableUserNotificationAction = UIMutableUserNotificationAction()

secondAction.identifier = "SECOND_ACTION";

secondAction.title = "Second Action";

secondAction.activationMode = UIUserNotificationActivationMode.Foreground

secondAction.destructive = false

secondAction.authenticationRequired = false

var thirdAction:UIMutableUserNotificationAction = UIMutableUserNotificationAction()

thirdAction.identifier = "THIRD_ACTION";

thirdAction.title = "Third Action";

thirdAction.activationMode = UIUserNotificationActivationMode.Background

thirdAction.destructive = false

thirdAction.authenticationRequired = false

//Category

var firstCategory: UIMutableUserNotificationCategory = UIMutableUserNotificationCategory()

firstCategory.identifier = "FIRST_CATEGORY";

let defaultActions:NSArray = [firstAction, secondAction, thirdAction];

let minimalActions:NSArray = [firstAction, secondAction];

// Sets the UIUserNotificationActions in the order to be displayed for the specified context

firstCategory.setActions(defaultActions, forContext: UIUserNotificationActionContext.Default);// // the default context of a notification action

firstCategory.setActions(minimalActions, forContext: UIUserNotificationActionContext.Minimal);//Minimal // the context of a notification action when space is limited

let categories: NSSet = NSSet(objects: firstCategory)

//设置Notification 的类型

let types:UIUserNotificationType = UIUserNotificationType.Alert | UIUserNotificationType.Badge

//设置Notification的设置项,其中categories参数用来设置Notification的类别

let mySettings: UIUserNotificationSettings = UIUserNotificationSettings(forType: types, categories: nil);

//注册UserNotification

UIApplication.sharedApplication().registerUserNotifiationSettings(mySettings)

return true

}

4. 打开ViewController.swift文件,在ViewDidLoad方法中输入以下代码

override func viewDidLoad() {

super.viewDidLoad()

//define notification center

NSNotificationCenter.defaultCenter().addObserver(self, selector: “TestShape:”, name: “actionOne”, object: nil)

NSNotificationCenter.defaultCenter().addObserver(self, selector:”TestMessage:”, name: “actionTwo”, object: nil)

//定义一个触发Notification的程序
var dateComp: NSDateComponents  =
NSDateComponents()

dateComp.year = 2014

dateComp.month = 09

dateComp.day = 09

dateComp.hour = 11

dateComp.minute = 11

dateComp.timeZone = NSTimeZone.systemTimeZone()

var calendar: NSCalendar = NSCalendar(calendarIdentifier:
NSGregorianCalendar)

var date: NSDate = calendar.dateFromComponents(dateComp)

//define Location notification

var notification: UILocalNotification =
UILocalNotification()

notification.category = “FIRST_CATEGORY”;

notification.alertBody = “This is a notification”

notification.fireDate = date

//fire notification

UIApplication.shareApplication().scheduleLocalNotification(notification)

}

func TestShape(notification: NSNotification) {

UIView *view = UIView(frame:
CGRectMake(100,100,100,100));

view.backgroundColor = UIColor.blackColor()

Self.view.addSubview(view)

}

func TestMessage(notification: NSNotification) {

var message:
UIAlertController = UIAlertController(title: “Notification Message”, message: “Hello,
this is an alert message”, preferredStyle: UIAlertControllerStyle.Alert)

message.addAction(UIAlertAction(title: “OK”, style:
UIAlertActionStyle.Default, handle: nil))

self.presentViewController(message, animated: true,
completion: nil)

}

5. 回到AppDelegate.swift,并添加以下方法

func
application(application: UIApplication!, handleActionWithIdentifier identifier: String!,
forLocalNotification notification: UILocalNotification!, completionHandler: (() -> Void)!) {

if identifier ==
“FIRST_ACTION” {

NSNotificationCenter.defaultCenter().postNotificationName(“actionOne”,
object: nil)

}

else if identifier == “SECOND_ACTION” {

NSNotificationCenter.defaultCenter().postNotificationName(“actionTwo”,
object: nil)

}

completionHandler()

}

iPhone开发 Swift - NSNotification 通知,布布扣,bubuko.com

时间: 2024-08-05 16:46:52

iPhone开发 Swift - NSNotification 通知的相关文章

iphone开发 IOS 组织架构图

转载自 :http://blog.csdn.net/mashi321323/article/details/18267719 登录|注册     mashi321323的专栏 目录视图 摘要视图 订阅 10月28日 大牛带你玩转Spark    微信开发学习路线高级篇上线    免费公开课平台正式上线啦    恭喜July新书上市 iphone开发 IOS 组织架构图 分类: iphone2014-01-14 17:20 1870人阅读 评论(0) 收藏 举报 iphone开发组织架构 目录(?

swift中通知的使用

1.发通知.(以这条通知为例,通知名字:gameOverNotification.通知参数:title) NSNotificationCenter.defaultCenter().postNotificationName("gameOverNotification", object: title) 2.在要监听这则通知的viewDidload方法里面添加观察者,以便监听这则通知 NSNotificationCenter.defaultCenter().addObserver(self,

iPhone开发常问的十个问题

iPhone开发常问的十个问题 前言 今天去stackoverflow.com上看了一下iPhone标签下排名最高的10个问题,将它们整理出来,希望这些常见问题能帮到一些iPhone开发的初学者.本来想把答案也翻译过来的,后来发现答案资料通常都比较复杂,翻译起来太麻烦.所以大家还是看英文的答案吧,我只顺带用中文总结一下答案. 问题一: 有哪些iPhone开发和Objective-C的入门资料 这个确实是最常问的问题了.对于我个人来说,入门时所看的资料主要是<From C++ to Objecti

swift中通知的使用方法

其实swift语言和OC语言,在本质上都是一样,其里面的方法之类的也基本相同.通知的使用方法也是一样,只是代码的书写格式发生了改变而已.下面我通过一个简单的小需求,也讲一讲通知,用swift中的闭包,也能完成此功能. 使用通知需要注意事项: 1,先确保接收中心存在,在设置通知中心. 2,最后一定要移除通知中心. 3,通知也是可以传值的,放在userInfo里面. 具体界面效果,我在这里就不截图了,希望各位开发者,自己写一遍,然后运行. ViewController // //  ViewCont

iPhone 开发概述

### iPhone开发概述 沙盒机制( sandbox) iOS 中的沙盒机制( Sandbox) 是一种安全体系,它规定了应用程序只能在为该应用程序创建的文件夹内读取文件,不可以访问其他地方的内容.所有的非代码文件都保存在这个地方,比如图片.声音.属性列表和文本文件等. 每一个应用程序都在自己的沙盒内 不能随意跨越自己的沙盒去访问别人的沙盒的内容 应用程序向外请求或者接收数据都需要经过权限认证 应用程序的五种状态 Not runing( 未运行) 程序没启动 Inactive (未激活) 程

让编程菜鸟爱上iPhone开发1-Swift+iOS8版

对iPhone开发感兴趣的初学童鞋们奉上一篇认可度较高的iPhone开发教程,助你轻轻松松提高自己的职业修养. 提醒:本文是系列教程中的开篇,不适合程序猿出身的童鞋!目的只是让初学者.产品策划,设计或管理人员了解iPhone开发的基础知识. 主要素材和示例项目来源:Ray Wenderlich store 特别说明:不适合程序猿出身的童鞋!目的是让初学者.产品策划,设计或管理人员了解iPhone开发的基础知识. 适合看本系列教程的对象: 1.从未学过编程,或者对Swift语言一无所知,但要懂一些

深入理解iPhone数据持久化(手把手教你iphone开发 – 基础篇)

在所有的移动开发平台数据持久化都是很重要的部分:在j2me中是rms或保存在应用程序的目录中,在symbian中可以保存在相应的磁盘目录中和数据库中.symbian中因为权限认证的原因,在3rd上大多数只能访问应用程序的private目录或其它系统共享目录.在iphone中,apple博采众长,提供了多种数据持久化的方法,下面笔者会逐个进行详细的讲解. iphone提供的数据持久化的方法,从数据保存的方式上讲可以分为三大部分:属性列表.对象归档.嵌入式数据库(SQLite3).其他方法. 一.属

iOS开发Swift篇—(一)简单介绍

一.简介 Swift是苹果于2014年WWDC(苹果开发者大会)发布的全新编程语言 Swift在天朝译为“雨燕”,是它的LOGO 是一只燕子,跟Objective-C一样,可以用于开发iOS.Mac应用程序 苹果从2010年7月开始设计Swift语言,耗时4年打造 Swift的语法特点 从它的语法中能看到Objective-C.JavaScript.Python等语言的影子 语法简单.代码简洁.使用方便 可与Objective-C混合使用(相互调用) 为什么要设计Swift语言? 让应用开发更简

iOS/iphone开发如何为苹果开发者帐号APPID续费

原文地址:iOS/iphone开发如何为苹果开发者帐号APPID续费作者:陈双超_群雄 其实相当的简单,这篇内容是给财务看的,有的地方连我自己看了都感觉有点...但如果不详细,她又要为难我,所以我就当她0智商 1.     打开 https://developer.apple.com/account/overview.action 2.(如何查看到自己的证书哪天到期,就是具体日起,这个的话平时是看不到的只有在最后60天的时候会提醒你.) 3.4.5.6.7.为了避免财务输入麻烦或者她输入出错引起