如何在 APP 中使用 WebView 接入有客

如需在 APP 中使用有客,可以使用 Webview 进行接入,详情请参考此文档。


# 第一步:创建用于在 WebView 中打开的客服对话页面

创建一个 HTML 文件,内容如下:

<html>
<title>在线客服</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<script type="text/javascript">
(function(y,o,u,k,e){if(y[k]){return;}y['YoukeInstance']=k;y[k]=y[k]||function(){(y[k].e=y[k].e||[]).push(arguments)};e=o.createElement('script');e.async=1;e.src=u;o.currentScript?o.currentScript.parentNode.insertBefore(e,o.currentScript):o.body.appendChild(e);})(window,document,'https://static.cdn.youke.co/js/widget.js','$youke');


// 下方的 #{projectKey} 请务必替换为您的项目的 projectKey
// 可以在网页插件代码中,$youke('load', 旁边找到

$youke('load', '#{projectKey}', {
  config: {
    pluginConfig: {
      fullScreenMode: true,
      hideCloseButton: true,
      autoOpenChatWindow: true,
    }
  }
});
</script>
</html>

# 第二步: 在 WebView 中加载它

1、Andorid 示例代码:

webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setDomStorageEnabled(true);

webview.setWebChromeClient(new WebChromeClient() {
    @Override
    public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams) {
        uploadMessageAboveL = filePathCallback;
        openImageChooserActivity();
        return true;
    }
});

String url = "file:///android_asset/youke.html";

/* WebView 内嵌 Client 可以在APP内打开网页而不是跳出到浏览器 */
WebViewClient webViewClient = new WebViewClient() {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        super.shouldOverrideUrlLoading(view, url);
        view.loadUrl(url);
        return true;
    }
};
mWebView.setWebViewClient(webViewClient);

webView.loadUrl(url);

Android 演示项目:https://github.com/xinxiaotech/youke-webview-android-demo


2、iOS 示例代码:

#import "ViewController.h"

// 如果使用 WKWebview 的话,需要导入 Webkit 的头文件
#import "WebKit/WebKit.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    WKWebView *webview = [[WKWebView alloc] initWithFrame:CGRectMake(0, 36, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height)];

    NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"youke" ofType:@"html"];
    NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
    [webview loadHTMLString:htmlString baseURL: [[NSBundle mainBundle] bundleURL]];

    // 将WebView对象添加到当前页面当中
    [self.view addSubview:webview];

    // WebView对象加载请求并且现实内容
    [webview loadRequest:request];
}

@end

最后更新于 2 years ago

这篇文章是否解决了你的问题?

😀😟