0

有一个应用程序需要在手表应用程序激活时共享文件和用户信息字典,但无论 iOS 应用程序是否处于活动状态。触发从 iPhone 到 Watch 的请求的最佳方式是什么?

4

1 回答 1

-3

UserDefaults 仅在 WatchOS 1 中,不在最新的 WatchOS 中。

您可以通过在应用程序和手表目标上启用组的功能并通过目标(iPhone 和手表)之间的用户默认值共享来共享您的“用户信息” 。

//iPhone sharing Userinfo 
func sharedUserInfo() {

        if let userDefaults =  UserDefaults(suiteName: "group.watch.app.com" ) {
            userDefaults.set( userinfo as AnyObject, forKey: "UserInfo")
            userDefaults.synchronize()
            }

        }


//Watch extracting the info 
 func sharedInfo() {
        if let userDefaults = UserDefaults(suiteName: "group.watch.app.com") {
            let userInfo = userDefaults.string(forKey: "UserInfo")
           }

       }

对于 Watch 连接,我们可以简单地通过以下方式实现:-

 //  Watch Side
 //  InterfaceController.swift
 //  WatchKit Extension

import WatchKit
import Foundation
import WatchConnectivity

类接口控制器:WKInterfaceController,WCSessionDelegate {

@IBOutlet var textLabel: WKInterfaceLabel!

var session:WCSession?

override func awake(withContext context: Any?) {
    super.awake(withContext: context)

    // Configure interface objects here.
}

override func willActivate() {
    // This method is called when watch view controller is about to be visible to user
    super.willActivate()
    checkSupportOfSession()
}

override func didDeactivate() {
    // This method is called when watch view controller is no longer visible
    super.didDeactivate()
}

func checkSupportOfSession() {
    if( WCSession.isSupported() ) {
        self.session = WCSession.default()
        self.session?.delegate = self
        self.session?.activate()
    }
}

func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
    print("session")
}

func session(_ session: WCSession, didReceiveMessage message: [String : Any]) {

    let message:String = message["textIndex"] as! String
    textLabel.setText(message)
    print(message)

}

}

//应用端代码

import UIKit
import WatchConnectivity

类视图控制器:UIViewController,WCSessionDelegate {

@IBOutlet weak var textWord: UITextField!
var session:WCSession?

override func viewDidLoad() {
    super.viewDidLoad()
    checkSupportOfSession()
}

func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
    print("session")
}

func checkSupportOfSession() {
    if( WCSession.isSupported() ) {
        self.session = WCSession.default()
        self.session?.delegate = self
        self.session?.activate()
    }
}



@available(iOS 9.3, *)
public func sessionDidBecomeInactive(_ session: WCSession)
{
    print("sessionS 2")
}

@available(iOS 9.3, *)
public func sessionDidDeactivate(_ session: WCSession){

}

@IBAction func sendTextToWatch(_ sender: Any) {
    print("send text to watch amount")
     if let textName = textWord.text {
        session?.sendMessage(["textIndex" : textName as String], replyHandler: nil, errorHandler: nil)
    }

}

}

https://github.com/srawan2015/Application-Demo-StackOverFlow/tree/master/WatchOS

于 2016-11-23T04:51:59.190 回答