1
我想從父應用的更新,但我不希望把父應用推到前臺時,請求從Apple關注發送。我最終希望將父應用程序置於後臺狀態,讓它運行所需的方法並將其發送回手表。打開父應用(WatchOS 2.X +)
具體而言,是有辦法打開父應用程序,並將它從WatchOS 2.0+後臺狀態暫住?
我已經看過這個:
[WKInterfaceController openParentApplication:[NSDictionary new] reply:nil];
但這是無法開始關注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"]];
}
(監視應用程序)接口控制器:
@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"]];
}
「openParentApplication」 的方法現在已經貶值。是的watchConnectivity現在是解決方案。但是我沒有看到WCSession的任何前臺/後臺方法在iPhone和Watch之間發送信息。您實際面臨的問題是什麼? – Muneeba
Muneeba,我需要將iPhone當前的網絡狀態發送到手錶,以便使用wifi,手機或無服務更新標籤。但它只能在前景或背景中執行此操作,因爲我正在使用可達性。 –
如果你需要在watch上執行任何網絡請求,你可以直接通過NSURLSession來實現,watchOS 2也支持。檢查這個https://developer.apple.com/videos/play/wwdc2015-711/ – Muneeba