簡體   English   中英

Healthkit:數據未顯示首次運行。

[英]Healthkit: Data not showing first time running.

從Healthkit讀取數據時遇到一個奇怪的問題。 第一次按下按鈕時砝碼打印為0,下次按下按鈕時砝碼將正常打印。如果有人可以幫助我,我將非常感激。 謝謝!

我有以下代碼。

HealtkitManager.h

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <HealthKit/HealthKit.h>

@interface HealthKitManager : NSObject
@property (nonatomic) float weight;
@property (nonatomic) HKHealthStore *healthStore;

+(HealthKitManager *)sharedManager;

-(void) requestAuthorization;
-(double) readWeight;




@end

Healthkitmanager.m

#import "HealthKitManager.h"
#import <healthKit/healthKit.h>
#import "StartScreen.h"
@interface HealthKitManager ()

@end



@implementation HealthKitManager

+(HealthKitManager *) sharedManager{
    static dispatch_once_t pred = 0;
    static HealthKitManager *instance = nil;
    dispatch_once(&pred, ^{
        instance = [[HealthKitManager alloc]init];
        instance.healthStore = [[HKHealthStore alloc]init];
    });
    return instance;
}

-(void)requestAuthorization {
    if([HKHealthStore isHealthDataAvailable]==NO){
        return;
    }

    NSArray *readTypes = @[[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMass],
                           [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMassIndex],
                           [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyFatPercentage]];

    [self.healthStore requestAuthorizationToShareTypes:nil readTypes:[NSSet setWithArray:readTypes] completion:nil];

}

-(double) readWeight{
    NSMassFormatter *massFormatter = [[NSMassFormatter alloc]init];
    massFormatter.unitStyle = NSFormattingUnitStyleLong;

    HKQuantityType *weightType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMass];

    [self fetchMostRecentDataOfQuantityType:weightType withCompletion:^(HKQuantity *mostRecentQuantity, NSError *error) {
        if(!mostRecentQuantity){
            NSLog(@"%@",@"Error. ");
        }
        else{
            HKUnit *weightUnit = [HKUnit gramUnit];
            _weight = [mostRecentQuantity doubleValueForUnit:weightUnit];
            _weight = (float)_weight/1000.00f;
        }
    }];
    return _weight;
}

- (void)fetchMostRecentDataOfQuantityType:(HKQuantityType *)quantityType withCompletion:(void (^)(HKQuantity *mostRecentQuantity, NSError *error))completion {
    NSSortDescriptor *timeSortDescriptor = [[NSSortDescriptor alloc] initWithKey:HKSampleSortIdentifierEndDate ascending:NO];


    HKSampleQuery *query = [[HKSampleQuery alloc] initWithSampleType:quantityType predicate:nil limit:1 sortDescriptors:@[timeSortDescriptor] resultsHandler:^(HKSampleQuery *query, NSArray *results, NSError *error) {
        if (!results) {
            if (completion) {
                completion(nil, error);
            }

            return;
        }

        if (completion) {

            HKQuantitySample *quantitySample = results.firstObject;
            HKQuantity *quantity = quantitySample.quantity;

            completion(quantity, error);
        }
    }];

    [self.healthStore executeQuery:query];
}


@end

開始畫面

#import "StartScreen.h"
#import "HealthKitManager.h"

@interface StartScreen ()
@property(nonatomic,weak) IBOutlet UILabel *weightLabel;

@end

@implementation StartScreen

- (void)viewDidLoad {
    [super viewDidLoad];
    [[HealthKitManager sharedManager] requestAuthorization];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(IBAction)readAgeButtonPressed:(id)sender{
    HealthKitManager *readWeight = [[HealthKitManager sharedManager] init];
   NSLog(@"%.2f",readWeight.readWeight);

}
@end

您使用單例HealthKitManager,但是您的代碼

HealthKitManager *readWeight = [[HealthKitManager sharedManager] init];

在IBAction中,readAgeButtonPressed應該是

HealthKitManager *readWeight = [HealthKitManager sharedManager];

您不需要init

另外,您可以更改代碼

[[HealthKitManager sharedManager] requestAuthorization];

成為

HealthKitManager *readWeight = [HealthKitManager sharedManager];
[readWeight requestAuthorization];
NSLog(@"%.2f",readWeight.readWeight);

檢查readWeight的值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM