I am trying to send file from watch to iOS every 15min. Core motion data will be saved in a file in watch and every 15 min watch sends it to iOS. both watch and iOS app are in background. So,I activate iOS app by calling sendMessage and on replyHandler, calling transferFile to send the file to iOS. If watch is in foreground its working fine, when watch in background, send message is received by iOS but transferFile is not working. only when watch app comes to foreground, it sends the file to iOS.
Code on watch side
func transferMotionDataFile() {
func sendFileToiOSApp () {
Code on iOS side
func session(_ session: WCSession, didReceiveMessage message: [String : Any], replyHandler: @escaping ([String : Any]) -> Void) {
func session(_ session: WCSession, didReceive file: WCSessionFile) {
print ("\(#function): \(file.fileURL.lastPathComponent)")
accessQueue is the serial queue created as let accessQueue = DispatchQueue (label: "xxxxxAccessQueue", qos: .default)
I have tried with dispatch queue main too.
How to send file when watch also in background?
Code on watch side
func transferMotionDataFile() {
Code Block self.accessQueue.async { sendFileToiOSApp }
}func sendFileToiOSApp () {
Code Block guard isSessionActive() else { print("\(#function): Session not activated") return } if (session.isReachable) { /* Sending log message along with activate message*/ let message = getActivateMessage() self.session.sendMessage(message, replyHandler: { replyMessage in let metaData = [WC_KEY.FILENAME: self.fileSentToServer[0].lastPathComponent] self.fileTransferSession = self.session.transferFile(self.fileSentToServer[0], metadata: metaData) print ("\(#function): iphone activated,transferring file") UserDefaults.standard.set(true, forKey: UD_KEY.WATCH_AWAITING_RESP) }, errorHandler: { error in print ("Error reaching iOS, Retrying..") let err = error as! WCError if (err.code != WCError.Code.notReachable) { /* wait till reachability status change*/ DispatchQueue.main.async { self.sendFileToApp() } } }) }
}Code on iOS side
func session(_ session: WCSession, didReceiveMessage message: [String : Any], replyHandler: @escaping ([String : Any]) -> Void) {
Code Block let msgKey = message[WC_KEY.MESSAGEKEY] as! String print ("\(#function): for \(msgKey)") if (msgKey == WC_KEY.ACTIVATE) { let replyMessage: [String: Any] = [WC_KEY.STATUS: WC_KEY.ACTIVATED] replyHandler (replyMessage) }
}func session(_ session: WCSession, didReceive file: WCSessionFile) {
print ("\(#function): \(file.fileURL.lastPathComponent)")
Code Block let destFile = FileUtility.getUrl(file.fileURL) if (FileUtility.moveFile(from: file.fileURL, to: destFile)) { print ("\(#function): Move to \(destFile.absoluteString)") self.accessQueue.async { //Processing the file } }
}accessQueue is the serial queue created as let accessQueue = DispatchQueue (label: "xxxxxAccessQueue", qos: .default)
I have tried with dispatch queue main too.
How to send file when watch also in background?