0
当手表应用程序变为活动状态时,无论iOS应用程序是否处于活动状态,都有一个应用程序需要共享文件,然后是用户信息词典。从iPhone触发手表的最佳方式是什么?通过WatchConnectivity传递手表应用程序上的文件和用户信息的最简单方法
当手表应用程序变为活动状态时,无论iOS应用程序是否处于活动状态,都有一个应用程序需要共享文件,然后是用户信息词典。从iPhone触发手表的最佳方式是什么?通过WatchConnectivity传递手表应用程序上的文件和用户信息的最简单方法
UserDefaults只在WatchOS 1中不在最新的WatchOS中。
您可以通过启用两个应用程序组的Capabilites分享您的“用户信息”,看目标并通过Userdefaults共享目标(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 Side
// InterfaceController.swift
// WatchKit Extension
import WatchKit
import Foundation
import WatchConnectivity
类InterfaceController: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
类的ViewController:以下文件的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/shrawan2015/Application-Demo-StackOverFlow/tree/master/WatchOS
在WatchOS 1你可以,但不是2或3 –
根据有没有这样的限制。 https://developer.apple.com/library/content/documentation/General/Conceptual/WatchKitProgrammingGuide/SharingData.html – Shrawan
我的坏,我们不能用于观看OS 2。 – Shrawan