提供如下几种方式去开启华为运动健康App对Health Service Kit的开放授权:
开发者调用Health Service Kit云侧提供的H5页面关联华为运动健康服务的操作如下:
https://oauth-login.cloud.huawei.com/oauth2/v3/authorize?response_type=code&client_id=106804381&redirect_uri=https%3A%2F%2Fh5hosting.dbankcdn.com%2Fcch5%2Fhealthkit%2Flink%2Fpages%2FprivacyManage.html&access_type=offline&display=touch&state=zh-cn
其中,可供开发者修改的参数如下:
| 参数名称 | 是否必选 | 参数说明 |
|---|---|---|
| display | 否 | 授权页面展示风格规则:PC端为“page”、移动端为“touch”。默认为“page”。 |
| state | 否 | 用于字体语言种类展示,默认为美式英文(en-us)。 |
例如:“client_id”为“101489619”的应用,在通过认证鉴权功能之后,想要访问华为运动健康App的数据,并且以中文的方式“zh-cn”进行展示此页面,则应用需要重定向用户的浏览器到如下URL:
https://oauth-login.cloud.huawei.com/oauth2/v3/authorize?response_type=code&client_id=106804381&redirect_uri=https%3A%2F%2Fh5hosting.dbankcdn.com%2Fcch5%2Fhealthkit%2Flink%2Fpages%2FprivacyManage.html&access_type=offline&state=zh-cn
此过程中需要,用户登录华为账号,跳转到连接页面,用户点击即可,如下图所示:

采用WebView的addJavascriptInterface方法实现客户端与前端的交互。
请使用"jsToAndroid"名称注入到网页框架中,以保持与前端定义的一致性。
示例代码如下:
class JsObject {
@JavascriptInterface
public String resultCodeProc(String result) {
// 开发者接收到返回码,实现逻辑处理(也可以在此处关闭webview)
Log("get the link result:" + result);
getActivity().finish();
}
}
webview.getSettings().setJavaScriptEnabled(true);
webView.addJavascriptInterface(new JsObject(), "jsToAndroid"); 在实现了WebKit的addScriptMessageHandler:name: 方法里,添加处理与用户点击“连接”按钮结果返回码相关的交互消息。
为了防止循环依赖,在-viewWillAppear:方法中执行addScriptMessageHandler操作,在-viewWillDisappear:方法中执行removeScriptMessageHandlerForName操作。
示例代码如下:
#import <WebKit/WebKit.h>
@interface WKWebViewWKScriptMessageHandlerController () <WKScriptMessageHandler>
//! 添加ScriptMessageHandler,并指明name为"jsToObj"
WKUserContentController *userContentController = [[WKUserContentController alloc] init];
[userContentController addScriptMessageHandler:self name:@"jsToObj"];
//! 配置configuration
WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
configuration.userContentController = userContentController;
//! 初始化webView
_webView = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:configuration];
#pragma mark - WKScriptMessageHandler
//! WKWebView收到ScriptMessage时回调此方法
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
if ([message.name caseInsensitiveCompare:@"jsToObj"] == NSOrderedSame) {
//! proce message.body
}
}