transferFile does not start transfer for WatchConnectivity












0















I'm trying to send a dog image from the watch app to the iOS app but for some reason, the transfer is not starting. The transfer progress remains stuck at 1 and whenever I implement didFinishFileTransfer, it doesn't even give me an error. I'm not sure where it is going wrong. I've been using this tutorial. It says the watch session is already in progress or activated so I know it's not just an activation issue. And my interactive messaging works as well. Any help would be greatly appreciated!!



WatchSessionManager.swift
import WatchKit
import WatchConnectivity
import os.log



typealias MessageReceived = (session: WCSession, message: [String : Any], replyHandler: (([String : Any]) -> Void)?)

typealias ApplicationContextReceived = (session: WCSession, applicationContext: [String : Any])
typealias FileTransferred = (session: WCSession, file: WCSessionFileTransfer)
typealias FileReceived = (session: WCSession, file: WCSessionFile)

protocol WatchOSDelegate: AnyObject {
func transferCompleted(tuple: FileTransferred)
}

protocol iOSDelegate: AnyObject {
func messageReceived(tuple: MessageReceived)
func fileReceived(tuple: FileReceived)
}

class WatchSessionManager: NSObject {
static let sharedManager = WatchSessionManager()

weak var watchOSDelegate: WatchOSDelegate?
weak var iOSDelegate: iOSDelegate?

fileprivate let session : WCSession? = WCSession.isSupported() ? WCSession.default : nil

var validSession: WCSession? {
#if os(iOS)
if let session = session, session.isPaired && session.isWatchAppInstalled {
return session
}
return nil
#elseif os(watchOS)
return session
#endif
}

func startSession() {
session?.delegate = self
session?.activate()
}
}

extension WatchSessionManager: WCSessionDelegate {

func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
print("activationDidCompleteWith activationState:(activationState) error:(String(describing: error))")
}

#if os(iOS)
func sessionDidBecomeInactive(_ session: WCSession) {
print("sessionDidBecomeInactive: (session)")
}

func sessionDidDeactivate(_ session: WCSession) {
print("sessionDidDeactivate: (session)")
self.session?.activate()
}
#endif
}

extension WatchSessionManager{
private var validReachableSession: WCSession? {
if let session = validSession, session.isReachable {
return session
}
return nil
}

func sendMessage(message: [String : AnyObject], replyHandler: (([String : Any]) -> Void)? = nil, errorHandler: ((Error) -> Void)? = nil) {
validReachableSession?.sendMessage(message, replyHandler: replyHandler, errorHandler: errorHandler) }

func session(_ session: WCSession, didReceiveMessage message: [String : Any], replyHandler: @escaping(([String : Any]) -> Void)) {
handleSession(session, didReceiveMessage: message, replyHandler: replyHandler)
}

func handleSession(_ session: WCSession, didReceiveMessage message: [String : Any], replyHandler: (([String : Any]) -> Void)? = nil) {
#if os(iOS)
iOSDelegate?.messageReceived(tuple: (session, message, replyHandler))
#endif
}
}

extension WatchSessionManager {
func transferFile(file: URL, metadata: [String : AnyObject]?) -> WCSessionFileTransfer? {
print(validSession?.isReachable)
os_log("transfer of file")
return validSession?.transferFile(file, metadata: nil)
}

func session(session: WCSession, didFinishFileTransfer fileTransfer: WCSessionFileTransfer, error: Error?) {
handleFileSession(session, didFinishFileTransfer: fileTransfer, error: error)
}

func handleFileSession(_ session: WCSession, didFinishFileTransfer fileTransfer: WCSessionFileTransfer, error: Error?) {
#if os(watchOS)
watchOSDelegate?.transferCompleted(tuple: (session, fileTransfer))
#endif
}

func handleReceivedSession(_ session: WCSession, didReceieveFile file: WCSessionFile) {
#if os(iOS)
iOSDelegate?.fileReceived(tuple: (session, file))
#endif
}

func session(session: WCSession, didReceiveFile file: WCSessionFile) {
handleReceivedSession(session, didReceieveFile: file)
}
}


