简体   繁体   English

iPhone无法从AppleWatch接收applicationContext

[英]iPhone not receiving applicationContext from AppleWatch

I have created a program to test sending data back and forth from iPhone and AppleWatch, and visa versa. 我创建了一个程序来测试从iPhone和AppleWatch来回发送数据,反之亦然。 I've set it up so there is a button on the AppleWatch and a button on the iPhone. 我已经设置好了,因此AppleWatch上有一个按钮,iPhone上有一个按钮。 When the iPhone one is pressed, it will send data and rename the button on the AppleWatch to whatever that data String was. 当按下iPhone一台时,它将发送数据并将AppleWatch上的按钮重命名为该数据字符串所在的位置。

I then implemented the same code for AppleWatch to iPhone but for some reason iPhone doesn't seem to receive the data. 然后,我为AppleWatch实现了与iPhone相同的代码,但由于某种原因,iPhone似乎没有收到数据。 Here's the code for iPhone: 这是iPhone的代码:

//  ViewController.swift

import UIKit
import Foundation
import WatchConnectivity

class WatchManager: UIViewController, WCSessionDelegate {

    var watchSession: WCSession? {
        didSet {
            if let session = watchSession {
                session.delegate = self
                session.activate()
            }
        }
    }

    override func viewDidLoad(){
        super.viewDidLoad()

        watchSession = WCSession.default

    }

    private func sendDict(_ dict: [String: Any]) {
        do {
            try self.watchSession?.updateApplicationContext(dict)
        } catch {
            print("Error sending dictionary \(dict) to Apple Watch!")
        }
    }

    @IBOutlet weak var transferButton: UIButton!

    @IBAction func dataTransfer(_ sender: Any) {
        sendDict(["DataKey": UUID().uuidString])
        print("sent")
    }

    func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
        print("Session activation did complete")
    }

    public func sessionDidBecomeInactive(_ session: WCSession) {
        print("session did become inactive")
    }

    public func sessionDidDeactivate(_ session: WCSession) {
        print("session did deactivate")
    }

    func session(_ session: WCSession, didReceiveApplicationContext applicationContext: [String : Any]) {
        print("phone received app context: ", applicationContext)
        if let temperature = applicationContext["DataKey"] as? String {
            self.transferButton.setTitle(temperature, for: .normal)
        }

    }

}

and AppleWatch: 和AppleWatch:

//  InterfaceController.swift

import WatchKit
import Foundation
import WatchConnectivity


class InterfaceController: WKInterfaceController {

    var watchSession: WCSession? {
        didSet {
            if let session = watchSession {
                session.delegate = self
                session.activate()
            }
        }
    }


    @IBOutlet weak var temperatureLabel: WKInterfaceButton!

    private func sendDict(_ dict: [String: Any]) {
        do {
            try self.watchSession?.updateApplicationContext(dict)
        } catch {
            print("Error sending dictionary \(dict) to iPhone!")
        }
    }

    @IBAction func button() {
        let urg = ["DataKey":UUID().uuidString]
        sendDict(urg)
        print("watch sent app context \(urg)")
    }

}

extension InterfaceController: WCSessionDelegate {

    #if os(iOS)
    public func sessionDidBecomeInactive(_ session: WCSession) { }
    public func sessionDidDeactivate(_ session: WCSession) {
        session.activate()
    }
    #endif

    func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
        print("Session activation did complete")
    }

    func session(_ session: WCSession, didReceiveApplicationContext applicationContext: [String : Any]) {
        print("watch received app context: ", applicationContext)
        if let temperature = applicationContext["DataKey"] as? String {
            self.temperatureLabel.setTitle(temperature)
        }

    }

    override func willActivate() {
        // This method is called when watch view controller is about to be visible to user
        super.willActivate()

        watchSession = WCSession.default

    }

    override func didDeactivate() {
        // This method is called when watch view controller is no longer visible
        super.didDeactivate()
    }

}

I've tried changing the name of the key (didn't work), I made it so the data value is always changing (UUID().uuidString) and kept that. 我试图更改键的名称(无法正常工作),但这样做了,因此数据值始终在更改(UUID()。uuidString)并保留了该值。 Other things I tried consisted of creating a label and trying to rename that instead of the button which failed, and lastly instead of renaming the button just sending some confirmation that it received data back to the apple watch, which failed. 我尝试的其他操作包括创建标签,并尝试重命名该标签而不是失败的按钮,最后不是重命名该按钮,只是向其发送了确认已将数据接收回Apple Watch的确认。

