猿问

应用未运行时的Healthkit后台传递

如果HealthKit后台交付未运行,是否可以启动该应用程序?特别是在终止状态下?



白猪掌柜的
浏览 857回答 3
3回答

凤凰求蛊

在iOS 8.1中可以。不过,您需要确保在应用程序委托的中重新创建观察者查询application:didFinishLaunchingWithOptions:。8.0中的错误完全阻止了HealthKit的后台通知。编辑:在您的AppDelegate中:- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    //create/get your HKHealthStore instance (called healthStore here)    //get permission to read the data types you need.    //define type, frequency, and predicate (called type, frequency, and predicate here, appropriately)    UIBackgroundTaskIdentifier __block taskID = [application beginBackgroundTaskWithExpirationHandler:^{        if (taskID != UIBackgroundTaskInvalid) {            [application endBackgroundTask:taskID];            taskID = UIBackgroundTaskInvalid;        }    }];    [healthStore enableBackgroundDeliveryForType:type frequency:frequency withCompletion:^(BOOL success, NSError *error) {}];    HKQuery *query = [[HKObserverQuery alloc] initWithSampleType:healthType predicate:predicate updateHandler:        ^void(HKObserverQuery *query, HKObserverQueryCompletionHandler completionHandler, NSError *error)        {            //If we don't call the completion handler right away, Apple gets mad. They'll try sending us the same notification here 3 times on a back-off algorithm.  The preferred method is we just call the completion handler.  Makes me wonder why they even HAVE a completionHandler if we're expected to just call it right away...            if (completionHandler) {                completionHandler();            }            //HANDLE DATA HERE            if (taskID != UIBackgroundTaskInvalid) {                [application endBackgroundTask:taskID];                taskID = UIBackgroundTaskInvalid;            }        }];    [healthStore executeQuery:query];}
随时随地看视频慕课网APP
我要回答