gpt4 book ai didi

ios - 如何从 Watch App(Watch OS 2)与不活动的 IOS 应用程序通信

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:33:30 24 4
gpt4 key购买 nike

尝试将我的 Watch OS1 应用程序升级到 Watch OS 2。为 Watch OS 2 创建了新目标。并使用 sendMessage:replyHandler:errorHandler 从 IOS 应用程序发送/获取回复。如果只有 IOS 应用程序正在运行,它才能正常工作。如果 Watch 应用程序尝试在 iOS 应用程序处于非事件状态(Killed 状态)时进行通信,出现连接错误 7001。如何从 Watch App(Watch OS 2)通信非事件的 IOS 应用程序?

watch app 的 sendMessage:replyHandler:errorHandler 方法会在后台唤醒相应的 iOS 应用程序并使其可访问吗?

谢谢。

编辑1:-

iOS APP的App Delegate:

- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions{

if ([WCSession isSupported]) {
WCSession *session = [WCSession defaultSession];
session.delegate = self;
[session activateSession];
}
return YES;
}
- (void)session:(nonnull WCSession *)session didReceiveMessage:(nonnull NSDictionary<NSString *,id> *)message replyHandler:(nonnull void (^)(NSDictionary<NSString *,id> * __nonnull))replyHandler {

UIApplication *application = [UIApplication sharedApplication];
__block UIBackgroundTaskIdentifier identifier = UIBackgroundTaskInvalid;
dispatch_block_t endBlock = ^ {
if (identifier != UIBackgroundTaskInvalid) {
[application endBackgroundTask:identifier];
}
identifier = UIBackgroundTaskInvalid;
};
identifier = [application beginBackgroundTaskWithExpirationHandler:endBlock];

if (replyHandler!=nil) {
replyHandler(resultContainer); // my data dictionary from Iphone app to watch os as reply.
}

if (identifier!=UIBackgroundTaskInvalid) {
[application endBackgroundTask:identifier];
identifier = UIBackgroundTaskInvalid;
}
}

观看应用:

- (void)applicationDidFinishLaunching {
// Perform any final initialization of your application.
if ([WCSession isSupported]) {
WCSession *session = [WCSession defaultSession];
session.delegate = self;
[session activateSession];
}
NSDictionary *context = @{@"APP_LOADING":@"LOADING"};
[WKInterfaceController reloadRootControllersWithNames:@[WATCH_INTERFACE_LOADING] contexts:@[context]];


NSDictionary *request = //My Request data;
[[WCSession defaultSession] sendMessage:request
replyHandler:^(NSDictionary *reply) {
//handle reply from iPhone app here

NSDictionary *resultDict = [reply objectForKey:WATCH_REQUEST_RESULT];
// Use reply from Phone app
}
errorHandler:^(NSError *error) {
//catch any errors here
// Getting error here 7001 Error.
}
];

}

最佳答案

因为 activate 方法是异步的,所以在委托(delegate)方法中使用它,如下所示:

    public func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?){
print("activationDidCompleteWith")
if activationState == WCSessionActivationState.activated {
NSLog("Activated")
if(WCSession.default().isReachable){

do {
try session.updateApplicationContext(
[WatchRequestKey : "updateData"]
)
}
catch let error as NSError {
print("\(error.localizedDescription)")
}
}
}

if activationState == WCSessionActivationState.inactive {
NSLog("Inactive")
}

if activationState == WCSessionActivationState.notActivated {
NSLog("NotActivated")
}
}

关于ios - 如何从 Watch App(Watch OS 2)与不活动的 IOS 应用程序通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36930951/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com