gpt4 book ai didi

ios - iPhone应用程序处于终止状态时Apple Watch应用程序不起作用?

转载 作者:行者123 更新时间:2023-12-01 16:33:52 30 4
gpt4 key购买 nike

我正在开发一个苹果 watch 应用程序,但面临一个奇怪的问题,即我的 watch 应用程序仅在我手动打开 iPhone 应用程序或 iPhone 应用程序在后台时才有效。当我终止我的 iPhone 应用程序并测试 Apple Watch 应用程序时,它不再工作了。

在这里我提到 watch 应用程序流程:

  • 当 Apple Watch 应用程序启动时,我调用 Web api 从服务器获取响应。
  • 我用了openParentApplication:reply:从父应用调用web api的方法
  • 我知道,我将不得不在后台线程中调用 web api 方法,因为 openParentApplication:reply: 方法会自动在 iPhone 中打开父应用程序并同时暂停,所以如果我们正在使用此方法处理耗时任务,那么我们应该使用 WatchKit Development Tips 中提到的后台线程.所以我使用后台线程来调用 web api。
  • 当我收到回复时,我将其传递给观看应用程序。

  • 这是附加的片段:

    观看应用程序 - InitialInterfaceController.m
    - (void)awakeWithContext:(id)context {
    [super awakeWithContext:context];
    }

    - (void)willActivate {
    [super willActivate];
    [self getDetails];
    }


    - (void)getDetails{
    //Open parent app
    [WKInterfaceController openParentApplication:@{@“request”:@“details”}
    reply:^(NSDictionary *replyInfo, NSError *error) {
    if (!error) {
    NSLog(@“Success”);
    [self parseKPI:replyInfo];
    }
    else{
    NSLog(@"Error - %@", error.localizedDescription);
    }
    }];

    }

    iPhone 应用程序 - AppDelegate.m
    - (void)application:(UIApplication *)application
    handleWatchKitExtensionRequest:(NSDictionary *)userInfo
    reply:(void (^)(NSDictionary *))reply{
    NSString *request = [userInfo objectForKey:@“request”];

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
    // Get Details
    if ([request isEqualToString:@"details"]) {
    APIHandler *api = [[APIHandler alloc] init];
    [api getDetailsUsername:@“my_user_name”
    onSuccess:^(NSDictionary *details) {
    dispatch_async(dispatch_get_main_queue(), ^{
    reply(details);
    });

    } onFailure:^(NSString *message) {
    dispatch_async(dispatch_get_main_queue(), ^{
    reply(@{@"Error":message});
    });

    }];
    }
    }

    iPhone 应用程序 - APIHandler.m
    - (void) getDetailsUsername:(NSString *)username
    onSuccess:(void(^)(NSDictionary * details))success
    onFailure:(void(^)(NSString *message))failure{

    NSString *urlString = [NSString stringWithFormat:@"%@%@", HOST, DETAILS_API];
    urlString = [urlString stringByAppendingFormat:@"?username=%@",username];
    urlString = [urlString stringByAppendingFormat:@"&%@", self.APIKeyParameter];

    NSURL *url = [NSURL URLWithString:urlString];
    NSMutableURLRequest *mutableURL = [NSMutableURLRequest requestWithURL:url];
    [NSURLConnection sendAsynchronousRequest:mutableURL
    queue:[NSOperationQueue mainQueue]
    completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
    if (!connectionError) {
    NSError *error = nil;
    NSDictionary *details = [NSJSONSerialization JSONObjectWithData:data
    options:NSJSONReadingMutableLeaves
    error:&error];
    success(details);
    }
    else{
    failure(@"Connection Error!");
    }
    }];
    }

    但是这种方法对我不起作用。

    我在 watch 应用模拟器中发现了另一个问题,即
    - (void)awakeWithContext:(id)context因为我的初始 View Controller 被调用,但是 - (void)willActivate有时不会调用方法,我只看到 watch 应用微调器。有时它会起作用。这很奇怪。在使用 Storyboard 添加的初始界面 Controller 中,我有大约 15 个控件(包括所有组)。

    我也提到了 Watchkit not calling willActivate method并修改了我的代码,但仍然面临同样的问题。

    谁能告诉我为什么这个问题在我的应用程序中仍然存在?

    最佳答案

    您需要在 iPhone 应用程序中以稍微不同的方式处理请求,以使您的应用程序不会被操作系统杀死。我在这里分享了一个类似的答案:https://stackoverflow.com/a/29848521/3704092

    关于ios - iPhone应用程序处于终止状态时Apple Watch应用程序不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29938617/

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