2017-02-10 147 views
11

我工作的一個應用程序iPhone和Apple關注之間共享數據,使用WCSession方法sendMessage:replyHandler:errorHandler:WCErrorCodeDeliveryFailed:有效載荷無法交付

實施該方法後,我得到這樣的錯誤:

WCSession _onqueue_notifyOfMessageError :withErrorHandler:errorHandler:YES和WCErrorCodeDeliveryFailed。

錯誤=無法傳送有效載荷。

import Foundation 
import WatchKit 
import WatchConnectivity 

class ResultInterfaceController: WKInterfaceController, WCSessionDelegate { 

override func awake(withContext context: Any?) { 
    super.awake(withContext: context) 

    let applicationData = ["name": "ViratKohli"] 
    self.sendToPhone(data: applicationData) 
} 

func sendToPhone(data: [String: Any]) { 

    if WCSession.isSupported() { 

     let session = WCSession.default 
     session().delegate = self 
     session().activate() 

     if WCSession.default().isReachable { 

      session().sendMessage(data, replyHandler: {(_ replyMessage: [String: Any]) -> Void in 

       print("ReplyHandler called = \(replyMessage)") 
       WKInterfaceDevice.current().play(WKHapticType.notification) 
      }, 
      errorHandler: {(_ error: Error) -> Void in 

       print("Error = \(error.localizedDescription)") 
      }) 
     } 
    } 
} 
.... 

任何幫助表示讚賞。

+0

也許[這](http://stackoverflow.com/questions/33200630/wcsession-sendmessagereplyhandler-error-code-7014-wcerrorcodedeliveryfailed)不幫你嗎? –

+0

@ReinhardMännernope。那沒有 – SahyadriChava

回答

5
  1. 您是否有session(_ session: WCSession, didReceiveMessage message: [String : Any], replyHandler: @escaping ([String : Any]) -> Void)在ios端的WCSessionDelegate?
  2. 你打電話replyHandler()這個方法裏面?

請注意,session(_ session: WCSession, didReceiveMessage message: [String : Any])將僅用於未發送replyHandler的郵件。

+0

這是正確的答案。如果提供了'replyHandler'但其他應用程序不執行與'replyHandler'變異,請求將失敗。 –