1

正如苹果建议在 Glance 中使用Handoff 。我想在 Glance Interface 中调用 web API,为此我做了以下事情

    - (void)awakeWithContext:(id)context
    {
        [super awakeWithContext:context];   
        [self CreateUaerActivity];   
    }
    -(void)CreateUaerActivity
    {
        NSUserActivity *activity = [[NSUserActivity alloc] initWithActivityType:@"com.xxx.xxx.glance"];    
        activity.title = @"Glance";
        activity.delegate=self;
        NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:kUserLoginWatchKit,kRequestTypeWatchKit, nil];
        activity.userInfo = dict;
        self.userActivity = activity;
        [self.userActivity becomeCurrent];
    }

- (void)willActivate
{

    [super willActivate];
    [NSTimer scheduledTimerWithTimeInterval:120 target:self selector:@selector(doSomething) userInfo:nil repeats:YES];
  }
-(void)doSomething
{

    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:kUserLoginWatchKit,kRequestTypeWatchKit, nil];
    [super updateUserActivity:@"com.xxx.xxx.glance" userInfo:dict webpageURL:nil];
 }
-(void)handleUserActivity:(NSDictionary *)userInfo
{
//displaying data    
}

AppDelegate.m文件中 -

    -(BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray *))restorationHandler
    {
        NSLog(@"Handoff dictionary: %@", userActivity.userInfo);
         NSString *requestType = userActivity.userInfo[kRequestTypeWatchKit];
    if ([requestType  isEqual: kGlanceDataWatchKit])
    {
//calling web API to get Data
}
  return YES;
  }

我发现 AppDelegate 从来没有调用continueUserActivity方法来向 Glance 界面返回一些东西。请指导我如何通过 Glance 接口调用 API。

4

1 回答 1

0

我不确定这是否是您想要的,但是如果您想调用网络 Api,我建议您这样做:

在 GlanceInterfaceController 中:

NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];

    [dictionary setObject:@"getSomething" forKey:@"action"];        
    [MainInterfaceController openParentApplication:dictionary reply:^(NSDictionary *replyInfo, NSError *error) {
        NSLog(@"Reply received by Watch app: %@", replyInfo); // the reply from the appDelegate... 
         }

在您父母的应用 Delegate 中:

  - (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void (^)(NSDictionary *))reply
{
       NSLog(@"Request received by iOS app");
if( [userInfo objectForKey:@"action"] isEqualToString:@"getSomething"] ){
//call you're Web API
//send the reponse to you're glance :              

reply(DictResponse);// some Dictionary from your web API... 

}

*****编辑*****

我也收到了同样的问题,一个简单的解决方法是开始后台任务,来自: fiveminutewatchkit

方法如下:

    - (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void (^)(NSDictionary *))reply
{
    // Temporary fix, I hope.
    // --------------------
    __block UIBackgroundTaskIdentifier bogusWorkaroundTask;
    bogusWorkaroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
        [[UIApplication sharedApplication] endBackgroundTask:bogusWorkaroundTask];
    }];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [[UIApplication sharedApplication] endBackgroundTask:bogusWorkaroundTask];
    });
    // --------------------

    __block UIBackgroundTaskIdentifier realBackgroundTask;
    realBackgroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
            reply(nil);
            [[UIApplication sharedApplication] endBackgroundTask:realBackgroundTask];
        }];

        // Kick off a network request, heavy processing work, etc.

        // Return any data you need to, obviously.
        reply(nil);
        [[UIApplication sharedApplication] endBackgroundTask:realBackgroundTask];
}

实际上,iOS 在您检索数据之前就杀死了您父母的应用程序...这(不是很干净的解决方案)可以防止您的应用程序被杀死...并让您有时间检索信息...

******结束编辑******

于 2015-05-19T13:31:57.940 回答