Skip to content

Commit

Permalink
feat: Add method to open native health settings
Browse files Browse the repository at this point in the history
  • Loading branch information
Marc Sanny committed Sep 1, 2023
1 parent 5cc3b59 commit 45f5808
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1393,6 +1393,7 @@ class HealthPlugin(private var channel: MethodChannel? = null) :
when (call.method) {
"useHealthConnectIfAvailable" -> useHealthConnectIfAvailable(call, result)
"checkAvailability" -> checkAvailability(call, result)
"openSystemSettings" -> openSystemSettings(call, result)
"hasPermissions" -> hasPermissions(call, result)
"requestAuthorization" -> requestAuthorization(call, result)
"revokePermissions" -> revokePermissions(call, result)
Expand Down Expand Up @@ -1442,6 +1443,18 @@ class HealthPlugin(private var channel: MethodChannel? = null) :
result?.success(healthConnectAvailable)
}

fun openSystemSettings(call: MethodCall, result: Result) {
try {
val intent = Intent()
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
intent.action = HealthConnectClient.ACTION_HEALTH_CONNECT_SETTINGS
context?.startActivity(intent)
result.success(true)
} catch (e: Throwable) {
result.error("UNABLE_TO_START_ACTIVITY", e.message, e)
}
}

fun useHealthConnectIfAvailable(call: MethodCall, result: Result) {
useHealthConnectIfAvailable = true
result.success(null)
Expand Down
8 changes: 8 additions & 0 deletions packages/health/ios/Classes/SwiftHealthPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ public class SwiftHealthPlugin: NSObject, FlutterPlugin {
else if call.method.elementsEqual("requestAuthorization") {
try! requestAuthorization(call: call, result: result)
}

else if call.method.elementsEqual("openSystemSettings") {
openSystemSettings(call: call, result: result)
}

/// Handle getData
else if call.method.elementsEqual("getData") {
Expand Down Expand Up @@ -182,6 +186,10 @@ public class SwiftHealthPlugin: NSObject, FlutterPlugin {
func checkIfHealthDataAvailable(call: FlutterMethodCall, result: @escaping FlutterResult) {
result(HKHealthStore.isHealthDataAvailable())
}

func openSystemSettings(call: FlutterMethodCall, result: @escaping FlutterResult) {
UIApplication.shared.open(URL(string: "x-apple-health://")!)
}

func hasPermissions(call: FlutterMethodCall, result: @escaping FlutterResult) throws {
let arguments = call.arguments as? NSDictionary
Expand Down
9 changes: 9 additions & 0 deletions packages/health/lib/src/health_factory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,15 @@ class HealthFactory {
return result ?? false;
}

/// Opens native system settings for:
/// - Health on iOS
/// - Health Connect on Android
///
/// Throws if the application is not installed on the device.
Future<void> openSystemSettings() async {
await _channel.invokeMethod('openSystemSettings');
}

/// Requests permissions to access data types in Apple Health or Google Fit.
///
/// Returns true if successful, false otherwise
Expand Down

0 comments on commit 45f5808

Please sign in to comment.