是否可以使用NSUserActivity通过Glance Apple watch调用API

时间:2015-05-19 11:00:11

标签: ios objective-c iphone apple-watch openstack-glance

苹果建议使用Handoff。 我想在Glance界面中调用Web API,为此我做了以下事情

    -(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.m 文件 -

{{1}}

我发现AppDelegate从未调用 continueUserActivity 方法将某些内容返回给Glance界面。 请指导我如何通过Glance界面调用API。

1 个答案:

答案 0 :(得分: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... 
         }

在您父母的应用中代表:

  - (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... 

}

***** EDIT *****

我已经发出同样的问题,一个简单的解决方法是开始后台任务,来自: 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会杀死您父母的应用程序...这(不是非常干净的解决方案)会阻止您的应用程序被杀死...并让您有时间检索信息......

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