NotificationCenter(实战)
来源:互联网 发布:达内java培训视频 2016 编辑:程序博客网 时间:2024/05/14 00:28
下面将介绍使用NotificationCenter库实现如下效果图
需求如下:
- 显示TodayExtension;
- 点击Button按钮打开主APP;
- 和主APP共享数据
1 显示TodayExtension
1.1 创建项目
和常规创建项目一样,你也可以使用你目前的项目,这里不做详细说明。
1.2 创建Today Extension
打开项目后,点击Editor-Add Target
如下所示
运行即可看见效果图。
2 点击Button按钮打开主APP
修改布局文件,添加button按钮,并修改源代码,如下。
//// TodayViewController.swift// TodayExtension//// CSDN:http://blog.csdn.net/y550918116j// GitHub:https://github.com/937447974/Blog//// Created by yangjun on 16/1/19.// Copyright © 2016年 阳君. All rights reserved.//import UIKitimport NotificationCenterclass TodayViewController: UIViewController, NCWidgetProviding { override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view from its nib. // 隐藏或显示界面 NCWidgetController.widgetController().setHasContent(true, forWidgetWithBundleIdentifier: YJUtilsAPP.identifier) print(YJUtilsAPP.identifier) // 调整显示区域 self.preferredContentSize = CGSizeMake(self.view.frame.width, 50) print(self.view.frame) } @IBAction func onClickButton(sender: AnyObject) { print(__FUNCTION__) } // MARK: - NCWidgetProviding // MARK: 当前状态 func widgetPerformUpdateWithCompletionHandler(completionHandler: ((NCUpdateResult) -> Void)) { // Perform any setup necessary in order to update the view. // If an error is encountered, use NCUpdateResult.Failed // If there's no update required, use NCUpdateResult.NoData // If there's an update, use NCUpdateResult.NewData print(__FUNCTION__) completionHandler(NCUpdateResult.NewData) } // MARK: 扩展显示边缘 func widgetMarginInsetsForProposedMarginInsets(defaultMarginInsets: UIEdgeInsets) -> UIEdgeInsets { print(__FUNCTION__) print(defaultMarginInsets) return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0) }}
在onClickButton方法添加如下代码即可跳转
// 打开当前应用if let url = NSURL(string: "appextension://test") { self.extensionContext?.openURL(url, completionHandler: { (success: Bool) -> Void in print("open url result: \(success)") })}
其url路径可如图添加
在主APP可监听打开的请求,添加如下方法即可。
// MARK: - 能否打开应用func application(app: UIApplication, openURL url: NSURL, options: [String : AnyObject]) -> Bool { print(url) print(options) return true}
3 共享数据
共享数据可运用App Groups。开启App Groups如下所示。
接下来即可使用NSUserDefaults同步数据,核心代码如下所示。
// 数据同步if let user = NSUserDefaults(suiteName: "group.com.YJNotificationCenter") { print(user.objectForKey("test")) user.setObject("阳君测试", forKey: "test") print(user.synchronize())}
Appendix
Sample Code
Swift
Related Documentation
Notification Center Framework Reference
Revision History
Copyright
CSDN:http://blog.csdn.net/y550918116j
GitHub:https://github.com/937447974/Blog
0 0
- NotificationCenter(实战)
- NotificationCenter
- NotificationCenter
- NotificationCenter
- Unity3D NotificationCenter
- NotificationCenter(简介)
- cocos2d-x NotificationCenter
- NotificationCenter的基本使用
- NotificationCenter观察模式
- NotificationCenter、KVO、Delegate
- 本地通知 NotificationCenter
- NotificationCenter KVC KVO Delegate 区别
- Cocos2d-x-------------------------------消息机制NotificationCenter
- Swift: NotificationCenter 用法 --- swift3.0
- poco库学习笔记(10) Poco::NotificationCenter
- Cocos2dx NotificationCenter(广播或通告)的使用方法
- cocos2dx EventListenerCustom 和NotificationCenter的优缺点
- Swift-通知中心(NotificationCenter)的使用
- Python第三方模块
- error C2664: “int CWnd::GetWindowTextW(LPTSTR,int) const”: 不能将参数 1 从“char [15]”转换为“LPTSTR”
- 合唱队
- 深入理解JVM:OutOfMemory实战
- nginx配置https方法
- NotificationCenter(实战)
- spring的事务传播
- tp二维码
- SSO - CAS 4.0 入门实战
- Android循环广告的实现
- UIWebView与JS交互
- Asp.net MVC 4 异步方法
- 面试笔试 ----- YY Java/Android(未通过)
- 关于Web界面查看日志的权限问题