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