###前提・実現したいこと
AppleWatchの実機でバックグラウンドタスクを動作させたい。
自分では正しく実装できてると思っているのですが、実機だとどうしても上手く行きません。シミュレータでは正常に動作するので、原因が特定できない状況です。
検索しても情報自体が少ないため、ご助力頂けますでしょうか。
よろしくお願いいたします。
###問題
ExtensionDelegateクラスにて、バックグラウンドタスクのスケジュールを設定し、アプリをバックグラウンド状態に移行させても、handleBackgroundTasks:メソッドが呼ばれない。
###該当のソースコード
Objective
1#import "ExtensionDelegate.h" 2 3@implementation ExtensionDelegate{ 4 int labelUpdateCount; 5} 6 7- (void)applicationDidFinishLaunching { 8 // Perform any final initialization of your application. 9 } 10} 11 12- (void)applicationDidBecomeActive { 13 // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 14} 15 16- (void)applicationWillResignActive { 17 // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 18 // Use this method to pause ongoing tasks, disable timers, etc. 19} 20 21-(void)applicationDidEnterBackground{ 22 NSLog(@"バックグラウンドへ移行"); 23 [self scheduleTest]; 24} 25 26-(void)testLabelUpdate{ 27 28} 29 30-(void)scheduleTest { 31 NSLog(@"バックグラウンドタスクスケジュール設定"); 32 NSDate *fireDate = [NSDate dateWithTimeIntervalSinceNow:(NSTimeInterval)60]; 33 34 NSDictionary *userInfo = @{@"reason": @"Background Refresh"}; 35 [[WKExtension sharedExtension] scheduleBackgroundRefreshWithPreferredDate: fireDate 36 userInfo: userInfo 37 scheduledCompletion: ^(NSError *error){ 38 if (error == nil) { 39 NSLog(@"スケジュール設定成功"); 40 }else{ 41 NSLog(@"スケジュール設定失敗"); 42 } 43 }]; 44} 45 46-(void)updateData{ 47 NSLog(@"バックグラウンドタスク実行成功"); 48} 49 50- (void)handleBackgroundTasks:(NSSet<WKRefreshBackgroundTask *> *)backgroundTasks { 51 // Sent when the system needs to launch the application in the background to process tasks. Tasks arrive in a set, so loop through and process each one. 52 for (WKRefreshBackgroundTask * task in backgroundTasks) { 53 // Check the Class of each task to decide how to process it 54 if ([task isKindOfClass:[WKApplicationRefreshBackgroundTask class]]) { 55 // Be sure to complete the background task once you’re done. 56 WKApplicationRefreshBackgroundTask *backgroundTask = (WKApplicationRefreshBackgroundTask*)task; 57 //バックグラウンドタスク実行 58 [self updateData]; 59 [backgroundTask setTaskCompleted]; 60 } else if ([task isKindOfClass:[WKSnapshotRefreshBackgroundTask class]]) { 61 // Snapshot tasks have a unique completion call, make sure to set your expiration date 62 WKSnapshotRefreshBackgroundTask *snapshotTask = (WKSnapshotRefreshBackgroundTask*)task; 63 [snapshotTask setTaskCompletedWithDefaultStateRestored:YES estimatedSnapshotExpiration:[NSDate distantFuture] userInfo:nil]; 64 } else if ([task isKindOfClass:[WKWatchConnectivityRefreshBackgroundTask class]]) { 65 // Be sure to complete the background task once you’re done. 66 WKWatchConnectivityRefreshBackgroundTask *backgroundTask = (WKWatchConnectivityRefreshBackgroundTask*)task; 67 [backgroundTask setTaskCompleted]; 68 } else if ([task isKindOfClass:[WKURLSessionRefreshBackgroundTask class]]) { 69 // Be sure to complete the background task once you’re done. 70 WKURLSessionRefreshBackgroundTask *backgroundTask = (WKURLSessionRefreshBackgroundTask*)task; 71 [backgroundTask setTaskCompleted]; 72 } else { 73 // make sure to complete unhandled task types 74 [task setTaskCompleted]; 75 } 76 } 77} 78@end 79
###試したこと
・InterfaceControllerクラスにて、同様の実装を行ったがhandleBackGroundTasks:メソッドは実行されなかった。この際、InterfaceControllerクラスのawakeWithContext:メソッド内に下記の処理を実装していた。
//WKExtensionのデリゲートを自分自身にセット
[[WKExtension sharedExtension]setDelegate:self];
・Xcodeのシミュレータで実行したところ、正常に動作した。
###補足情報(言語/FW/ツール等のバージョンなど)
【テストに使用した端末】
AppleWatch 38mm WatchOS 3.0 beta3
AppleWatch 42mm WatchOS 3.0 beta6
【開発環境】
macOS 10.12 Beta3
Xcode8 beta6(beta3,4,5でも上手く行かなかった)
【その他】
実機デバッグを行うと、なぜかデバッグコンソールにNSLogが出力されない。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。