watchos-2 - 要添加WatchOS 3快照功能,永远不会触发handleBackgroundTasks和scheduleSnapshotRefreshWithPreferredDate

标签 watchos-2 snapshot watchos-3 xcode8-beta3 wkrefreshbackgroundtask

我需要添加要放置在 Dock 中的 watch 应用程序的快照,因为它是一项新功能,我找不到任何资源来指导我。我已阅读Apple的文档https://developer.apple.com/library/prerelease/content/samplecode/WatchBackgroundRefresh/Introduction/Intro.html我已经根据 Apple 文档在 Watch 应用程序的主 InterfaceController 中实现了此代码:https://developer.apple.com/library/prerelease/content/samplecode/WatchBackgroundRefresh/Introduction/Intro.html 但从未调用过handleBackgroundTasks。难道我做错了什么?我可以将另一个 InterfaceController 而不是主 InterfaceController 的屏幕截图放置在 Dock 中吗?如何?

@interface ExtensionDelegate : NSObject <WKExtensionDelegate>

@property (nonatomic, weak) MainInterfaceController *interfaceController;

@implementation ExtensionDelegate

-(void)applicationDidEnterBackground {

    [self scheduleRefreshBackgroundTask];
    [self scheduleSnapshotRefreshBackgroundTask];
  }

-(void)scheduleRefreshBackgroundTask WK_AVAILABLE_WATCHOS_ONLY(3.0){

// Background Refresh

NSDictionary *backgroundRefreshUserInfo = @{@"reason": @"Bakcground Refresh"};

NSDate *backgroundRefreshDate = [NSDate dateWithTimeIntervalSinceNow:30 * 60];

[[WKExtension sharedExtension] scheduleBackgroundRefreshWithPreferredDate:backgroundRefreshDate
                                                                 userInfo:backgroundRefreshUserInfo
                                                      scheduledCompletion:^(NSError *error){

    if (error == nil) {

        DMLogVerbose(DebugLogTypeWatch, @"successfully scheduled background refresh tesk");

    } else {

        DMLogError(DebugLogTypeWatch, @"unable to schedule background refresh task, error:%@", error);

    }

}];

}

-(void)scheduleSnapshotRefreshBackgroundTask WK_AVAILABLE_WATCHOS_ONLY(3.0){

//fire in 1 hr

NSDate *inputDate = [NSDate date];

NSDate *fireDate = [inputDate initWithTimeIntervalSinceNow:1 * 60 * 60];

//optional.. any sourceCoding compliant data can be passed here

// SnapShot

NSDictionary *userInfo = @{@"reason": @"Snapshot Refresh"};

[[WKExtension sharedExtension] scheduleSnapshotRefreshWithPreferredDate:fireDate
                                                               userInfo:userInfo
                                                    scheduledCompletion:^(NSError *error){

if (error == nil) {
                                                            DMLogVerbose(DebugLogTypeWatch, @"successfully scheduled background tesk, use the crown to send the app to the background and wait for handle:backgroundTasks to fire.");
                   }
 }];

 }

-(void)handleBackgroundTasks:(NSSet<WKRefreshBackgroundTask *> *)backgroundTasks WK_AVAILABLE_WATCHOS_ONLY(3.0) {

for (WKRefreshBackgroundTask *task in backgroundTasks) {

    if ([[WKExtension sharedExtension] applicationState] == WKApplicationStateBackground) {

        // Snapshot
        if ([task isKindOfClass:[WKSnapshotRefreshBackgroundTask class]]) {

            if (self.interfaceController) {

                [self.interfaceController updateData:YES];

                [self.interfaceController initializeDefaultUI];

                [self scheduleSnapshotRefreshBackgroundTask];

            }

        }

        // Connectivity Refresh
        else if ([task isKindOfClass:[WKWatchConnectivityRefreshBackgroundTask class]]) {

            // do something

        }

        // Network (URLSession)
        else if ([task isKindOfClass:[WKURLSessionRefreshBackgroundTask class]]) {

            // Resume download

            NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:[DataManager watchDataURLSessionIdentifier]];

            NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfig
                                                                  delegate:
                                     [TWNDataManager sharedDataManager].currentWatchDataDownloader
                                                             delegateQueue:nil];

            DMLogVerbose(DebugLogTypeWatch, @"Resume watch download session in background:%@", session);

        }
        // Background Refresh
        else if ([task isKindOfClass:[WKApplicationRefreshBackgroundTask class]]){

            NSDictionary *userInfo = [task userInfo];

            NSString *reason = [userInfo objectForKey:@"reason"];

            if ([reason isEqualToString:@"Bakcground Refresh"]) {

                if (self.interfaceController && !self.isBackgroundUpdating) {

                    self.isBackgroundUpdating = YES;

                    [self.interfaceController updateData:YES];

                    self.isBackgroundUpdating = NO;

                    [self scheduleRefreshBackgroundTask];

                }
            }
        }
    }

    [task setTaskCompleted];
}
}

最佳答案

我也有同样的问题。我也测试了Apple的示例代码https://developer.apple.com/library/prerelease/content/samplecode/WatchBackgroundRefresh/Introduction/Intro.html并且 handle(_:) 方法未被调用。直到我重新启动模拟器几次,清理产品,做平常的事情。然后我让它工作了,但只有 30 次测试中只有一次该方法正常运行(完成下载后安排快照请求,然后唤醒应用程序)。我已经填写了一份错误报告,我希望在下一个测试版本中看到这个问题得到修复,因为我在测试中浪费了很多时间,而这些测试并没有按照文档进行。

关于watchos-2 - 要添加WatchOS 3快照功能,永远不会触发handleBackgroundTasks和scheduleSnapshotRefreshWithPreferredDate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38552465/

相关文章:

ios - 在 Watch 应用程序使用的文件中有条件地使用 UIKit

Linux LVM 快照脏 block 位图

java - 自动视频快照

swift - 我们可以在 watchOS 中重叠 UI 元素吗?

ios - 与 WatchOS 共享 Realm 数据

watchkit - watchOS 2.0 运行 watchOS 1.0 应用程序?

ios - 我可以使用 watchOS2 从 WatchKit 应用程序发送网络电话吗?

ios - WatchOS 唤醒 iOS 应用程序以使用配对的蓝牙耳机开始录音

在 MANIFEST 文件中使用 Maven 程序集的 Maven SNAPSHOT jar 文件名不一致

ios - 如何在 Apple Watch 模拟器上强制调用获取占位符模板以更新复杂功能预览