WCSessionDelegate

Hello,

how can i send data to apple watch if app is not running. I need the application in apple watch to run with data from iphone as (login, password))

I send data to apple watch when I click the Sign In button. I need the data to be stored in apple watch even if the application in apple watch is not running. as soon as I run it, the data that was received from the Iphone must be used.

my extension in apple watch extension ExtensionDelegate:WCSessionDelegate{

    

    func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {

    }

    

    

    

    func session(_ session: WCSession, didReceiveMessage message: [String : Any]) {

      

      print("received data: (message)")

      if let value = message["iPhone"] as? String {//**7.1

         // self.label.setText("(message)")

          WKuserdefautls.set(value, forKey: "id")

      }

        

        if let valueQR = message["QRPath"] as? String {//**7.1

            //self.label.setText("(message)")

            WKuserdefautls.set(valueQR, forKey: "QRPath")

        }

        

        if let usermail = message["usermail"] as? String {//**7.1

            

            WKuserdefautls.set(usermail, forKey: "usermail")

        }

        

        if let userid = message["userid"] as? String {//**7.1

        

            WKuserdefautls.set(userid, forKey: "userid")

        }

        

        if let userphone = message["userphone"] as? String {//**7.1

       

            WKuserdefautls.set(userphone, forKey: "userphone")

        }

        

        if let username = message["username"] as? String {//**7.1

        

            WKuserdefautls.set(username, forKey: "username")

        }

        

        if let usersurname = message["usersurname"] as? String {//**7.1

            WKuserdefautls.set(usersurname, forKey: "usersurname")

        }

    }

  }

In my iPhone APP

tappedButton(){ if let validSession = self.session, validSession.isReachable {//5.1

                            let data: [String: Any] = ["iPhone": User_permitions.id! as Any] // Create your Dictionay as per uses

                            

                            let dataQR: [String: Any] = ["QRPath": User_permitions.personal_OR_path! as Any]

                            

                            let datauserid: [String: Any] = ["userid": User_permitions.personal_ID! as Any]

                            let datausername: [String: Any] = ["username": User_permitions.name! as Any]

                            let datausersurname: [String: Any] = ["usersurname": User_permitions.surname! as Any]

                            let datausermail: [String: Any] = ["usermail": User_permitions.eamil! as Any]

                            let datauserphone: [String: Any] = ["userphone": User_permitions.phone! as Any]

                            

                            

                            //validSession.transferUserInfo(data)

                           // validSession.transferUserInfo(dataQR)

                         validSession.sendMessage(data, replyHandler: nil, errorHandler: nil)

                         validSession.sendMessage(dataQR, replyHandler: nil, errorHandler: nil)

                            

                            validSession.sendMessage(datauserid, replyHandler: nil, errorHandler: nil)

                            validSession.sendMessage(datausername, replyHandler: nil, errorHandler: nil)

                            validSession.sendMessage(datausersurname, replyHandler: nil, errorHandler: nil)

                            validSession.sendMessage(datausermail, replyHandler: nil, errorHandler: nil)

                            validSession.sendMessage(datauserphone, replyHandler: nil, errorHandler: nil)

                         

                        } }

This WWDC21 talk will help you find the right way to get info to the watch. If you’re looking to share a password with your watch app, storing it in the keychain is likely the right choice. https://developer.apple.com/videos/play/wwdc2021/10003/

WCSessionDelegate
 
 
Q