iOS项目开发实战——开发基于Handoff的App(Swift)

        iOS8推出一个新特性,叫做Handoff。Handoff中文含义为换手(把接力棒传给下一个人),可以在一台Mac和iOS设备上开始工作,中途将工作交换到另一个Mac或iOS设备中进行。这个在iOS8和Yosemite得到支持,当然在iOS9和EI Captain中也有这个功能。

     Handoff中使用了一个“活动”的概念,它可以在多台设备间传递用户的“活动”。当用户从一台设备切换到另一台设备后,用户完全可以不中断原有的“活动”,也不需要重新配置应用程序。在这个过程中,Handoff将自动在iOS设备和Mac间保持应用程序的同步。

       本应用实现了在两台iOS设备之间的协同工作。当在一台iOS设备上运行该应用时,打开另一台安装该应用的iPhone,可以快速看到同样的显示内容。可以快速实现多台设备的同步,提高了工作效率。这里我将通过Swift来实现。项目我已经上传 https://github.com/chenyufeng1991/Handoff  。

(1)创建一个iOS项目,首先在Info.plist文件中加入一个字段,NSUserActivityTypes,值可以任意,但是要唯一,我这里是:com.chenyufengweb.HandoffDemo.sync。到时候我会在代码中用到。

(2)在AppDelegate.swift中实现一个方法如下:

  func application(application: UIApplication, continueUserActivity userActivity: NSUserActivity, restorationHandler: ([AnyObject]?) -> Void) -> Bool {
    print("AppDelegate -- continueUserActivity")
    let topViewController = self.window?.rootViewController as! AnyObject
    restorationHandler([topViewController])
    
    return true
  }

(3)在ViewController.swift中实现如下:

import UIKit

class ViewController: UIViewController,NSUserActivityDelegate {
  
  @IBOutlet weak var textField: UITextField!
  @IBOutlet weak var switchButton: UISwitch!
  
  var str:String!
  var isOn:String!
  
  override func viewDidLoad() {
    super.viewDidLoad()
    
    let myActivity:NSUserActivity! = NSUserActivity(activityType: "com.chenyufengweb.HandoffDemo.sync")
    
    str = textField.text
    isOn = switchButton.on ? "on" : "off"
    
    //定义一个字典;
    let items = ["text":str,"switch":isOn]
    
    //配置活动参数;
    myActivity.userInfo = items
    myActivity.title = "sync"
    myActivity.becomeCurrent()
    myActivity.delegate = self
    myActivity.needsSave = true
    
    self.userActivity = myActivity
    
    //以代码的方式进行消息响应;
    //    self.textField.addTarget(self, action: "textFieldDidChange:", forControlEvents: UIControlEvents.ValueChanged)
    //    self.switchButton.addTarget(self, action: "onChanged", forControlEvents: UIControlEvents.ValueChanged)
    
  }
  
  func textFieldDidChange() -> Void{
    
    print("Text changed")
    self.userActivity?.becomeCurrent()
    self.userActivity?.needsSave = true
    
  }
  
  func onChanged() -> Void{
    
    print("Switch changed")
    self.userActivity?.becomeCurrent()
    self.userActivity?.needsSave = true
    
  }
  
  //以segue的方式进行控件响应,个人比较喜欢;
  @IBAction func textFieldDidChange(sender: AnyObject) {
    
    print("Text changed")
    self.userActivity?.becomeCurrent()
    self.userActivity?.needsSave = true
    
  }
  
  
  @IBAction func onChanged(sender: AnyObject) {
    
    print("Switch changed")
    self.userActivity?.becomeCurrent()
    self.userActivity?.needsSave = true
    
  }
  
  
  //MARK: - Handoff
  /*
  注意:userActivityWillSave()方法和userActivityWasContinued()这两个方法在当前进行操作的设备A上执行;
  
  
  restoreUserActivityState()方法在打开另一个设备B上执行;
  */
  
  //把当前的数据存储到字典中;
  func userActivityWillSave(userActivity: NSUserActivity) {
    print("ViewController userActivityWillSave")
    
    str = textField.text
    isOn = switchButton.on ? "on" : "off"
    
    let items = ["text" : str,"switch" : isOn]
    userActivity.addUserInfoEntriesFromDictionary(items)
    
    print("userActivityWillSave:\(str),  \(isOn)")
    
  }
  
  
  
  func userActivityWasContinued(userActivity: NSUserActivity) {
    print("ViewController userActivityWasContinued")
//    
//    let dictionary:NSDictionary = (userActivity.userInfo)!
//    let textForKey = dictionary.objectForKey("text") as! String
//    let switchForKey = dictionary.objectForKey("switch") as! String
//    print("text = \(textForKey),switch = \(switchForKey)")
//    
  }
  
  
  //从字典中读出数据;
  override func restoreUserActivityState(activity: NSUserActivity) {
    print("ViewCOntroller restoreUserActivityState")
    let dictionary:NSDictionary = (activity.userInfo)!
    let textForKey = dictionary.objectForKey("text") as! String
    let switchForKey = dictionary.objectForKey("switch") as! String
    print("text = \(textForKey),switch = \(switchForKey)")
    
    self.textField.text = textForKey
    
    if (switchForKey == "on"){
      
      self.switchButton.on = true
      print("ononononon")
      
    }else{
      
      self.switchButton.on = false
      print("offoffoffoffoff")
      
    }
    
  }
  
}

(4)运行效果如下:


iOS项目开发实战——开发基于Handoff的App(Swift)_第1张图片


iOS项目开发实战——开发基于Handoff的App(Swift)_第2张图片



github主页:https://github.com/chenyufeng1991  。欢迎大家访问!

你可能感兴趣的:(ios,handoff)