Any help would be much appreciated, I hope it's not a silly error. 任何帮助将不胜感激,我希望这不是一个愚蠢的错误。

I think updateApplicationContext is not the right method for your needs. 我认为updateApplicationContext不是满足您需求的正确方法。 From the docs: The system sends context data when the opportunity arises, with the goal of having the data ready to use by the time the counterpart wakes up. 来自文档: 系统在机会出现时发送上下文数据,目的是在对方醒来之前准备好使用数据。 If you want to send data back and forth while both apps are in foreground sendMessage should work. 如果您想在两个应用程序都处于前台时来回发送数据,则sendMessage应该可以工作。

In the end you may need to implement a combination of both methods. 最后,您可能需要实现两种方法的组合。 I suggest reading the following doc: https://developer.apple.com/documentation/watchconnectivity/wcsession 我建议阅读以下文档: https : //developer.apple.com/documentation/watchconnectivity/wcsession

Edit: 编辑:

Just to make the point with "In the end you may need to implement a combination of both methods" even clearer, I have added some sample code from one of my apps. 为了更明确地指出“最终可能需要实现两种方法的组合”,我从一个应用程序中添加了一些示例代码。

The method _sendData tries to send the current data via sendMessageData , if the watch is reachable. 如果监视可达,则方法_sendData尝试通过sendMessageData发送当前数据。 If not it updates the application context to have the data available as some as the watch app starts. 否则,它将在监视应用程序启动时更新应用程序上下文以使数据可用。

- (void)_sendCurrentData
{
    if ([WCSession defaultSession].isPaired && [WCSession defaultSession].isWatchAppInstalled)
    {
        if ([WCSession defaultSession].isReachable)
        {
            // Send data to watch
            [[WCSession defaultSession] sendMessageData:self.currentData
                                           replyHandler:nil
                                           errorHandler:^(NSError * _Nonnull error) {
                                               [self _updateApplicationContext];
                                           }];
        }
        else
        {
            [self _updateApplicationContext];
        }
    }
}

- (void)_updateApplicationContext
{
    if ([WCSession defaultSession].isPaired && [WCSession defaultSession].isWatchAppInstalled)
    {
        NSError* error = nil;
        [[WCSession defaultSession] updateApplicationContext:@{@"data": self.currentData}
                                                       error:&error];
        if (error != nil)
        {
            NSLog(@"error while updating application context: %@", error.localizedDescription);
        }
    }
}
use self.watchSession?.transferUserInfo(dict) instead of self.watchSession?.updateApplicationContext(dict) 
you'll got a call back in:
    func session(_ session: WCSession, didReceiveUserInfo userInfo: [String : Any] = [:]) {
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当AppleWatch处于后台状态或睡眠模式时,我们如何从iPhone向AppleWatch发送消息? - How can we send a message from iphone to AppleWatch when AppleWatch in backgroundstate or sleepmode? 数据未在真实设备上从iphone传输到iWatch(AppleWatch) - Data is not being transferred from iphone to iWatch (AppleWatch) on real devices 使用watchOS2将消息从AppleWatch发送到iPhone应用程序 - send message from AppleWatch to iPhone app using watchOS2 如何在AppleWatch OS2上使用iPhone上的coredata? - How to use coredata from iPhone on AppleWatch OS2? 是否可以通过手表连接将复杂的数组从 iPhone 发送到 AppleWatch? - Is it possible to send complex Arrays from iPhone to AppleWatch with watch connectivity? 如何以编程方式在AppleWatch上唤醒iPhone - How to wake iphone on applewatch programmatically AppleWatch 模拟器和 iPhone 模拟器之间的 transferUserInfo 停止从 Xcode 11 工作 - transferUserInfo between AppleWatch simulator and iPhone simulator stop working from Xcode 11 如何在iPhone应用程序和applewatch之间共享数据? - How to Share data between iPhone app and applewatch? 从 AppleWatch 屏幕捕获视频 - Capturing video from AppleWatch screen AppleWatch在iOS上发布Live HeartRate统计信息 - Live HeartRate Stats on iOS from AppleWatch
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM