새로운 기능이므로 Dock에 배치 할 시계 응용 프로그램의 스냅 샷을 추가해야합니다. 가이드 리소스가 없습니다. Apple의 설명서 https://developer.apple.com/library/prerelease/content/samplecode/WatchBackgroundRefresh/Introduction/Intro.html을 읽었으며 Apple 설명서에 기반한 Watch 응용 프로그램의 기본 InterfaceController에서이 코드를 구현했지만 handleBackgroundTasks는 호출되지 않습니다. https://developer.apple.com/library/prerelease/content/samplecode/WatchBackgroundRefresh/Introduction/Intro.html . 내가 뭔가 잘못하고 있는거야? 기본 InterfaceController가 아닌 Dock에있는 다른 InterfaceController의 스크린 샷을 사용할 수 있습니까? 방법?WatchOS 3 스냅 샷 기능을 추가하려면 handleBackgroundTasks 및 scheduleSnapshotRefreshWithPreferredDate가 실행되지 않습니다.
@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 – Chiara
많은 도움이되었습니다. 공유 해줘서 고마워! – Mona