{"id":8840,"date":"2023-11-16T18:34:46","date_gmt":"2023-11-16T10:34:46","guid":{"rendered":"https:\/\/www.yimenyun.cn\/emen\/?p=8840"},"modified":"2023-11-16T18:34:46","modified_gmt":"2023-11-16T10:34:46","slug":"%e5%a6%82%e4%bd%95%e5%b0%86%e4%b8%80%e4%b8%aa%e7%bd%91%e9%a1%b5%e7%94%9f%e6%88%90app","status":"publish","type":"post","link":"https:\/\/www.yimenyun.cn\/emen\/8840\/","title":{"rendered":"\u5982\u4f55\u5c06\u4e00\u4e2a\u7f51\u9875\u751f\u6210app?"},"content":{"rendered":"

\u5c06\u4e00\u4e2a\u7f51\u9875\u751f\u6210APP\uff08\u5e94\u7528\u7a0b\u5e8f\uff09\u7684\u8fc7\u7a0b\u4e3b\u8981\u662f\u7531WebView\u6280\u672f\u5b9e\u7f51\u7ad9\u6e90\u7801\u751f\u6210app<\/a>\u73b0\u7684\u3002WebView\u662f\u4e00\u4e2a\u5185\u5d4c\u5728\u539f\u751fAPP\u4e2d\u7684\u6d4f\u89c8\u5668\u89c6\u7a97\uff0c\u652f\u6301\u6d4f\u89c8\u7f51\u9875\u3001\u6267\u884cJS\u811a\u672c\u7b49\u529f\u80fd\uff0c\u4f7f\u5f97\u5f00\u53d1\u8005\u53ef\u4ee5\u76f4\u63a5\u5c06\u7f51\u9875\u5185\u5bb9\u5d4c\u5165APP\u4e2d\uff0c\u65b9\u4fbf\u7528\u6237\u901a\u8fc7APP\u76f4\u63a5\u8bbf\u95ee\u7f51\u9875\u3002\u63a5\u4e0b\u6765\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5c06\u7f51\u9875\u751f\u6210APP\u7684\u539f\u7406\u53ca\u5b9e\u73b0\u65b9\u6cd5\u3002<\/p>\n

\u4e00\u3001\u6280\u672f\u539f\u7406<\/p>\n

\u5c06\u7f51\u9875\u751f\u6210APP\u7684\u539f\u7406\u4e3b\u8981\u53ef\u4ee5\u5206\u4e3a\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4\uff1a<\/p>\n

1. \u4f7f\u7528Android Studio\uff08\u5bf9\u4e8eAndroid\u5e94\u7528\uff09\u6216Xcode\uff08\u5bf9\u4e8eiOS\u5e94\u7528\uff09\u7b49\u5f00\u53d1\u5de5\u5177\uff0c\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u539f\u751fAPP\u9879\u76ee\u3002<\/p>\n

2. \u5728APP\u4e2d\u521b\u5efa\u4e00\u4e2aWebView\u7ec4\u4ef6\uff0c\u5e76\u8bbe\u7f6e\u5176\u5bb9\u5668\u5e03\u5c40\u4ee5\u4f7f\u5176\u5360\u636e\u6574\u4e2a\u5c4f\u5e55\u7a7a\u95f4\u3002<\/p>\n

3. \u5c06\u76ee\u6807\u7f51\u9875\u7684URL\u4f20\u9012\u7ed9WebView\u7ec4\u4ef6\uff0c\u4f7f\u5176\u52a0\u8f7d\u8be5\u7f51\u9875\u8d44\u6e90\u3002<\/p>\n

4. \u4e3aWebView\u7ec4\u4ef6\u6dfb\u52a0\u76d1\u542c\u5668\uff0c\u76d1\u542c\u7f51\u9875\u8d44\u6e90\u52a0\u8f7d\uff0c\u5904\u7406\u9519\u8bef\u7b49\u4e8b\u4ef6\u3002<\/p>\n

5. \u5bf9\u63a5\u6536\u5230\u7684\u7f51\u9875\u5411\u539f\u751fAPP\u53d1\u8d77\u7684\u8bf7\u6c42\uff0c\u5904\u7406APP\u7684\u7279\u6b8a\u529f\u80fd\uff0c\u4f8b\u5982\u542f\u52a8\u624b\u673a\u6444\u50cf\u5934\u3001\u8bbf\u95ee\u624b\u673a\u901a\u8baf\u5f55\u7b49\u3002<\/p>\n

6. \u5c06\u5b8c\u6210\u7684APP\u6253\u5305\u53d1\u5e03\u5230\u5e94\u7528\u5546\u5e97\uff0c\u4f9b\u7528\u6237\u4e0b\u8f7d\u4f7f\u7528\u3002<\/p>\n

\u4e8c\u3001\u5b9e\u73b0\u65b9\u6cd5\uff1a\u4ee5\u4e0b\u9488\u5bf9Android\u548ciOS\u4e24\u4e2a\u5e73\u53f0\u5206\u522b\u4ecb\u7ecd\u5b9e\u73b0\u65b9\u6cd5\uff1a<\/p>\n

1. Android\u5b9e\u73b0\u65b9\u6cd5\uff1a<\/p>\n

(1) \u9996\u5148\uff0c\u4f7f\u7528Android Studio\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u539f\u751fAPP\u9879\u76ee\u3002<\/p>\n

(2) \u5728\u9879\u76ee\u4e2d\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5e03\u5c40\u6587\u4ef6\uff0c\u4f8b\u5982activity_webview.xml\uff0c\u5c06WebView\u7ec4\u4ef6\u6dfb\u52a0\u5230\u5e03\u5c40\u4e2d\uff1a<\/p>\n

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

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

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

h5\u751f\u6210\u8f6f\u4ef6app<\/a> android:orientation=”vertical”><\/p>\n<\/p>\n

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

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

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

“`<\/p>\n

(3) \u5728\u65b0\u5efa\u6216\u5df2\u6709\u7684Activity\uff08\u5982MainActivity)\u4e2d\uff0c\u4fee\u6539\u5e03\u5c40\u6587\u4ef6\u5f15\u7528\uff0c\u52a0\u8f7d\u521b\u5efa\u7684activity_webview.xml\u5e03\u5c40\u3002\u63a5\u7740\uff0c\u521d\u59cb\u5316WebView\u7ec4\u4ef6\uff0c\u5e76\u4e3a\u5176\u52a0\u8f7d\u76ee\u6807\u7f51\u9875\u8d44\u6e90\uff1a<\/p>\n

“`java<\/p>\n

import android.support.v7.app.AppCompatActivity;<\/p>\n

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

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

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

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

@Override<\/p>\n

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

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

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

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

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

WebSettings webSettings = webView.getSettings();<\/p>\n

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

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

}<\/p>\n

}<\/p>\n

“`<\/p>\n

(4) \u6d4b\u8bd5APP\uff0c\u5982\u679c\u4e00\u5207\u6b63\u5e38\uff0c\u6700\u540e\u751f\u6210\u5b89\u88c5\u5305\u5e76\u53d1\u5e03\u5230\u5e94\u7528\u5546\u5e97\u3002<\/p>\n

2. iOS\u5b9e\u73b0\u65b9\u6cd5\uff1a<\/p>\n

(1) \u4f7f\u7528Xcode\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u539f\u751fAPP\u9879\u76ee\u3002<\/p>\n

(2) \u5728\u9879\u76ee\u4e2d\uff0c\u6253\u5f00Main.storyboard\uff0c\u4eceObject Library\u4e2d\u62d6\u62fd\u4e00\u4e2aWKWebView\u7ec4\u4ef6\u5230View Controller\u4e0a\uff0c\u5e76\u8bbe\u7f6eAutolayout\u7ea6\u675f\uff0c\u4f7f\u5176\u5360\u636e\u6574\u4e2a\u5c4f\u5e55\u3002<\/p>\n

(3) \u4f7f\u7528Assistant\u7f16\u8f91\u5668\uff0c\u6dfb\u52a0IBOutlet\u5c5e\u6027\uff0c\u4e3aWKWebView\u521b\u5efa\u5f15\u7528\uff1a<\/p>\n

“`swift<\/p>\n

import UIKit<\/p>\n

import WebKit<\/p>\n

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

@IBOutlet weak var webView: WKWebView!<\/p>\n

}<\/p>\n

“`<\/p>\n

(4) \u4fee\u6539ViewController.swift\u6587\u4ef6\uff0c\u4fee\u6539\u5176\u7ee7\u627f\u7c7b\u4e3aWKNavigationDelegate\uff0c\u5e76\u5728viewDidLoad()\u65b9\u6cd5\u4e2d\u8bbe\u7f6e\u5e76\u52a0\u8f7dURL\uff1a<\/p>\n

“`swift<\/p>\n

import UIKit<\/p>\n

import WebKit<\/p>\n

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

@IBOutlet weak var webView: WKWebView!<\/p>\n

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

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

webView.navigationDelegate = self<\/p>\n

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

webView.load(URLRequest(url: url))<\/p>\n

}<\/p>\n

}<\/p>\n

“`<\/p>\n

(5) \u6d4b\u8bd5APP\uff0c\u5982\u679c\u4e00\u5207\u6b63\u5e38\uff0c\u6700\u540e\u751f\u6210\u5b89\u88c5\u5305\u5e76\u53d1\u5e03\u5230\u5e94\u7528\u5546\u5e97\u3002<\/p>\n

\u5c06\u7f51\u9875\u751f\u6210APP\u7684\u8fc7\u7a0b\u5c31\u5b8c\u6210\u4e86\uff0c\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u7531\u4e8eWebView\u5185\u7f6e\u7684\u6d4f\u89c8\u5668\u7248\u672c\u4e0d\u540c\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u9488\u5bf9\u4e00\u4e9b\u53ef\u80fd\u51fa\u73b0\u7684\u517c\u5bb9\u6027\u95ee\u9898\u8fdb\u884c\u8c03\u6574\u3002\u5e0c\u671b\u4ee5\u4e0a\u4fe1\u606f\u80fd\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"

\u5c06\u4e00\u4e2a\u7f51\u9875\u751f\u6210APP\uff08\u5e94\u7528\u7a0b\u5e8f\uff09\u7684\u8fc7\u7a0b\u4e3b\u8981\u662f\u7531WebView\u6280\u672f\u5b9e\u73b0\u7684\u3002WebView\u662f\u4e00\u4e2a\u5185\u5d4c\u5728\u539f\u751fAPP\u4e2d\u7684\u6d4f\u89c8\u5668\u89c6\u7a97\uff0c\u652f\u6301\u6d4f\u89c8\u7f51\u9875\u3001\u6267\u884cJS\u811a\u672c\u7b49\u529f\u80fd\uff0c\u4f7f\u5f97\u5f00\u53d1\u8005\u53ef\u4ee5\u76f4\u63a5\u5c06\u7f51\u9875\u5185\u5bb9\u5d4c\u5165APP\u4e2d<\/p>\n","protected":false},"author":6,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[2325,10448,736,149,6140],"topic":[],"class_list":["post-8840","post","type-post","status-publish","format-standard","hentry","category-appkf","tag-2325","tag-app","tag-736","tag-149"],"_links":{"self":[{"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/posts\/8840","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/comments?post=8840"}],"version-history":[{"count":0,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/posts\/8840\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/media?parent=8840"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/categories?post=8840"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/tags?post=8840"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/topic?post=8840"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}