2016-11-15 63 views

回答

-2

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

+0

在WatchOS 1你可以,但不是2或3 –

+0

根據有沒有這樣的限制。 https://developer.apple.com/library/content/documentation/General/Conceptual/WatchKitProgrammingGuide/SharingData.html – Shrawan

+0

我的壞,我們不能用於觀看OS 2。 – Shrawan

相關問題