Then from my watch app, I do:



    @IBAction func stop() {
let filePath = URL.init(fileURLWithPath: Bundle.main.path(forResource: "dogs", ofType: "png")!)
transfers = connectivityHandler.transferFile(file: filePath, metadata: nil)
}


And in my iOS app, I have:



extension ViewController: iOSDelegate {
func messageReceived(tuple: MessageReceived) {
// works fine here
}

func fileReceived(tuple: FileReceived) {
let file = tuple.file
DispatchQueue.main.async {
let imageData = NSData.init(contentsOf: file.fileURL)
self.imageLoader.image = UIImage(data: imageData! as Data)
}
}
}









share|improve this question



























    0















    I'm trying to send a dog image from the watch app to the iOS app but for some reason, the transfer is not starting. The transfer progress remains stuck at 1 and whenever I implement didFinishFileTransfer, it doesn't even give me an error. I'm not sure where it is going wrong. I've been using this tutorial. It says the watch session is already in progress or activated so I know it's not just an activation issue. And my interactive messaging works as well. Any help would be greatly appreciated!!



    WatchSessionManager.swift
    import WatchKit
    import WatchConnectivity
    import os.log



    typealias MessageReceived = (session: WCSession, message: [String : Any], replyHandler: (([String : Any]) -> Void)?)

    typealias ApplicationContextReceived = (session: WCSession, applicationContext: [String : Any])
    typealias FileTransferred = (session: WCSession, file: WCSessionFileTransfer)
    typealias FileReceived = (session: WCSession, file: WCSessionFile)

    protocol WatchOSDelegate: AnyObject {
    func transferCompleted(tuple: FileTransferred)
    }

    protocol iOSDelegate: AnyObject {
    func messageReceived(tuple: MessageReceived)
    func fileReceived(tuple: FileReceived)
    }

    class WatchSessionManager: NSObject {
    static let sharedManager = WatchSessionManager()

    weak var watchOSDelegate: WatchOSDelegate?
    weak var iOSDelegate: iOSDelegate?

    fileprivate let session : WCSession? = WCSession.isSupported() ? WCSession.default : nil

    var validSession: WCSession? {
    #if os(iOS)
    if let session = session, session.isPaired && session.isWatchAppInstalled {
    return session
    }
    return nil
    #elseif os(watchOS)
    return session
    #endif
    }

    func startSession() {
    session?.delegate = self
    session?.activate()
    }
    }

    extension WatchSessionManager: WCSessionDelegate {

    func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
    print("activationDidCompleteWith activationState:(activationState) error:(String(describing: error))")
    }

    #if os(iOS)
    func sessionDidBecomeInactive(_ session: WCSession) {
    print("sessionDidBecomeInactive: (session)")
    }

    func sessionDidDeactivate(_ session: WCSession) {
    print("sessionDidDeactivate: (session)")
    self.session?.activate()
    }
    #endif
    }

    extension WatchSessionManager{
    private var validReachableSession: WCSession? {
    if let session = validSession, session.isReachable {
    return session
    }
    return nil
    }

    func sendMessage(message: [String : AnyObject], replyHandler: (([String : Any]) -> Void)? = nil, errorHandler: ((Error) -> Void)? = nil) {
    validReachableSession?.sendMessage(message, replyHandler: replyHandler, errorHandler: errorHandler) }

    func session(_ session: WCSession, didReceiveMessage message: [String : Any], replyHandler: @escaping(([String : Any]) -> Void)) {
    handleSession(session, didReceiveMessage: message, replyHandler: replyHandler)
    }

    func handleSession(_ session: WCSession, didReceiveMessage message: [String : Any], replyHandler: (([String : Any]) -> Void)? = nil) {
    #if os(iOS)
    iOSDelegate?.messageReceived(tuple: (session, message, replyHandler))
    #endif
    }
    }

    extension WatchSessionManager {
    func transferFile(file: URL, metadata: [String : AnyObject]?) -> WCSessionFileTransfer? {
    print(validSession?.isReachable)
    os_log("transfer of file")
    return validSession?.transferFile(file, metadata: nil)
    }

    func session(session: WCSession, didFinishFileTransfer fileTransfer: WCSessionFileTransfer, error: Error?) {
    handleFileSession(session, didFinishFileTransfer: fileTransfer, error: error)
    }

    func handleFileSession(_ session: WCSession, didFinishFileTransfer fileTransfer: WCSessionFileTransfer, error: Error?) {
    #if os(watchOS)
    watchOSDelegate?.transferCompleted(tuple: (session, fileTransfer))
    #endif
    }

    func handleReceivedSession(_ session: WCSession, didReceieveFile file: WCSessionFile) {
    #if os(iOS)
    iOSDelegate?.fileReceived(tuple: (session, file))
    #endif
    }

    func session(session: WCSession, didReceiveFile file: WCSessionFile) {
    handleReceivedSession(session, didReceieveFile: file)
    }
    }


    Then from my watch app, I do:



        @IBAction func stop() {
    let filePath = URL.init(fileURLWithPath: Bundle.main.path(forResource: "dogs", ofType: "png")!)
    transfers = connectivityHandler.transferFile(file: filePath, metadata: nil)
    }


    And in my iOS app, I have:



    extension ViewController: iOSDelegate {
    func messageReceived(tuple: MessageReceived) {
    // works fine here
    }

    func fileReceived(tuple: FileReceived) {
    let file = tuple.file
    DispatchQueue.main.async {
    let imageData = NSData.init(contentsOf: file.fileURL)
    self.imageLoader.image = UIImage(data: imageData! as Data)
    }
    }
    }









    share|improve this question

























      0












      0








      0








      I'm trying to send a dog image from the watch app to the iOS app but for some reason, the transfer is not starting. The transfer progress remains stuck at 1 and whenever I implement didFinishFileTransfer, it doesn't even give me an error. I'm not sure where it is going wrong. I've been using this tutorial. It says the watch session is already in progress or activated so I know it's not just an activation issue. And my interactive messaging works as well. Any help would be greatly appreciated!!



      WatchSessionManager.swift
      import WatchKit
      import WatchConnectivity
      import os.log



      typealias MessageReceived = (session: WCSession, message: [String : Any], replyHandler: (([String : Any]) -> Void)?)

      typealias ApplicationContextReceived = (session: WCSession, applicationContext: [String : Any])
      typealias FileTransferred = (session: WCSession, file: WCSessionFileTransfer)
      typealias FileReceived = (session: WCSession, file: WCSessionFile)

      protocol WatchOSDelegate: AnyObject {
      func transferCompleted(tuple: FileTransferred)
      }

      protocol iOSDelegate: AnyObject {
      func messageReceived(tuple: MessageReceived)
      func fileReceived(tuple: FileReceived)
      }

      class WatchSessionManager: NSObject {
      static let sharedManager = WatchSessionManager()

      weak var watchOSDelegate: WatchOSDelegate?
      weak var iOSDelegate: iOSDelegate?

      fileprivate let session : WCSession? = WCSession.isSupported() ? WCSession.default : nil

      var validSession: WCSession? {
      #if os(iOS)
      if let session = session, session.isPaired && session.isWatchAppInstalled {
      return session
      }
      return nil
      #elseif os(watchOS)
      return session
      #endif
      }

      func startSession() {
      session?.delegate = self
      session?.activate()
      }
      }

      extension WatchSessionManager: WCSessionDelegate {

      func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
      print("activationDidCompleteWith activationState:(activationState) error:(String(describing: error))")
      }

      #if os(iOS)
      func sessionDidBecomeInactive(_ session: WCSession) {
      print("sessionDidBecomeInactive: (session)")
      }

      func sessionDidDeactivate(_ session: WCSession) {
      print("sessionDidDeactivate: (session)")
      self.session?.activate()
      }
      #endif
      }

      extension WatchSessionManager{
      private var validReachableSession: WCSession? {
      if let session = validSession, session.isReachable {
      return session
      }
      return nil
      }

      func sendMessage(message: [String : AnyObject], replyHandler: (([String : Any]) -> Void)? = nil, errorHandler: ((Error) -> Void)? = nil) {
      validReachableSession?.sendMessage(message, replyHandler: replyHandler, errorHandler: errorHandler) }

      func session(_ session: WCSession, didReceiveMessage message: [String : Any], replyHandler: @escaping(([String : Any]) -> Void)) {
      handleSession(session, didReceiveMessage: message, replyHandler: replyHandler)
      }

      func handleSession(_ session: WCSession, didReceiveMessage message: [String : Any], replyHandler: (([String : Any]) -> Void)? = nil) {
      #if os(iOS)
      iOSDelegate?.messageReceived(tuple: (session, message, replyHandler))
      #endif
      }
      }

      extension WatchSessionManager {
      func transferFile(file: URL, metadata: [String : AnyObject]?) -> WCSessionFileTransfer? {
      print(validSession?.isReachable)
      os_log("transfer of file")
      return validSession?.transferFile(file, metadata: nil)
      }

      func session(session: WCSession, didFinishFileTransfer fileTransfer: WCSessionFileTransfer, error: Error?) {
      handleFileSession(session, didFinishFileTransfer: fileTransfer, error: error)
      }

      func handleFileSession(_ session: WCSession, didFinishFileTransfer fileTransfer: WCSessionFileTransfer, error: Error?) {
      #if os(watchOS)
      watchOSDelegate?.transferCompleted(tuple: (session, fileTransfer))
      #endif
      }

      func handleReceivedSession(_ session: WCSession, didReceieveFile file: WCSessionFile) {
      #if os(iOS)
      iOSDelegate?.fileReceived(tuple: (session, file))
      #endif
      }

      func session(session: WCSession, didReceiveFile file: WCSessionFile) {
      handleReceivedSession(session, didReceieveFile: file)
      }
      }


      Then from my watch app, I do:



          @IBAction func stop() {
      let filePath = URL.init(fileURLWithPath: Bundle.main.path(forResource: "dogs", ofType: "png")!)
      transfers = connectivityHandler.transferFile(file: filePath, metadata: nil)
      }


      And in my iOS app, I have:



      extension ViewController: iOSDelegate {
      func messageReceived(tuple: MessageReceived) {
      // works fine here
      }

      func fileReceived(tuple: FileReceived) {
      let file = tuple.file
      DispatchQueue.main.async {
      let imageData = NSData.init(contentsOf: file.fileURL)
      self.imageLoader.image = UIImage(data: imageData! as Data)
      }
      }
      }









      share|improve this question














      I'm trying to send a dog image from the watch app to the iOS app but for some reason, the transfer is not starting. The transfer progress remains stuck at 1 and whenever I implement didFinishFileTransfer, it doesn't even give me an error. I'm not sure where it is going wrong. I've been using this tutorial. It says the watch session is already in progress or activated so I know it's not just an activation issue. And my interactive messaging works as well. Any help would be greatly appreciated!!



      WatchSessionManager.swift
      import WatchKit
      import WatchConnectivity
      import os.log



      typealias MessageReceived = (session: WCSession, message: [String : Any], replyHandler: (([String : Any]) -> Void)?)

      typealias ApplicationContextReceived = (session: WCSession, applicationContext: [String : Any])
      typealias FileTransferred = (session: WCSession, file: WCSessionFileTransfer)
      typealias FileReceived = (session: WCSession, file: WCSessionFile)

      protocol WatchOSDelegate: AnyObject {
      func transferCompleted(tuple: FileTransferred)
      }

      protocol iOSDelegate: AnyObject {
      func messageReceived(tuple: MessageReceived)
      func fileReceived(tuple: FileReceived)
      }

      class WatchSessionManager: NSObject {
      static let sharedManager = WatchSessionManager()

      weak var watchOSDelegate: WatchOSDelegate?
      weak var iOSDelegate: iOSDelegate?

      fileprivate let session : WCSession? = WCSession.isSupported() ? WCSession.default : nil

      var validSession: WCSession? {
      #if os(iOS)
      if let session = session, session.isPaired && session.isWatchAppInstalled {
      return session
      }
      return nil
      #elseif os(watchOS)
      return session
      #endif
      }

      func startSession() {
      session?.delegate = self
      session?.activate()
      }
      }

      extension WatchSessionManager: WCSessionDelegate {

      func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
      print("activationDidCompleteWith activationState:(activationState) error:(String(describing: error))")
      }

      #if os(iOS)
      func sessionDidBecomeInactive(_ session: WCSession) {
      print("sessionDidBecomeInactive: (session)")
      }

      func sessionDidDeactivate(_ session: WCSession) {
      print("sessionDidDeactivate: (session)")
      self.session?.activate()
      }
      #endif
      }

      extension WatchSessionManager{
      private var validReachableSession: WCSession? {
      if let session = validSession, session.isReachable {
      return session
      }
      return nil
      }

      func sendMessage(message: [String : AnyObject], replyHandler: (([String : Any]) -> Void)? = nil, errorHandler: ((Error) -> Void)? = nil) {
      validReachableSession?.sendMessage(message, replyHandler: replyHandler, errorHandler: errorHandler) }

      func session(_ session: WCSession, didReceiveMessage message: [String : Any], replyHandler: @escaping(([String : Any]) -> Void)) {
      handleSession(session, didReceiveMessage: message, replyHandler: replyHandler)
      }

      func handleSession(_ session: WCSession, didReceiveMessage message: [String : Any], replyHandler: (([String : Any]) -> Void)? = nil) {
      #if os(iOS)
      iOSDelegate?.messageReceived(tuple: (session, message, replyHandler))
      #endif
      }
      }

      extension WatchSessionManager {
      func transferFile(file: URL, metadata: [String : AnyObject]?) -> WCSessionFileTransfer? {
      print(validSession?.isReachable)
      os_log("transfer of file")
      return validSession?.transferFile(file, metadata: nil)
      }

      func session(session: WCSession, didFinishFileTransfer fileTransfer: WCSessionFileTransfer, error: Error?) {
      handleFileSession(session, didFinishFileTransfer: fileTransfer, error: error)
      }

      func handleFileSession(_ session: WCSession, didFinishFileTransfer fileTransfer: WCSessionFileTransfer, error: Error?) {
      #if os(watchOS)
      watchOSDelegate?.transferCompleted(tuple: (session, fileTransfer))
      #endif
      }

      func handleReceivedSession(_ session: WCSession, didReceieveFile file: WCSessionFile) {
      #if os(iOS)
      iOSDelegate?.fileReceived(tuple: (session, file))
      #endif
      }

      func session(session: WCSession, didReceiveFile file: WCSessionFile) {
      handleReceivedSession(session, didReceieveFile: file)
      }
      }


      Then from my watch app, I do:



          @IBAction func stop() {
      let filePath = URL.init(fileURLWithPath: Bundle.main.path(forResource: "dogs", ofType: "png")!)
      transfers = connectivityHandler.transferFile(file: filePath, metadata: nil)
      }


      And in my iOS app, I have:



      extension ViewController: iOSDelegate {
      func messageReceived(tuple: MessageReceived) {
      // works fine here
      }

      func fileReceived(tuple: FileReceived) {
      let file = tuple.file
      DispatchQueue.main.async {
      let imageData = NSData.init(contentsOf: file.fileURL)
      self.imageLoader.image = UIImage(data: imageData! as Data)
      }
      }
      }






      ios iphone swift watch-os watchconnectivity






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 23 '18 at 17:58









      jaewhyunjaewhyun

      2618




      2618
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53451153%2ftransferfile-does-not-start-transfer-for-watchconnectivity%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53451153%2ftransferfile-does-not-start-transfer-for-watchconnectivity%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Sphinx de Gizeh

          Dijon

          Determine an Integral..