简体   繁体   English

WCSession didReceiveUserInfo被调用,但iPhone应用程序停止后台活动

[英]WCSession didReceiveUserInfo gets called but iPhone app stops background activity

I'm experiencing issues with my watchOS 2 app. 我的watchOS 2应用出现问题。 I'm using [[WCSession defaultSession] transferUserInfo:request] to transfer a dictionary of data to my iPhone app. 我正在使用[[WCSession defaultSession] transferUserInfo:request]将数据字典传输到我的iPhone应用程序。

In AppDelegate.m in the iPhone app I have implemented -(void)session:(WCSession *)session didReceiveUserInfo:(NSDictionary<NSString *,id> *)userInfo 在iPhone应用程序的AppDelegate.m中,我实现了-(void)session:(WCSession *)session didReceiveUserInfo:(NSDictionary<NSString *,id> *)userInfo

When I send a transfer request from my watch, this method gets called (confirmed by a NSLog I created), but not all code is executed. 当我从手表发送传输请求时,此方法会被调用(由我创建的NSLog确认),但并非所有代码都会执行。 To be more specific, it calls the following method to post to Facebook, which also gets called but which will not be completely executed. 更具体地说,它调用以下方法发布到Facebook,该方法也会被调用,但不会完全执行。

- (void)postToFacebook:(NSString*)postMessage {

NSLog(@"begin");

    if ([[FBSDKAccessToken currentAccessToken] hasGranted:@"publish_actions"]) {

        [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me/feed" parameters:@{@"message" : postMessage, @"privacy": @"{'value': 'SELF'}"}  HTTPMethod:@"POST"] startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {

            NSLog(@"done");

        }];

    }

}
}

NSLog "begin" is shown, but NSLog "done" isn't shown. 显示NSLog“开始”,但不显示NSLog“完成”。

I've tested running the method from my iPhone and everything is working fine. 我已经测试了在iPhone上运行该方法的一切,一切正常。 But when I try to call it from the watch using transferUserInfo , it isn't executed properly. 但是,当我尝试使用transferUserInfo从手表中调用它时,它执行不正确。

Now my question is, how I can assure that the background task within the iPhone app isn't killed before the method is completely done. 现在我的问题是,如何确保在该方法完全完成之前不会杀死iPhone应用程序中的后台任务。

A bit of a long shot, but try wrapping the code in a dispatch to the main queue: 有点远,但是尝试将代码包装在分派到主队列中:

- (void)postToFacebook:(NSString*)postMessage {
    dispatch_sync(dispatch_get_main_queue(), ^{
        if ([[FBSDKAccessToken currentAccess...etc...
    });
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在didReceiveUserInfo之前调用TableView安装程序 - TableView setup gets called before didReceiveUserInfo 当 WCSession 的 sendMessage 被触发时,iPhone 应用程序不会在后台唤醒 - iPhone app isn't waking in the background when WCSession's sendMessage is fired 使用WatchKit 2的iOS应用程序中的WCSession停止应用程序构建 - WCSession in iOS App using WatchKit 2 Stops App Building WatchOS2 WCSession sendMessage不会在后台唤醒iPhone - WatchOS2 WCSession sendMessage doesn't wake iPhone on background 应用程序进入后台时调用方法 - Methods gets called when app goes to the background WCSession sendMessage何时无法在后台唤醒iOS应用? - When does WCSession sendMessage fail to wake iOS app in background? 当watchOS应用程序使用HKWorkoutSession在后台运行时如何使WCSession可访问 - How to make WCSession reachable when the watchOS app is running in background with HKWorkoutSession 是否有一个UIViewController方法,当应用程序移动到后台时会自动调用? - Is there a UIViewController method that gets automatically called when the app moves to the background? 当我最小化iPhone中的任何应用程序时,该应用程序是否在后台运行? 或暂停? 或停止应用程序? - When i minimise any app in my iPhone Does that app runs in background? or Pauses? or stops the app ? applicationDidReceiveRemoteNotification未在后台iPhone 7上调用 - applicationDidReceiveRemoteNotification not called on iPhone 7 on background
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM