iOS HealthKit,微信修改步數(shù),QQ修改步數(shù)

1.Linked Frameworks and Libraries 里添加 HealthKit.framework
2.需要一個(gè)開發(fā)者賬號(hào),在生成描述文件的時(shí)候,選中HealthKit
3.代碼實(shí)現(xiàn):

UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, 320, 40)];
[self.view addSubview:lable];
HKHealthStore *health= [[HKHealthStore alloc] init];
NSSet *helathkit =[NSSet setWithArray:[NSArray arrayWithObjects:  [HKObjectType quantityTypeForIdentifier:@"HKQuantityTypeIdentifierStepCount"], nil]];
;
[health requestAuthorizationToShareTypes:helathkit readTypes:helathkit completion:^(BOOL tt,NSError *error){
    /**
     *  @author 付強(qiáng), 16-05-13 13:05:43
     *
     *  寫入健康數(shù)據(jù)
     *
     *  @return return value description
     */
    HKQuantityType *ty = [HKQuantityType quantityTypeForIdentifier:@"HKQuantityTypeIdentifierStepCount"];
            NSDate *date = [NSDate date];
            HKQuantity *witre = [HKQuantity quantityWithUnit:[HKUnit countUnit] doubleValue:80000.0];
            HKQuantitySample *samplequan = [HKQuantitySample quantitySampleWithType:ty quantity:witre startDate:date endDate:date];
            [health saveObject:samplequan withCompletion:^(BOOL tt,NSError *error){
                if (tt) {
                    NSLog(@"寫入成功");
                }
            }];
    /**
     獲取健康數(shù)據(jù)
     
     - returns: return value description
     */
    HKSampleType *sampleType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];
    NSSortDescriptor *start = [NSSortDescriptor sortDescriptorWithKey:HKSampleSortIdentifierStartDate ascending:NO];
    NSSortDescriptor *end = [NSSortDescriptor sortDescriptorWithKey:HKSampleSortIdentifierEndDate ascending:NO];
    NSDate *now = [NSDate date];
    NSDateFormatter *dateBJ=[[NSDateFormatter alloc] init];
    [dateBJ setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *str = [dateBJ stringFromDate:now];
    str = [str substringToIndex:10];
    NSString *startda = [NSString stringWithFormat:@"%@ 00:00:00",str];
    NSDate *d=[dateBJ dateFromString:startda];
    NSPredicate *predicate = [HKQuery predicateForSamplesWithStartDate:d endDate:now options:HKQueryOptionNone];
    HKSampleQuery *sampleQuery = [[HKSampleQuery alloc] initWithSampleType:sampleType predicate:predicate limit:0 sortDescriptors:@[start,end] resultsHandler:^(HKSampleQuery * _Nonnull query, NSArray<__kindof HKSample *> * _Nullable results, NSError * _Nullable error) {
        NSInteger num = 0;
        for (int i = 0; i < results.count; i++) {
            HKQuantitySample *result = results[i];
            NSString *das = (NSString *)result;
            NSString *dass = [NSString stringWithFormat:@"%@",das];
            NSString *endtime = [dass substringFromIndex:dass.length - 25];
            NSString *startt = [dass substringFromIndex:dass.length -51];
            NSString *startime = [startt substringToIndex:25];
            if (![endtime isEqualToString:startime]) {
                HKQuantity *quantity = result.quantity;
                NSString *stepStr = (NSString *)quantity;
                
                NSString *sad = [NSString stringWithFormat:@"%@ ",stepStr];
                sad = [sad substringToIndex:sad.length - 7];
                NSInteger geshu = [sad integerValue];
                num = num + geshu;
            }
        }
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            
            //查詢是在多線程中進(jìn)行的,如果要對(duì)UI進(jìn)行刷新,要回到主線程中
            //                    NSLog(@"最新步數(shù):%@",stepStr);
            lable.text = [NSString stringWithFormat:@"你的步數(shù)%ld",(long)num];
        }];
        
    }];
    [health executeQuery:sampleQuery];
    
}];

4.大功告成,目前寫入微信步數(shù)不好用。但是QQ的可以寫入步數(shù)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 近日,看到糯米粉寫的文章iOS 教你如何修改微信運(yùn)動(dòng)步數(shù) ,趁著五一放假,也想來(lái)玩一下改微信運(yùn)動(dòng)步數(shù),占領(lǐng)一下朋友...
    我系哆啦閱讀 160,412評(píng)論 14 68
  • 現(xiàn)在很多軟件都加上了運(yùn)動(dòng)模塊,比如QQ和微信,而且還有排行榜,可以和好友比較誰(shuí)的運(yùn)動(dòng)步數(shù)多,任何東西只要添加了比較...
    idage閱讀 21,764評(píng)論 19 32
  • 昨天上QQ玩QQ步數(shù)的時(shí)候發(fā)現(xiàn)很多小伙伴的步數(shù)高的嚇人,而我僅僅可憐的三位數(shù),于是我就想我怎么能把這個(gè)步數(shù)刷刷,這...
    CoderZNB閱讀 8,484評(píng)論 6 3
  • [iOS] HealthKit 獲取步數(shù) 前言 HealthKit 是蘋果在 iOS 8.0 之后推出的健康框架,...
    李小六_閱讀 1,556評(píng)論 0 7
  • 文:耳雙生 我們已走得太遠(yuǎn),以至于忘記了來(lái)時(shí)的路。 無(wú)從考據(jù)這句話是誰(shuí)開始用的,似乎是一首歌的歌詞。 這句話,細(xì)想...
    耳雙生閱讀 394評(píng)論 0 0

友情鏈接更多精彩內(nèi)容