ios - 从 watch 应用程序向 iOS 应用程序发送消息时,“isReachable”为假

标签 ios watchkit apple-watch

我想从 watch 应用程序向 iOS 应用程序发送即时消息。在 XCode7 beta 4 版本中实现了以下代码,并在两个模拟器中将应用程序保持在前台。这是我实现的代码

在watchkit interfaceController中


    -(void)willActivate 
    {
        [super willActivate];
        if ([WCSession isSupported]) {
            WCSession *session = [WCSession defaultSession];
            session.delegate = self;
            [session activateSession];
        }
    }

-(IBAction)buttonClicked
{
    NSDictionary *applicationDict = [[NSDictionary alloc] initWithObjects:@[@"Hi"] forKeys:@[@"key"]];
    if([[WCSession defaultSession] isReachable])
    {
        [[WCSession defaultSession] sendMessage:applicationDict
                                   replyHandler:^(NSDictionary *reply) {

                                       NSLog(@"%@",reply);

                                   }

                                   errorHandler:^(NSError *error) {

                                       NSLog(@"%@",error);

                                   }];
    }
}


在 iOS 应用类中


    -(void)viewDidLoad 
    {
        [super viewDidLoad];
        if ([WCSession isSupported]){
            WCSession *session = [WCSession defaultSession];
            session.delegate = self;
            [session activateSession];
        }
    }


    -(void)session:(nonnull WCSession *)session 
    didReceiveMessage:(nonnull NSDictionary *)message replyHandler:(nonnull void (^)(NSDictionary * __nonnull))replyHandler 
    {
        dispatch_async(dispatch_get_main_queue(), ^{
            self.testLbl.text = [message objectForKey:@"key"];
            [self.view setNeedsDisplay];
        });
    }

最佳答案

您必须使用 sendMessage API 吗?我发现它们也不可靠且不可预测。我最终使用了 applicationContext API。 watch 不一定是可访问的,但如果是,它会立即到达,如果不可访问,它会在应用程序启动时交付。每次更新应用程序上下文时,它都会覆盖以前的版本,这可能不是您要查找的版本。

关于ios - 从 watch 应用程序向 iOS 应用程序发送消息时,“isReachable”为假,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31583592/

相关文章:

ios - 配置文件错误 whatchkit

ios - 在 iPhone 和 Watch 应用程序中共享文档目录中的数据

ios - 结合 2 个项目-主要 iOS 应用程序和 Watchkit pp

ios - WatchKit 和 UIAlertView/UIAlertController 弹出窗口

ios - Watchkit,dismissController 函数

apple-watch - 获取复杂数据 : ExtensionDelegate not Called

iphone - uitableview 中的多个倒数计时器

html - CSS 动画并不总是在 iOS 8 Safari 中启动

php - 服务器端 Apple 推送通知

IOS video over video 与圆形裁剪合并