How can i check from watch app that if user logged-in or not into the phone application

1.1k Views Asked by At

I am trying to add watch extension to my existing application. Login is compulsory for my application. How can i check from watch app if user logged-in or not... and as user gets loged-in, I want to pass that login-data from the application to the watchapplication. I don't know how to pass the login data to watch application.

2

There are 2 best solutions below

1
On BEST ANSWER

Prior to WatchOS 2, you could share data between iOS companion app and watchOS app by using shared group container or iCloud to exchange data.

From WatchOS 2, since WatchKit extension now runs on Apple Watch itself, the extension must exchange data with the iOS app wirelessly. You will have to use WCSession class which is part of the WatchConnectivity framework. The framework is used to implement two-way communication between an iOS app and its paired watchOS app.

In iPhone companion app. Implement the following:

import WatchConnectivity

class LoginViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        if WCSession.isSupported() {
            WCSession.default().activate()
        } 
    }

    @IBAction func loginAction(_ sender: Any) {

    // In this case ApiHandler is just a class which performs REST call to authenticate login credentials.
     ApiHandler.login(username: txtUsername.text!, password: txtPassword.text!, { (loginSuccess) in
        if loginSuccess {

            let dict = ["statusLogin": "success"] 
            WCSession.default().sendMessage(dict, replyHandler: { (replyMessage) in
                print(replyMessage)
            }) { (error) in
                print(error.localizedDescription)
            }
        }

     }) 
    }

}

In watchOS app.

import WatchConnectivity

class BaseInterfaceController: WKInterfaceController {
    override func willActivate() {
        super.willActivate()

       if WCSession.isSupported() {
           WCSession.default().delegate = self
           WCSession.default().activate()
       }
    }

}

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

   }

   func session(_ session: WCSession, didReceiveMessage message: [String : Any], replyHandler: @escaping ([String : Any]) -> Swift.Void) {
        if let loginStatus = message["statusLogin"] as? String {
            if loginStatus == "success" {
              // login has been success in iOS app
              // perform further tasks, like saving in Userdefaults etc.
            }
        }
   }
}
0
On

You can do this using Watch Connectivity. Specifically sending a boolean value with transferUserInfo(). This will allow you to send the data from the iPhone application when the user either logs in or out, and it will be delivered to the watch extension in the background.

Here is an example:

func updateUserLoggedIn(_ loggedInValue : Bool) {

    if WCSession.isSupported() {
        WCSession.default.transferUserInfo(["User Logged In" : loggedInValue])
    }
}

Then you simply handle the transfer in your watch extension:

func session(_ session: WCSession, didReceiveUserInfo userInfo: [String : Any] = [:]) {

    if let userInfoFromPhone = userInfo["User Logged In"] {
        // Handle Result Accordingly
    }
}