尝试设置 healthkit 权限以访问/读取用户的健康记录临床数据,如过敏和状况等。
关注文章并为 Xamarin 翻译。
https://developer.apple.com/documentation/healthkit/samples/accessing_a_user_s_clinical_records
以下代码不起作用并给我错误。
NSSet HealthRecordDataTypesToRead
{
get
{
return NSSet.MakeNSObjectSet<HKSampleType>(new HKSampleType[] {
//Clinical Record
HKObjectType.GetClinicalType(HKClinicalTypeIdentifier.ConditionRecord),
HKObjectType.GetClinicalType(HKClinicalTypeIdentifier.AllergyRecord),
HKObjectType.GetClinicalType(HKClinicalTypeIdentifier.MedicationRecord),
HKObjectType.GetClinicalType(HKClinicalTypeIdentifier.ProcedureRecord),
HKObjectType.GetClinicalType(HKClinicalTypeIdentifier.LabResultRecord),
HKObjectType.GetClinicalType(HKClinicalTypeIdentifier.ImmunizationRecord),
HKObjectType.GetClinicalType(HKClinicalTypeIdentifier.VitalSignRecord),
});
}
}
var (ishrAllowed, hrerror) = await HealthStore.RequestAuthorizationToShareAsync(new NSSet(), HealthRecordDataTypesToRead);
if (hrerror != null)
throw new Exception($"There is an error during permssion request. Error Description: {error.LocalizedDescription}");
result = ishrAllowed;
错误
Foundation.MonoTouchException:抛出 Objective-C 异常。Name: NSInvalidArgumentException Reason: Authorization to read the following types is disallowed: HKClinicalTypeIdentifierMedicationRecord, HKClinicalTypeIdentifierConditionRecord, HKClinicalTypeIdentifierProcedureRecord, HKClinicalTypeIdentifierVitalSignRecord, HKClinicalTypeIdentifierImmunizationRecord, HKClinicalTypeIdentifierAllergyRecord, HKClinicalTypeIdentifierLabResultRecord
Native stack trace:
\t0 CoreFoundation 0x00007fff2043a126 __exceptionPreprocess + 242
\t1 libobjc.A.dylib 0x00007fff20177f78 objc_exception_throw + 48
\ t2 CoreFoundation 0x00007fff2043a004 -[NSException initWithCoder:] + 0
\t3 HealthKit 0x00007fff35315e22 -[HKHealthStore _throwIfAuthorizationDisallowedForSharing:types:] + 534
\t4 HealthKit 0x00007fff35314a4b -[HKHealthStore _validateAuthorizationRequestWithShareTypes:readTypes:] + 224
\t5 HealthKit 0x00007fff35314bca -[HKHealthStore requestAuthorizationToShareTypes:readTypes:shouldPrompt:completion:] + 336
\t6 HealthKit 0x00007fff35314948 -[HKHealthStore requestAuthorizationToShareTypes:readTypes:completion:] + 77
\t7 Health.iOS 0x000000010663b969 xamarin_dyn_objc_msgSend + 217
\t8 ??? 0x000000011abad00b 0x0 + 4743417867
在 ObjCRuntime.Runtime.ThrowNSException (System.IntPtr ns_exception) [0x00000] 在 /Library/Frameworks/Xamarin.iOS.framework/Versions/14.4.1.3/src/Xamarin.iOS/ObjCRuntime/Runtime.cs:407
在 ObjCRuntime.Runtime .throw_ns_exception (System.IntPtr exc) [0x00000] in /Users/builder/jenkins/workspace/xamarin-macios/xamarin-macios/runtime/Delegates.generated.cs:126
at (wrapper native-to-managed) ObjCRuntime.Runtime .throw_ns_exception(intptr)
at (wrapper managed-to-native) ObjCRuntime.Messaging.void_objc_msgSend_IntPtr_IntPtr_IntPtr(intptr,intptr,intptr,intptr,intptr)
at HealthKit.HKHealthStore.RequestAuthorizationToShare (Foundation.NSSet typesToShare, Foundation.NSSet typesToRead, System.Action2[T1,T2] completion) [0x0002f] in /Library/Frameworks/Xamarin.iOS.framework/Versions/14.4.1.3/src/Xamarin.iOS/HealthKit/HKHealthStore.g.cs:619 \ at HealthKit.HKHealthStore.RequestAuthorizationToShareAsync (Foundation.NSSet typesToShare, Foundation.NSSet typesToRead) [0x00011] in /Library/Frameworks/Xamarin.iOS.framework/Versions/14.4.1.3/src/Xamarin.iOS/HealthKit/HKHealthStore.g.cs:631 \ at Health.iOS.DependencyService.HealthKitData.GetHealthPermissionAsync () [0x0014f] in C:\\\\Repos\\\\Health.Master\\\\Mobile\\\\Health.iOS\\\\DependencyServices\\\\HealthKitData.cs:322 \ at (wrapper managed-to-native) ObjCRuntime.Messaging.void_objc_msgSend_IntPtr_IntPtr_IntPtr(intptr,intptr,intptr,intptr,intptr)\ at HealthKit.HKHealthStore.RequestAuthorizationToShare (Foundation.NSSet typesToShare, Foundation.NSSet typesToRead, System.Action2[T1,T2] 完成)[0x0002f] 在 /Library/Frameworks/Xamarin.iOS.framework/Versions/14.4.1.3/src/Xamarin.iOS/HealthKit/HKHealthStore.g.cs:619
at HealthKit.HKHealthStore.RequestAuthorizationToShareAsync (Foundation.NSSet typesToShare, Foundation.NSSet typesToRead) [0x00011] 在 /Library/Frameworks/Xamarin.iOS.framework/Versions/14.4.1.3/src/Xamarin.iOS/HealthKit/HKHealthStore.g.cs:631
在健康。 iOS.DependencyService.HealthKitData.GetHealthPermissionAsync () [0x0014f] 在 C:\\Repos\\Health.Master\\Mobile\\Health.iOS\\DependencyServices\\HealthKitData.cs:322
在 C:\\Repos\\Health.Master\\Mobile\\Health 中的 Health.Core.ViewModels.Trackers.TrackerListMenuViewModel.ProcessTrackerSelection (Health.Core.Helpers.Ui.UiDataItem selectedItem, System.Boolean saveDeviceChanges) [0x001b5]。 Core\\ViewModels\\Trackers\\TrackerListMenuViewModel.cs:304 \t0 CoreFoundation 0x00007fff2043a126 __exceptionPreprocess + 242
\t1 libobjc.A.dylib 0x00007fff20177f78 objc_exception_throw + 48
\t2 CoreFoundation 0x00007fff2043a004 -[NSException initWithCoder:] + 0
\t3 HealthKit 0x00007fff35315e22 -[ HKHealthStore _throwIfAuthorizationDisallowedForSharing:types:] + 534
\t4 HealthKit 0x00007fff35314a4b -[HKHealthStore _validateAuthorizationRequestWithShareTypes:readTypes:] + 224
\t5 HealthKit 0x00007fff35314bca -[HKHealthStore requestAuthorizationToShareTypes:readTypes:shouldPrompt:completion:] + 336
\t6 HealthKit 0x00007fff35314948 -[HKHealthStore requestAuthorizationToShareTypes:readTypes:completion:] + 77
\t7 Health.iOS 0x000000010663b969 xamarin_dyn_objc_msgSend + 217
\t8 ??? 0x000000011abad00b 0x0 + 4743417867\
** info.plist 条目 **
<string>Health App needs Health Share Access</string>
<key>NSHealthUpdateUsageDescription</key>
<string>Health App needs Health Update Access</string>
<key>NSHealthRequiredReadAuthorizationTypeIdentifiers</key>
<array>
<string>HKClinicalTypeIdentifierAllergyRecord</string>
<string>HKClinicalTypeIdentifierConditionRecord</string>
<string>HKClinicalTypeIdentifierImmunizationRecord</string>
<string>HKClinicalTypeIdentifierLabResultRecord</string>
<string>HKClinicalTypeIdentifierMedicationRecord</string>
<string>HKClinicalTypeIdentifierProcedureRecord</string>
<string>HKClinicalTypeIdentifierVitalSignRecord</string>
</array>
<key>NSHealthClinicalHealthRecordsShareUsageDescription</key>
<string>Health App needs your clinical Health Record Access</string> ```