本文介绍了打开父应用程序 (WatchOS 2.x+)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从父应用程序获取更新,但我不想在从 Apple Watch 发送请求时将父应用程序置于前台.我最终希望将父应用程序置于后台状态,让它运行所需的方法并将其发送回手表.

I am trying to get an update from the parent app, but I don't want to bring the parent app to the foreground when the request is sent from the Apple Watch. I ultimately would like to bring the parent app to a background state, have it run it's required method and send it back to the watch.

具体来说,有没有办法打开父应用程序并让它临时驻留在 WatchOS 2.0+ 的后台状态?

Specifically, is there a way to open the parent app and have it reside temporarily in a background state from WatchOS 2.0+?

我已经研究过这个:
[WKInterfaceController openParentApplication:[NSDictionary new] 回复:nil];

I have looked into this:
[WKInterfaceController openParentApplication:[NSDictionary new] reply:nil];

但这显然无法启动 Watch OS 2.0.

But this is unavailable starting Watch OS 2.0 apparently.

(父应用)应用委托:

@interface InterfaceController() <WCSessionDelegate>

@property (unsafe_unretained, nonatomic) IBOutlet WKInterfaceLabel *networkConnectionLabel;

@end

@implementation InterfaceController

- (void)awakeWithContext:(id)context
{
    [super awakeWithContext: context];
}

- (void) willActivate
{
    [super willActivate];
    [self startSessionToParentDevice];
}

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

- (void) session:(nonnull WCSession *)session didReceiveApplicationContext:(nonnull NSDictionary<NSString *,id> *)applicationContext
{
    [[self networkConnectionLabel] setText: [applicationContext objectForKey:@"CurrentConnectionStatus"]];
}

(手表应用)接口控制器:

(Watch App) Interface Controller:

@interface InterfaceController() <WCSessionDelegate>

@property (unsafe_unretained, nonatomic) IBOutlet WKInterfaceLabel *networkConnectionLabel;

@end

@implementation InterfaceController

- (void)awakeWithContext:(id)context
{
    [super awakeWithContext: context];
}

- (void) willActivate
{
    [super willActivate];
    [self startSessionToParentDevice];
}

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

- (void) session:(nonnull WCSession *)session didReceiveApplicationContext:(nonnull NSDictionary<NSString *,id> *)applicationContext
{
    [[self networkConnectionLabel] setText: [applicationContext objectForKey:@"CurrentConnectionStatus"]];
}

推荐答案

在手表端使用WatchConnectivity的sendMessage API在后台唤醒iOS应用

Use the WatchConnectivity's sendMessage APIs on the watch side to wake the iOS app up in the background

这篇关于打开父应用程序 (WatchOS 2.x+)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 03:54