亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

應用未運行時的Healthkit后臺傳遞

應用未運行時的Healthkit后臺傳遞

白豬掌柜的 2019-11-28 13:46:33
如果HealthKit后臺交付未運行,是否可以啟動該應用程序?特別是在終止狀態下?
查看完整描述

3 回答

?
鳳凰求蠱

TA貢獻1825條經驗 獲得超4個贊

在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];

}


查看完整回答
反對 回復 2019-11-28
  • 3 回答
  • 0 關注
  • 917 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號