我正在尝试从父应用程序获取更新,但是当我从Apple Watch发送请求时,我不想将父应用程序带到前台。我最终希望将父应用程序带到后台状态,让它运行它所需的方法并将其发送回手表。
具体来说,有没有办法打开父应用程序,让它暂时驻留在WatchOS 2.0+的后台状态?
我已经调查过这个:
[WKInterfaceController openParentApplication:[NSDictionary new]回复:nil];
但显然从Watch OS 2.0开始这是不可用的。
(家长应用)应用代表:
@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 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"]];
}
答案 0 :(得分:0)
在手表端使用WatchConnectivity的sendMessage API在后台唤醒iOS应用程序