2014-09-24 110 views
2
-(void)viewDidAppear:(BOOL)animated 
{ 

[super viewDidAppear:animated]; 
if ([HKHealthStore isHealthDataAvailable]){ 
    NSSet *writeDataTypes = [self dataTypesToWrite]; 
    NSSet *readDataTypes = [self dataTypesToRead]; 

    [self.healthStore requestAuthorizationToShareTypes:writeDataTypes readTypes:readDataTypes completion:^(BOOL success, NSError *error) { 

     NSLog(@"%s",__func__); 
     if (!success) { 
      NSLog(@"You didn't allow HealthKit to access these read/write data types. In your app, try to handle this error gracefully when a user decides not to provide access. The error was: %@. If you're using a simulator, try it on a device.", error); 
      return; 
     } 

     dispatch_async(dispatch_get_main_queue(), ^{ 
      // Update the user interface based on the current user's health information. 
      NSLog(@"=========================== %s",__func__); 
     }); 
    }]; 
} 


} 

requestAuthorizationToShareTypes不調用回完成方法。requestAuthorizationToShareTypes方法在iOS 8中不顯示權限提示Xcode 6

+0

如果紙張永遠不會出現,並且永遠不會調用完成後,那麼這聽起來像一個你應該提交的bug(bugreporter.apple.com)。你有沒有試過從設備看控制檯輸出,看看是否有任何記錄錯誤? – Allan 2014-09-25 04:28:37

+0

是的,我在控制檯上什麼也沒做。 – 2014-09-25 08:43:02

+0

我有同樣的問題,所以我崩潰:HTTPS://devforums.apple.com/thread/248835 TSTART = 0 – marciokoko 2014-10-02 17:50:46

回答

4

我有沒有出現在權限框中類似的問題,並沒有設立HKHealthStore得當,把這個事先定它爲我

self.healthStore = [[HKHealthStore alloc] init]; 
2

這是一個樣例實現,它返回類型而不是字符串,如註釋部分所述。

-(NSSet*)datatypesToWrite { 

    NSArray *quantityTypes = 
    @[HKQuantityTypeIdentifierHeartRate, 
    HKQuantityTypeIdentifierBodyTemperature, 
    HKQuantityTypeIdentifierBloodPressureSystolic, 
    HKQuantityTypeIdentifierBloodPressureDiastolic, 
    HKQuantityTypeIdentifierRespiratoryRate]; 

    NSMutableArray *hkTypes = [[NSMutableArray alloc] init]; 

    for (NSString *identifier in quantityTypes) { 
    HKQuantityType *quantType = 
     [HKObjectType quantityTypeForIdentifier:identifier]; 
    [hkTypes addObject:quantType]; 
    } 

    // Make sure the types are of the correct style (Quantity, Category, Etc.) 
    HKCategoryType *catType = 
    [HKObjectType categoryTypeForIdentifier:HKCategoryTypeIdentifierSleepAnalysis]; 
    [hkTypes addObject:catType]; 

    return [[NSSet alloc] initWithArray:hkTypes]; 
} 

您要求的新類型,第一次模態對話框的權限將會出現(但不會再次出現,如果你重新提示權限未授予)各一次。蘋果的指導方針是提示您可能需要的所有內容,但如果我知道某人只是要求保存其中的一些內容,那麼對於我來說最好要求12種類型。