水のサンプルを追加できるウォータートラッキングアプリを作っています。データはApple WatchおよびiPhoneアプリから直接Healthに追加されます。機能の1つは元に戻すことです。アップルウォッチのHealthKitから水サンプルを取り除く
アップルウォッチから水サンプルを取り除こうとしていて、うまくいきます。ただし、サンプルをiPhoneに追加してApple Watchから削除すると、サンプルは削除されません。最悪の部分は、私はエラーを取得していない、HealthKitのコールバックは成功を返します。
- (void)removeSampleWithCompletion:(void (^ _Nonnull)(bool success, NSError *_Nullable error))completion {
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *myDate = [NSDate date];
NSDate *startDate = [calendar startOfDayForDate:myDate];
// the type you're trying to delete (this could be heart-beats/steps/water/calories/etc..)
HKQuantityType *waterType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryWater];
// the predicate used to execute the query
NSPredicate *queryPredicate = [HKSampleQuery predicateForSamplesWithStartDate:startDate
endDate:myDate
options:HKQueryOptionStrictEndDate];
// prepare the query
HKSampleQuery *query = [[HKSampleQuery alloc] initWithSampleType:waterType
predicate:queryPredicate
limit:100
sortDescriptors:nil
resultsHandler:^(HKSampleQuery * _Nonnull query, NSArray<__kindof HKSample *> * _Nullable results, NSError * _Nullable error) {
if (!error) {
// Successfully retreived samples
// Filter only our samples. It is no possible to delete others.
NSMutableArray <HKSample *>*locassaSamples = [NSMutableArray new];
for (HKSample *sample in results) {
if ([sample.sourceRevision.source.bundleIdentifier isEqual:@"com.companyname.appname"]) {
[locassaSamples addObject:sample];
}
}
// Remove last
HKSample *theLast = locassaSamples.lastObject;
if (theLast == nil) {
return;
}
[self.healthStore deleteObject:theLast
withCompletion:^(BOOL success, NSError * _Nullable error) {
completion(success, error);
}];
return;
} else {
completion(FALSE, error);
}
}];
// Source http://stackoverflow.com/a/38263174/1162044
// last but not least, execute the query
[self.healthStore executeQuery:query];
}
更新。
私はレーダー#28007271を作成しました。
サンプルを削除した後でも、それはどこに気付いていますか?電話か時計? – Allan
彼らは電話でヘルスアプリに滞在します。 –