{"id":28962,"date":"2024-04-07T18:27:08","date_gmt":"2024-04-07T10:27:08","guid":{"rendered":"https:\/\/www.zhidianwl.com\/zhidian\/?p=28962"},"modified":"2024-04-07T18:27:08","modified_gmt":"2024-04-07T10:27:08","slug":"%e7%bd%91%e9%a1%b5%e4%b8%80%e9%94%ae%e7%94%9f%e6%88%90app%e6%ba%90%e7%a0%81%e4%bd%9c%e7%94%a8%e4%bb%8b%e7%bb%8d","status":"publish","type":"post","link":"https:\/\/www.zhidianwl.com\/zhidian\/2024\/28962\/","title":{"rendered":"\u7f51\u9875\u4e00\u952e\u751f\u6210app\u6e90\u7801\u4f5c\u7528\u4ecb\u7ecd"},"content":{"rendered":"

\u4e00\u952e\u5c06\u7f51\u9875\u751f\u6210\u4e3aAPP\u7684\u539f\u7406\u5927\u591a\u6570\u662f\u57fa\u4e8eWebView\u6280\u672f\u3002WebView\u662f\u4e00\u79cd\u5185\u7f6e\u4e8e\u539f\u751fAPP\u5185\u90e8\u7684\u6d4f\u89c8\u5668\u63a7\u4ef6\uff0c\u5b83\u53ef\u4ee5\u8ba9APP\u52a0\u8f7d\u5e76\u663e\u793a\u7f51\u9875\u5185\u5bb9\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06WebView\u5f00\u53d1\u6574\u5408\u5230\u79fb\u52a8APP\u91cc\uff0c\u5e2e\u52a9\u5f00\u53d1\u8005\u8f7b\u677e\u5730\u5c06\u7f51\u9875\u8f6c\u5316\u4e3a\u539f\u751fAPP\u3002<\/p>\n

\u4ee5\u4e0b\u662f\u539f\u7406\u6216\u8be6\u7ec6\u4ecb\u7ecd\uff1a<\/p>\n

1. WebView\u6280\u672f\u7b80\u4ecb<\/p>\n

WebView\u662f\u4e00\u79cd\u57fa\u4e8e\u6d4f\u89c8\u5668\u5185\u6838\u6e32\u67d3\u5f15\u64ce\u7684\u89c6\u56fe\uff0c\u5b83\u5141h5\u9875\u9762\u76f4\u63a5\u6253\u5305\u751f\u6210app<\/a>\u8bb8APP\u52a0\u8f7d\u663e\u793a\u7f51\u9875\u3002WebView\u7684\u529f\u80fd\u4e0e\u666e\u901a\u6d4f\u89c8\u5668\u7c7b\u4f3c\uff0c\u4f8b\u5982\u524d\u8fdb\u3001\u540e\u9000\u3001\u7f29\u653e\u7b49\u3002\u540c\u65f6\uff0cWebView\u652f\u6301\u4e0eJavaScript\u4e92\u52a8\uff0c\u4ee5\u4fbfAPP\u53ef\u4ee5\u6267\u884cJS\u811a\u672c\uff0c\u5b9e\u73b0\u539f\u751f\u529f\u80fd\u4e0e\u7f51\u9875\u5185\u5bb9\u7684\u4ea4\u4e92\u3002<\/p>\n

2. Android\u5e73\u53f0\u7684\u4e00\u952e\u751f\u6210APP<\/p>\n

\u5bf9\u4e8eAndroid\u7cfb\u7edf\uff0c\u53ef\u4ee5\u4f7f\u7528Android Studio\u5f00\u53d1\u5de5\u5177\uff0c\u521b\u5efa\u4e00\u4e2a\u65b0\u9879\u76ee\uff0c\u5e76\u5728\u9879\u76ee\u5e03\u5c40\u6587\u4ef6(layout)\u4e2d\u5f15\u5165WebView\u63a7\u4ef6\u3002\u5728MainActivity.java\u6587\u4ef6\u4e2d\uff0c\u52a0\u8f7d\u4f60\u60f3\u8981\u663e\u793a\u7684\u7f51\u9875\uff0c\u5e76\u4e3aWebView\u8bbe\u7f6e\u9002\u5f53\u7684\u5c5e\u6027\u548c\u6743\u9650\u3002<\/p>\n

\u53c2\u8003\u6b65\u9aa4:<\/p>\n

a. \u5728Android Studio\u4e2d\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u9879\u76ee\uff0c\u5e76\u9009\u62e9Empty Activity\u6a21\u677f\u3002<\/p>\n

b. \u5728\u9879\u76ee\u7684layout\u6587\u4ef6activity_main.xml\u4e2d\u6dfb\u52a0WebView\u63a7\u4ef6\uff1a<\/p>\n

“`xml<\/p>\n<\/p>\n

android:id=”@+id\/webv<\/p>\n

<\/figure>\n<\/p>\n

iew”<\/p>\n

android:layout_width=”match_parent”<\/p>\n

androiphp\u53ef\u4ee5\u751f\u6210app<\/a>d:layout_height=”match_parent” \/><\/p>\n

“`<\/p>\n

c. \u5728MainActivity.java\u6587\u4ef6\u91cc\u8bbe\u7f6eWebView\u63a7\u4ef6\uff1a\u52a0\u8f7d\u7f51\u9875\u3001\u5141\u8bb8JS\u811a\u672c\u3001\u4e3aWebView\u8bbe\u7f6eWebChromeClient\u7b49\u3002<\/p>\n

“`java<\/p>\n

import android.os.Bundle;<\/p>\n

import android.webkit.WebChromeClient;<\/p>\n

import android.webkit.WebView;<\/p>\n

import android.webkit.WebViewClient;<\/p>\n

public class MainActivity extends AppCompatActivity {<\/p>\n

private WebView webView;<\/p>\n

@Override<\/p>\n

protected void onCreate(Bundle savedInstanceState) {<\/p>\n

super.onCreate(savedInstanceState);<\/p>\n

setContentView(R.layout.activity_main);<\/p>\n

webView = findViewById(R.id.webview);<\/p>\n

webView.setWebChromeClient(new WebChromeClient());<\/p>\n

webView.getSettings().setJavaScriptEnabled(true);<\/p>\n

webView.setWebViewClient(new WebViewClient());<\/p>\n

webView.loadUrl(“https:\/\/www.example.com”);<\/p>\n

}<\/p>\n

}<\/p>\n

“`<\/p>\n

3. iOS\u5e73\u53f0\u7684\u4e00\u952e\u751f\u6210APP<\/p>\n

\u5bf9\u4e8eiOS\u7cfb\u7edf\uff0c\u53ef\u4ee5\u5728Xcode\u5de5\u5177\u4e2d\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u9879\u76ee\uff0c\u7136\u540e\u5728\u9879\u76ee\u7684ViewController.swift\u6587\u4ef6\u4e2d\u5f15\u5165WebKit\u63a7\u4ef6\u3002\u63a5\u4e0b\u6765\uff0c\u5c06WebKit\u63a7\u4ef6\u6dfb\u52a0\u5230\u89c6\u56fe\u5c42\u7ea7\uff0c\u5e76\u52a0\u8f7d\u4f60\u60f3\u8981\u663e\u793a\u7684\u7f51\u9875\u3002<\/p>\n

\u53c2\u8003\u6b65\u9aa4:<\/p>\n

a. \u5728Xcode\u4e2d\u521b\u5efa\u4e00\u4e2a\u65b0\u7684iOS\u9879\u76ee\uff0c\u5e76\u9009\u62e9Single View App\u6a21\u677f\u3002<\/p>\n

b. \u5728\u9879\u76ee\u7684ViewController.swift\u6587\u4ef6\u4e2d\uff0c\u5bfc\u5165WebKit\u5e93\uff0c\u5e76\u521b\u5efa\u4e00\u4e2aWKWebView\u5b9e\u4f8b\u3002<\/p>\n

“`swift<\/p>\n

import UIKit<\/p>\n

import WebKit<\/p>\n

class ViewController: UIViewController {<\/p>\n

var webView: WKWebView!<\/p>\n

override func viewDidLoad() {<\/p>\n

super.viewDidLoad()<\/p>\n

webView = WKWebView(frame: view.bounds)<\/p>\n

view.addSubview(webView)<\/p>\n

let url = URL(string: “https:\/\/www.example.com”)!<\/p>\n

let request = URLRequest(url: url)<\/p>\n

webView.load(request)<\/p>\n

}<\/p>\n

}<\/p>\n

“`<\/p>\n

\u901a\u8fc7\u8fd9\u4e9b\u7b80\u5355\u7684\u6b65\u9aa4\uff0c\u4f60\u53ef\u4ee5\u5728Android\u548ciOS\u5e73\u53f0\u4e0a\u8f7b\u677e\u5730\u5c06\u7f51\u9875\u4e00\u952e\u751f\u6210\u4e3aAPP\u3002\u5c3d\u7ba1WebView\u6280\u672f\u5b58\u5728\u6027\u80fd\u548c\u529f\u80fd\u7684\u9650\u5236\uff0c\u4f46\u5b83\u4ecd\u7136\u662f\u4e00\u4e2a\u6210\u672c\u4f4e\u5ec9\u3001\u5feb\u901f\u5f00\u53d1\u7684\u9009\u9879\uff0c\u7279\u522b\u662f\u5728\u5c06\u7f51\u9875\u5185\u5bb9\u96c6\u6210\u5230\u79fb\u52a8\u5e94\u7528\u4e2d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\u4e00\u952e\u5c06\u7f51\u9875\u751f\u6210\u4e3aAPP\u7684\u539f\u7406\u5927\u591a\u6570\u662f\u57fa\u4e8eWebView\u6280\u672f\u3002WebView\u662f\u4e00\u79cd\u5185\u7f6e\u4e8e\u539f\u751fAPP\u5185\u90e8\u7684\u6d4f\u89c8\u5668\u63a7\u4ef6\uff0c\u5b83\u53ef\u4ee5\u8ba9APP\u52a0\u8f7d\u5e76\u663e\u793a\u7f51\u9875\u5185\u5bb9\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06WebView\u5f00\u53d1\u6574\u5408\u5230\u79fb<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[29875,4633,303,117,322],"class_list":["post-28962","post","type-post","status-publish","format-standard","hentry","category-zuoapp","tag--app","tag-app","tag-303","tag-117","tag-322"],"_links":{"self":[{"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/posts\/28962","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/comments?post=28962"}],"version-history":[{"count":1,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/posts\/28962\/revisions"}],"predecessor-version":[{"id":29056,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/posts\/28962\/revisions\/29056"}],"wp:attachment":[{"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/media?parent=28962"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/categories?post=28962"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/tags?post=28962"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}