NotificationCenter(实战)

下面将介绍使用NotificationCenter库实现如下效果图

需求如下:

  1. 显示TodayExtension;
  2. 点击Button按钮打开主APP;
  3. 和主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 UIKit
import NotificationCenter

class 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

Notification Center Framework Reference

Revision History

时间描述
2016-01-20博文完成

CSDN:http://blog.csdn.net/y550918116j

GitHub:https://github.com/937447974/Blog

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值