{"id":33709,"date":"2024-07-10T11:56:00","date_gmt":"2024-07-10T03:56:00","guid":{"rendered":"https:\/\/h5app.yimenapp.cn\/h5app\/2024\/appkfa\/33709\/"},"modified":"2024-07-10T11:56:00","modified_gmt":"2024-07-10T03:56:00","slug":"ios%e6%89%93%e5%8c%85%e6%b5%8f%e8%a7%88%e5%99%a8%e7%89%88%e6%80%8e%e4%b9%88%e5%81%9a","status":"publish","type":"post","link":"https:\/\/h5app.yimenapp.cn\/h5app\/2024\/appkfa\/33709\/","title":{"rendered":"ios\u6253\u5305\u6d4f\u89c8\u5668\u7248\u600e\u4e48\u505a?"},"content":{"rendered":"<p>iOS\u6253\u5305\u6d4f\u89c8\u5668\u7248\u662f\u6307\u5c06\u7f51\u9875\u5e94\u7528\u6253\u5305\u6210\u4e00\u4e2aiOS\u5e94\u7528\uff0c\u7528\u6237\u53ef\u4ee5\u5728iOS\u8bbe\u5907\u4e0a\u4f7f\u7528\u3002\u8fd9\u79cd\u6253\u5305\u65b9\u5f0f\u53c8\u79f0\u4e3aHybrid\u5e94\u7528\uff0c\u662f\u5c06Web\u6280\u672f\u548c\u539f\u751f\u5e94\u7528\u6280\u672f\u7ed3\u5408\u7684\u4e00\u79cd\u5e94\u7528\u65b9\u5f0f\uff0c\u53ef\u4ee5\u517c\u987eWeb\u5e94\u7528\u7684\u7075\u6d3b\u6027\u548c\u539f\u751f\u5e94\u7528\u7684\u6027\u80fd\u3002<\/p>\n<p>iOS\u6253\u5305\u6d4f\u89c8\u5668\u7248\u7684\u539f\u7406\u662f\u5229\u7528iOS\u7684UIWebView\u6216WKWebView\u7ec4\u4ef6\u6765\u52a0\u8f7d\u7f51\u9875\uff0c\u5e76\u901a\u8fc7JavaScript\u4e0e\u539f\u751f\u5e94\u7528\u8fdb\u884c\u4ea4\u4e92\uff0c\u5b9e\u73b0\u4e00\u4e9b\u539f\u751f\u5e94\u7528\u5177\u5907\u7684\u529f\u80fd\u3002\u5728iOS 8\u4e4b\u540e\uff0cApple\u5f15\u5165\u4e86WKWebView\u7ec4\u4ef6\uff0c\u76f8\u6bd4UIWebView\uff0cWKWebView\u5177\u6709\u66f4\u5feb\u7684\u52a0\u8f7d\u901f\u5ea6\u3001\u66f4\u597d\u7684\u6027\u80fd\u548c\u66f4\u4e30\u5bcc\u7684\u7279\u6027\uff0c\u56e0\u6b64\u5efa\u8bae\u4f7f\u7528WKWebView\u7ec4\u4ef6\u6765\u52a0\u8f7d\u7f51\u9875\u3002<\/p>\n<p>iOS\u6253\u5305\u6d4f\u89c8\u5668\u7248\u7684\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<p>1. \u521b\u5efa\u4e00\u4e2a\u65b0\u7684Xcode\u9879\u76ee\uff0c\u9009\u62e9Single View App\u6a21\u677f\uff0c\u8bed\u8a00\u9009\u62e9Swift\u6216Objective-C\u3002<\/p>\n<p>2. \u5728\u9879\u76ee\u4e2d\u6dfb\u52a0\u4e00\u4e2aWKWebView\u6216UIWebView\u7ec4\u4ef6\uff0c\u7528\u4e8e\u52a0\u8f7d\u7f51\u9875\u3002<\/p>\n<p>3. \u5728AppDelegate.swift\u6216AppDelegate.m\u6587\u4ef6\u4e2d\u5b9e\u73b0\u4ee5\u4e0b\u4ee3\u7406\u65b9\u6cd5\uff1a<\/p>\n<p>&#8220;`swift<\/p>\n<p>func application(_ application: UIApplicat<a href=\"https:\/\/www.yimenapp.net\/dabao-461.html\">iOS\u5c01\u88c5<\/a>ion, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -&gt; Bool {<\/p>\n<p>    \/\/ Override point for customization after application launch.<\/p>\n<p>    \/\/ \u521b\u5efa\u4e00\u4e2aWKWebView\u6216UIWebView\u5bf9\u8c61<\/p>\n<p>    let webView = WKWebView(frame: UIScreen.main.bounds)<\/p>\n<p>    \/\/ \u52a0\u8f7d\u7f51\u9875<\/p>\n<p>    let url = URL(string: &#8220;http:\/\/www.example.com&#8221;)<\/p>\n<p>    let request = URLRequest(url: url!)<\/p>\n<p>    webView.load(request)<\/p>\n<p>    \/\/ \u5c06WKWebView\u6216UIWebView\u5bf9\u8c61\u6dfb\u52a0\u5230\u7a97\u53e3\u4e2d<\/p>\n<p>    self.window?.rootViewController = UIView<a href=\"https:\/\/www.yimenapp.net\/dabao-4429.html\">android widget\u5f00\u53d1<\/a>Controller()<\/p>\n<p>    self.window?.rootViewController?.view.addSubview(webView)<\/p>\n<p>    self.window?.makeKeyAndVisible()<\/p>\n<p>    return true<\/p>\n<p>}<\/p>\n<p>&#8220;`<\/p>\n<p>4. \u5728\u7f51\u9875\u4e2d\u901a\u8fc7JavaScript\u8c03\u7528\u539f\u751f\u5e94\u7528\u63d0\u4f9b\u7684API\uff0c\u53ef\u4ee5\u4f7f\u7528JavaScriptCore\u6846\u67b6\u6765\u5b9e\u73b0\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\n<p>&#8220;`javascript<\/p>\n<p>\/\/ \u5728\u7f51\u9875\u4e2d\u8c03\u7528\u539f\u751f\u5e94\u7528\u63d0\u4f9b\u7684API<\/p>\n<p>window.webkit.messageHandlers.showAlert.postMessage(&#8220;Hello, world!&#8221;);<\/p>\n<p>\/\/ \u5728\u539f\u751f\u5e94\u7528\u4e2d\u5b9e\u73b0API<\/p>\n<p>@objc func showAlert(_ message: String) {<\/p>\n<p>    let alertController = UIAlertController(title: &#8220;Alert&#8221;, message: message, preferredStyle: .alert)<\/p>\n<p>    let okAction = UIAlertAction(title: &#8220;OK&#8221;, style: .default, handler: nil)<\/p>\n<p>    alertController.addAction(okAction)<\/p>\n<p>    self.present(alertController, animated: true, compl<\/p>\n<p><figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/i.cdn.yimenapp.com\/2023-03\/31\/15\/12766754067.jpg\" \/><\/figure>\n<\/p>\n<p>etion: nil)<\/p>\n<p>}<\/p>\n<p>\/\/ \u6ce8\u518cAPI<\/p>\n<p>let script = WKUserScript(source: &#8220;window.webkit.messageHandlers.showAlert = function(message) { window.webkit.messageHandlers.nativeAPI.postMessage({name: &#8216;showAlert&#8217;, message: message}); }&#8221;, injectionTime: .atDocumentEnd, forMainFrameOnly: true)<\/p>\n<p>webView.configuration.userContentController.addUserScript(script)<\/p>\n<p>webView.configuration.userContentController.add(self, name: &#8220;nativeAPI&#8221;)<\/p>\n<p>&#8220;`<\/p>\n<p>5. \u6253\u5305\u5e94\u7528\uff0c\u53ef\u4ee5\u9009\u62e9\u4f7f\u7528Xcode\u81ea\u5e26\u7684\u6253\u5305\u5de5\u5177\u6216\u7b2c\u4e09\u65b9\u6253\u5305\u5de5\u5177\uff0c\u5982PhoneGap\u3001Cordova\u7b49\u3002<\/p>\n<p>\u603b\u7684\u6765\u8bf4\uff0ciOS\u6253\u5305\u6d4f\u89c8\u5668\u7248\u662f\u4e00\u79cd\u975e\u5e38\u65b9\u4fbf\u7684\u5e94\u7528\u5f00\u53d1\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5c06\u7f51\u9875\u5e94\u7528\u5feb\u901f\u6253\u5305\u6210iOS\u5e94\u7528\uff0c\u5e76\u901a\u8fc7JavaScript\u4e0e\u539f\u751f\u5e94\u7528\u8fdb\u884c\u4ea4\u4e92\uff0c\u5b9e\u73b0\u66f4\u591a\u7684\u529f\u80fd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>iOS\u6253\u5305\u6d4f\u89c8\u5668\u7248\u662f\u6307\u5c06\u7f51\u9875\u5e94\u7528\u6253\u5305\u6210\u4e00\u4e2aiOS\u5e94\u7528\uff0c\u7528\u6237\u53ef\u4ee5\u5728iOS\u8bbe\u5907\u4e0a\u4f7f\u7528\u3002\u8fd9\u79cd\u6253\u5305\u65b9\u5f0f\u53c8\u79f0\u4e3aHybrid\u5e94\u7528\uff0c\u662f\u5c06Web\u6280\u672f\u548c\u539f\u751f\u5e94\u7528\u6280\u672f\u7ed3\u5408\u7684\u4e00\u79cd\u5e94\u7528\u65b9\u5f0f\uff0c\u53ef\u4ee5\u517c\u987eWeb\u5e94\u7528\u7684\u7075\u6d3b\u6027\u548c\u539f\u751f<\/p>\n","protected":false},"author":17,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[30780,3735,442,22580,206],"topic":[],"class_list":["post-33709","post","type-post","status-publish","format-standard","hentry","category-appkfa","tag-android-popwindow","tag-3735","tag-442","tag-apk","tag-206"],"_links":{"self":[{"href":"https:\/\/h5app.yimenapp.cn\/h5app\/wp-json\/wp\/v2\/posts\/33709","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/h5app.yimenapp.cn\/h5app\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/h5app.yimenapp.cn\/h5app\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/h5app.yimenapp.cn\/h5app\/wp-json\/wp\/v2\/users\/17"}],"replies":[{"embeddable":true,"href":"https:\/\/h5app.yimenapp.cn\/h5app\/wp-json\/wp\/v2\/comments?post=33709"}],"version-history":[{"count":0,"href":"https:\/\/h5app.yimenapp.cn\/h5app\/wp-json\/wp\/v2\/posts\/33709\/revisions"}],"wp:attachment":[{"href":"https:\/\/h5app.yimenapp.cn\/h5app\/wp-json\/wp\/v2\/media?parent=33709"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/h5app.yimenapp.cn\/h5app\/wp-json\/wp\/v2\/categories?post=33709"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/h5app.yimenapp.cn\/h5app\/wp-json\/wp\/v2\/tags?post=33709"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/h5app.yimenapp.cn\/h5app\/wp-json\/wp\/v2\/topic?post=33709"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}