Recently in a project, involving the iphone device and watch transmission data, control each other interface jump, find a lot of information on the Internet, found that the domestic site this is not a lot of introduction, and the foreign site is not very full, so write this blog, for everyone to reference, hope the big God pointing twos.
iphone and iwatch pairing this does not need to say, Baidu search answers a lot, this is the premise.
The iphone interacts with iwatch in two cases, depending on the iwatch system. IWatch OS1 is different from OS2, 3 method, in OS1 system, IWatch send data code as follows
Let userinfo:[string:string] = ["key":"value" in}
This function is to send a message, but also to receive messages, received the iphone reply is replyinfo. In the iphone appdelegate, the code that receives the message:
Func application (application:uiapplication, handlewatchkitextensionrequest userInfo: [Nsobject:anyobject]?, reply: ([[ nsobject:anyobject]!) Void)! ) {}
The message received is UserInfo, and the data returned to iwatch is reply.
The above code only applies to WatchOS1, after the system does not have this API, for OS2 or OS3, the use of the framework is watchconnectivity, here, I will directly provide the class I write, and give the introduction and use of methods, you can directly copy the code, Write your own function in the place where I am commenting.
The following is the iphone code:
Import Uikitimport watchconnectivityclassIwatchsessionutil:nsobject, wcsessiondelegate {//Static single Case StaticLet Sharemanager =Iwatchsessionutil ()//Initialize Private Overrideinit () {super.init ()}//Connection Mechanism PrivateLet session:wcsession? = Wcsession.issupported ()? Wcsession.default(): Nil//Activation Mechanism Objectfunc Startsession () {Session?.Delegate=Self Session?. Activate ()}//Watch side app detectedFunc session (_ Session:wcsession, Activationdidcompletewith activationstate:wcsessionactivationstate, Error:error?) {print ("AppleWatch Match Complete") } //start passing data to watchFunc sessiondidbecomeinactive (_ session:wcsession) {}//data passed .Func Sessiondiddeactivate (_ session:wcsession) {}//Watch side sends data, iphone receives data and responds to data past//information sent over the Message:watch side//Replyhandler:iphone Reply to past informationFunc session (_ Session:wcsession, didreceivemessage message: [String:any], Replyhandler: @escaping ([String:any])-&G TVoid) { //here, we receive the data that watch sends over, can use the proxy, code block or notification center to pass value to Viewcontroller, make a series of operation. //Note!! : The watch side sends the message, the iphone responds directly to the function to reply to Replyhandler ([String:any]) (Replyhandler (data)), so that the watch side of the function to send the data corresponding to the reply to receive data, Don't confuse SendMessage with this function. If you reply with SendMessage, the watch side receives the message is the function of didreceivemessage. } //iphone sends data to watch//key: The key value of the data//Value: Data contentfunc Sendmessagetowatch (key:string,value:any) {session?. SendMessage ([Key:value], Replyhandler: {(dict:dictionary)inch //here is the operation after sending the data, such as writing an alert prompt to send a successful//Replyhandler is the watch side didreceivemessage function received information reply reply to the content, here you can edit the functions you need}, ErrorHandler: {(Error)inch //send failed, usually Bluetooth does not open, or mobile phone opened the flight mode }) }}
Call method: 1, first in the iphone's Appdelegate didfinishlaunchingwithoptions function to add code IwatchSessionUtil.shareManager.startSession () To ensure that the wcsession can match the app on the watch side
2, send the message: Call Method IwatchSessionUtil.shareManager.sendMessageToWatch (key:, value:) can be sent after receiving the watch side of the reply after the operation, Just edit it directly in the class SendMessage function.
3, watch side SendMessage send information to Iphone,iphone side Didreceivemessage received information, a series of operations above has been commented on.
iphone end of the introduction, the following write watch end of the code, in fact, and the iphone is no different, here just to write this part of the content completely.
Import Watchkitimport watchconnectivityclassWatchsessionutil:nsobject,wcsessiondelegate {//Static single Case StaticLet Sharedmanager =Watchsessionutil ()//Initialize Private Overrideinit () {super.init ()}//Connection Mechanism PrivateLet session:wcsession? = Wcsession.issupported ()? Wcsession.default(): Nil//activation mechanismfunc Startsession () {Session?.Delegate=Self Session?. Activate ()}//detected the iphone's parent appFunc session (_ Session:wcsession, Activationdidcompletewith activationstate:wcsessionactivationstate, Error:error?) { } //received the message sent by the iphone .//information sent over the Message:iphone side//Replyhandler:watch-side reply to iphone contentFunc session (_ Session:wcsession, didreceivemessage message: [String:any], Replyhandler: @escaping ([String:any])-&G TVoid) { //here can also send notifications to interfacecontroller through the Notification Center, the page operation, as to what method we are free. Note The iphone's code mentions the same nature, which is not written here. } //send messages to iphone sidefunc sendMessage (key:string, Value:any) {session?. SendMessage ([Key:value], Replyhandler: {(reply: [String:any])inch //after the message is sent, you receive an iphone reply.}, ErrorHandler: {(Error)inch //Send failed }) }}
The watch class is added to the extension folder, calling the method:
1. Write WatchSessionUtil.sharedManager.startSession () in the applicationdidfinishlaunching function of the extensiondelegate file.
2, send the message: Call Method IwatchSessionUtil.shareManager.sendMessageToWatch (key:, Value:) can, send after receiving the iphone side after the reply operation, Just edit it directly in the class SendMessage function.
3, the iphone side SendMessage send information to the Watch,watch side Didreceivemessage received information, a series of operations above has been commented on.
The content is so much, already very full, here is the SWIFT code, OC Code I give a Web site reference: http://blog.csdn.net/shenjie12345678/article/details/61913968
iphone and iwatch connection, control, data transfer (Swift)