Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can the watch app receive background info even if the app is in the background?

I cannot figure out how to make updateApplicationContext data arrive on the watch before the watch app is foregrounded. It seems to work only when the watch app is foregrounded.

How can the watch receive files while in the background?

This is what I've been trying to accomplish:

iOS code:

func sendDataToWatch() {
        if WCSession.isSupported() {
            do {
                try WCSession.default.updateApplicationContext(["key":value])
            } catch {
                print("ERROR: \(error)")
            }
        }
    }

Watch code:

func session(_ session: WCSession, didReceiveApplicationContext
        applicationContext:[String : Any]) {

      //handle data when it arrives

    }

I noticed that the WatchConnectivity was provided with a handler function. Is this something I should set up to be able to handle background connectivity while the Watch App is backgrounded or not even launched?

func handle(_ backgroundTasks: Set<WKRefreshBackgroundTask>) {
    // Sent when the system needs to launch the application in the background to process tasks. Tasks arrive in a set, so loop through and process each one.
    for task in backgroundTasks {
        // Use a switch statement to check the task type
        switch task {
        case let backgroundTask as WKApplicationRefreshBackgroundTask:
            // Be sure to complete the background task once you’re done.
            backgroundTask.setTaskCompletedWithSnapshot(false)
        default:
            // make sure to complete unhandled task types
            task.setTaskCompletedWithSnapshot(false)
        }
    }
}
like image 645
Oskar Gusgård Avatar asked Apr 09 '18 08:04

Oskar Gusgård


People also ask

Can iOS apps run in the background?

If an app plays audio in the background (over AirPlay or through the phone's speakers), iOS permits it to run in the background until it ceases to play the music; if an app allows you to make data-based phone calls (like Whatsapp or Skype calls) in the background, it can stay active, using CPU for the duration of the ...

Why do iOS apps run in the background?

Background app refresh is a feature of iOS and Android that allows apps to update their content from the internet, even while you're not using them. In contrast, we say that apps use data in the foreground when you open them and use them yourself.

How do I enable background mode in Xcode?

Select your app's target in Xcode and select the Capabilities tab. Under the Capabilities tab, set the Background Modes switch to ON and select the “Audio, AirPlay, and Picture in Picture” option under the list of available modes.


1 Answers

According to apple you can send data from iPhone to Apple Watch using SendMessage while session is reachable.

https://developer.apple.com/documentation/watchconnectivity/wcsession/1615687-sendmessage

Calling this method from your WatchKit extension while it is active and running wakes up the corresponding iOS app in the background and makes it reachable.

You can use below methods to send data from the iPhone to Apple Watch

Swift 2.2

let msg = ["IPrequest":"IsLogin"]

WCSession.defaultSession().sendMessage(msg, replyHandler: { (replyDict) in
    print(replyDict)
    }, errorHandler: { (error) in
        print(error)
})

Received dictionary using below method

Swift 2.2

func session(session: WCSession, didReceiveMessage message: [String : AnyObject], replyHandler: ([String : AnyObject]) -> Void)
{

    dispatch_async(dispatch_get_main_queue()) { () -> Void in

        print("Response:\(message)")

    }
}

I have implemented above solution in one of the my project.

Hope it will help you!

like image 180
iOS Developer Avatar answered Sep 24 '22 21:09

iOS Developer