handlewatchkitextensionrequest未在挂起状态下调用异步调用

时间:2015-10-18 08:45:41

标签: ios objective-c watchkit

从Apple手表扩展程序,handlewatchkitextensionrequest不会调用异步调用并将nill数据返回给Apple watch,下面是代码请帮帮我。

-(void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void (^)(NSDictionary *))reply
{
    __block UIBackgroundTaskIdentifier watchKitHandler;
    watchKitHandler = [[UIApplication sharedApplication] beginBackgroundTaskWithName:@"backgroundTask"
                                                                   expirationHandler:^{
                                                                       watchKitHandler = UIBackgroundTaskInvalid;
                                                                   }];


    dispatch_semaphore_t sema = dispatch_semaphore_create(0);

    if ([[userInfo objectForKey:@"request"] isEqualToString:@"NearestFacility"]) {

        [self NearestFacilityClicked];
        if (self.ary_WatchKitNearestFacility.count>0) {
        NSDictionary *response = @{@"response" : self.ary_WatchKitNearestFacility};
        reply(response);
        }
    }

    dispatch_semaphore_signal(sema);

    dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
    dispatch_after(dispatch_time( DISPATCH_TIME_NOW, (int64_t)NSEC_PER_SEC * 1),    dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [[UIApplication sharedApplication] endBackgroundTask:watchKitHandler];
    });
}


-(IBAction)NearestFacilityClicked
{

    // ASIFormdatarequest call
    [request startAsynchronous];
}

1 个答案:

答案 0 :(得分:0)

在观看操作系统2中,您需要使用会话在手表和手表之间传输数据。苹果手机。 首先,您需要在viewdidload()

中初始化WCSession
if (WCSession.isSupported()) {
let session = WCSession.defaultSession()
session.delegate = self
session.activateSession()

}

然后发送您可以使用的数据

 let transfer =  WCSession.defaultSession().transferUserInfo(applicationDict)

然后,在接收端,您需要实现session:didReceiveUserInfo :(开发人员文档)。请注意,根据Apple" watchOS2 Transition Guide,"

  

要开始沟通,您的Watch应用和iOS应用都必须具有有效的WCSession对象。通常,每个应用程序在启动时创建,配置和激活会话对象,并在中心位置存储对它的引用。如果要发送数据,则检索会话对象并调用其方法。