Open HealthKit App from another app
Asked Answered
K

4

17

I want to build a fitness app that will upload the data to HealthKit. Is there any way to open/navigate to HealthKit from another app?

Kocher answered 12/8, 2014 at 8:26 Comment(0)
P
22

On iOS 10, the Health app URL scheme is x-apple-health. You can open it from within your own app by calling:

Objective-C:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"x-apple-health://"]];

Swift:

UIApplication.shared.open(URL(string: "x-apple-health://")!)

See Open Health app using url scheme | Apple Developer Forums.

Pellegrino answered 8/11, 2016 at 11:37 Comment(4)
Only work on iOS 10+. I don't think the Health app on iOS 8 or 9 has a registered URL schema.Tremendous
@Tremendous You're right, it doesn't seem to work on iOS 8 or 9. I've updated my answer. Thanks!Pellegrino
Does anyone know how to open a special view? Like steps or weight?Boaster
This is not officially documented or supported, and could change at any time. I don't recommend you use this.Matador
M
4

iOS does not provide a general API for launching other applications and the Health app would need to have support for URL scheme in order for you to launch it from your own application. See Launch an app from within another (iPhone).

Matador answered 30/8, 2014 at 21:3 Comment(0)
I
3

Swift 5 "Safer way"

func openUrl(urlString: String) {
    guard let url = URL(string: urlString) else {
        return
    }

    if UIApplication.shared.canOpenURL(url) {
        UIApplication.shared.open(url, options: [:], completionHandler: nil)
    }
}

Usage:

openUrl(urlString: "x-apple-health://")
Intersex answered 20/11, 2019 at 15:23 Comment(0)
W
0

You can open the Settings>Health>Source path in iOS 12 or higher. And you can find MyApp in the list and change the Access permission. You can follow these steps:

extension UIApplication {

    func openAppleHealthSources() {
        guard let url = URL(string: "App-prefs:HEALTH&path=SOURCES"),
              self.canOpenURL(url) else {
            return
        }
        self.open(url)
    }
}

Usage:

UIApplication.shared.openAppleHealthSources()

But this might lead to your app being rejected during the review process...

Your app uses the "prefs:root=" non-public URL scheme, which is a private entity. The use of non-public APIs is not permitted on the App Store because it can lead to a poor user experience should these APIs change. Continuing to use or conceal non-public APIs in future submissions of this app may result in the termination of your Apple Developer account, as well as removal of all associated apps from the App Store.

Waggon answered 6/5 at 0:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.