Recently, I am working on a project involving iPhone devices and watches transmitting data and controlling the jump of each other's interface. I have found a lot of information online and found that domestic websites do not introduce much in this regard, and foreign websites are not very complete. So I am writing this article here for reference. I hope the master will give me some advice.
There is no need to say more about pairing iPhone and iWatch. Baidu search has a lot of answers, and this is the premise.
There are two situations in which the code interacts with the iPhone and the iWatch depends on the iWatch system. iWatch OS1 is different from OS2 and 3. In OS1 system, the code for iWatch to send data is as follows
let userInfo:[String:String] = ["key":"value"] (userInfo) { (replyInfo, error) -> Void in }
This function is to send a message and also to receive a message. The reply received from the iPhone is replyInfo. In AppDelegate on the iPhone, the code to receive the message:
func application(application: UIApplication, handleWatchKitExtensionRequest userInfo: [NSObject : AnyObject]?, reply: (([NSObject : AnyObject]!) -> Void)!) { }
The received message is userInfo, and the data replied to the iWatch is reply.
The above code is only applicable to watchOS1, and the system afterwards does not have this API. For OS2 or OS3, the framework used is WatchConnectivity. Here, I will directly provide the classes I wrote and give an introduction and usage method. You can copy the code directly and write your own functions in the comments.
The following is the code on the iPhone:
import UIKit import WatchConnectivity class IwatchSessionUtil: NSObject, WCSessionDelegate { // Static single case static let shareManager = IwatchSessionUtil() //initialization private override init() { () } // Connection mechanism private let session:WCSession? = () ? () : nil // Activate the mechanism object func startSession(){ session?.delegate = self session?.activate() } // Watch app detected func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) { print("AppleWatch matching is completed") } // Start passing data to Watch func sessionDidBecomeInactive(_ session: WCSession) { } // The data has been passed func sessionDidDeactivate(_ session: WCSession) { } // The watch side sends data over, the iPhone receives the data and replys to the data over // message: message sent from the watch side // replyHandler: iPhone reply past information func session(_ session: WCSession, didReceiveMessage message: [String : Any], replyHandler: @escaping ([String : Any]) -> Void) { // Here, we receive the data sent by the watch, and can use a proxy, code block or notification center to pass the value to the ViewController to perform a series of operations. // Note! ! : The message sent by the watch side, and the iPhone reply directly reply reply replyHandler([String: Any]) (replyHandler(data)) in this function, so that the reply corresponding to the function sending data on the watch side can receive the data. Don't be confused with the sendMessage function. If you reply with sendMessage, the message received by the watch side is the didReceiveMessage function. } // iPhone sends data to watch // key: the key value of the data // value: data content func sendMessageToWatch(key:String,value:Any) { session?.sendMessage([key : value], replyHandler: { (dict:Dictionary) in // This is the operation after sending data, such as writing an alert prompts that the send is successful // replyHandler is the content reply after receiving the information on the didReceiveMessage function on the watch side. Here you can edit the function you need }, errorHandler: { (Error) in // Send failed, usually because Bluetooth is not turned on, or the phone is turned on in flight mode }) } }
Calling method:
1. First add code to the didFinishLaunchingWithOptions function of iPhone AppDelegate
(), make sure that WCSession can match the App on the watch side
2. Send a message: just call the method (key: , value: ). After sending, the operation after receiving the reply from the watch side is directly edited in the sendMessage function.
3. The sendMessage on the watch side sends information to the iPhone, and the didReceiveMessage on the iPhone side receives the information, and a series of operations have been commented on it.
After the introduction of the iPhone end, the code on the watch end is written below. In fact, there is no difference between it and the iPhone. This is just to write this part of the content completely.
import WatchKit import WatchConnectivity class WatchSessionUtil: NSObject,WCSessionDelegate { // Static single case static let sharedManager = WatchSessionUtil() // Initialization private override init() { () } // Connection mechanism private let session:WCSession? = () ? () : nil // Activation mechanism func startSession(){ session?.delegate=self session?.activate() } // iPhone's parent app was detected func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) { } // Receive the message sent from the iPhone // message: The message sent from the iPhone // replyHandler: The contents reply to iPhone by the watch side func session(_ session: WCSession, didReceiveMessage message: [String : Any], replyHandler: @escaping ([String : Any]) -> Void) { // Here you can also send notifications to the InterfaceController through the notification center to perform page operations. As for what method to use, you can do whatever you want. Note: The code of iPhone mentioned that the same nature is not written here. } // Send information to the iPhone side func sendMessage(key:String, value:Any){ session?.sendMessage([key : value], replyHandler: { (reply: [String : Any]) in // After the information is sent, the operation of receiving a reply from the iPhone }, errorHandler: { (Error) in // Send failed }) } }
Add the watch class to the Extension folder and call the method:
1. Write () in the applicationDidFinishLaunching function of the ExtensionDelegate file
2. Send a message: just call the method (key: , value: ). After sending, the operation after receiving the reply from the iPhone side is directly edited in the sendMessage function.
3. The sendMessage on the iPhone side sends information to the watch, and the didReceiveMessage on the watch side receives information, and a series of operations have been commented on it.
The above is the method of connecting, controlling, and data transmission (Swift) between iPhone and iWatch introduced to you. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support for my website!