我正尝试将手表图像中的狗图像发送到iOS应用中,但由于某种原因,传输尚未开始。传输进度一直停留在1,并且每当我实现didFinishFileTransfer时,它甚至都不会给我一个错误。我不确定哪里出了问题。我一直在使用this tutorial。它说监视会话已经在进行中或已激活,所以我知道这不仅仅是激活问题。而且我的交互式消息传递也很好。任何帮助将不胜感激!!
WatchSessionManager.swift 导入WatchKit 导入WatchConnectivity 导入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)
}
}
然后从我的手表应用中执行操作
@IBAction func stop() {
let filePath = URL.init(fileURLWithPath: Bundle.main.path(forResource: "dogs", ofType: "png")!)
transfers = connectivityHandler.transferFile(file: filePath, metadata: nil)
}
在我的iOS应用中,我有:
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)
}
}
}