Open Parent Application (WatchOS 2.x+)

谁说胖子不能爱 提交于 2019-12-11 10:07:24

问题


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.

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

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

But this is unavailable starting Watch OS 2.0 apparently.

(Parent App) App Delegate:

@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"]];
}

回答1:


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



来源:https://stackoverflow.com/questions/34487780/open-parent-application-watchos-2-x

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!