【问题标题】:Share data using WCSession between apple Watch and iPhone在 Apple Watch 和 iPhone 之间使用 WCSession 共享数据
【发布时间】:2017-12-01 13:01:05
【问题描述】:

尝试使用 updateApplicationContext 将数据从 iPhone 共享到 Apple Watch,Watch 连接 (WCSession) 的传输用户信息在模拟器中工作,但在设备上尝试相同但无法正常工作。

设备:iPhone 6 plus(OS 11.1)、apple watch-Series 2(Watch OS 4.1)。

处理 defaultSession 并为 watchConnectivity 实现必要的委托方法。

iPhone 端:

步骤:1

if ([WCSession isSupported])
{
    [[WCSession defaultSession] setDelegate:self];
    [[WCSession defaultSession] activateSession];
}

步骤:2

[[WCSession defaultSession] transferUserInfo:dataDict];

Apple Watch 方面:处理 defaultSession 步骤:1

if ([WCSession isSupported])
{
    [[WCSession defaultSession] setDelegate:self];
    [[WCSession defaultSession] activateSession];
}

步骤:2

   -(void)session:(WCSession *)session didReceiveUserInfo:(NSDictionary<NSString *,id> *)userInfo
    {
       // code for data parsing which get from userInfo dictionary.
    }

【问题讨论】:

    标签: objective-c watchos


    【解决方案1】:

    我已经为我的项目做了这个

    设置会话并创建数据字典后,您可以使用发送消息

    [[WCSession defaultSession] sendMessage:dataDict replyHandler:^(NSDictionary<NSString *,id> * _Nonnull replyMessage) {
    
            //You task on completion
    
        } errorHandler:^(NSError * _Nonnull error) {
    
            if (error)
            {
                //Handle the error
            }
        }];
    

    在手表应用代码中 您可以设置委托方法

    - (void)session:(WCSession *)session didReceiveMessage:(NSDictionary<NSString *, id> *)message replyHandler:(void(^)(NSDictionary<NSString *, id> *replyMessage))replyHandler
    {
    }
    

    你已经完成了

    【讨论】:

    • 感谢您的回复..我已经尝试了上述解决方案,但它也无法在真实设备上运行。
    猜你喜欢
    • 1970-01-01
    • 2015-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多