问题
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