{"id":29362,"date":"2024-07-10T11:56:00","date_gmt":"2024-07-10T03:56:00","guid":{"rendered":"https:\/\/www.zhidianwl.net\/zhidianwl\/2024\/07\/10\/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\/"},"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:\/\/www.zhidianwl.net\/zhidianwl\/2024\/07\/10\/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\/","title":{"rendered":"ios\u6253\u5305\u6d4f\u89c8\u5668\u7248\u600e\u4e48\u505a?"},"content":{"rendered":"

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

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

iOS\u6253\u5305\u6d4f\u89c8\u5668\u7248\u7684\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n

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

2. \u5728\u9879\u76ee\u4e2d\u6dfb\u52a0\u4e00\u4e2aWKWebView\u6216UIWebView\u7ec4\u4ef6\uff0c\u7528\u4e8e\u52a0\u8f7d\u7f51\u9875\u3002<\/p>\n

3. \u5728AppDelegate.swift\u6216AppDelegate.m\u6587\u4ef6\u4e2d\u5b9e\u73b0\u4ee5\u4e0b\u4ee3\u7406\u65b9\u6cd5\uff1a<\/p>\n

“`swift<\/p>\n

func application(_ application: UIApplicatiOS\u5c01\u88c5<\/a>ion, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {<\/p>\n

\/\/ Override point for customization after application launch.<\/p>\n

\/\/ \u521b\u5efa\u4e00\u4e2aWKWebView\u6216UIWebView\u5bf9\u8c61<\/p>\n

let webView = WKWebView(frame: UIScreen.main.bounds)<\/p>\n

\/\/ \u52a0\u8f7d\u7f51\u9875<\/p>\n

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

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

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

\/\/ \u5c06WKWebView\u6216UIWebView\u5bf9\u8c61\u6dfb\u52a0\u5230\u7a97\u53e3\u4e2d<\/p>\n

self.window?.rootViewController = UIViewandroid widget\u5f00\u53d1<\/a>Controller()<\/p>\n

self.window?.rootViewController?.view.addSubview(webView)<\/p>\n

self.window?.makeKeyAndVisible()<\/p>\n

return true<\/p>\n

}<\/p>\n

“`<\/p>\n

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

“`javascript<\/p>\n

\/\/ \u5728\u7f51\u9875\u4e2d\u8c03\u7528\u539f\u751f\u5e94\u7528\u63d0\u4f9b\u7684API<\/p>\n

window.webkit.messageHandlers.showAlert.postMessage(“Hello, world!”);<\/p>\n

\/\/ \u5728\u539f\u751f\u5e94\u7528\u4e2d\u5b9e\u73b0API<\/p>\n

@objc func showAlert(_ message: String) {<\/p>\n

let alertController = UIAlertController(title: “Alert”, message: message, preferredStyle: .alert)<\/p>\n

let okAction = UIAlertAction(title: “OK”, style: .default, handler: nil)<\/p>\n

alertController.addAction(okAction)<\/p>\n

self.present(alertController, animated: true, compl<\/p>\n

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

etion: nil)<\/p>\n

}<\/p>\n

\/\/ \u6ce8\u518cAPI<\/p>\n

let script = WKUserScript(source: “window.webkit.messageHandlers.showAlert = function(message) { window.webkit.messageHandlers.nativeAPI.postMessage({name: ‘showAlert’, message: message}); }”, injectionTime: .atDocumentEnd, forMainFrameOnly: true)<\/p>\n

webView.configuration.userContentController.addUserScript(script)<\/p>\n

webView.configuration.userContentController.add(self, name: “nativeAPI”)<\/p>\n

“`<\/p>\n

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

\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":"

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":24,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[26831,674,594,19201,414],"topic":[],"class_list":["post-29362","post","type-post","status-publish","format-standard","hentry","category-kaifaapp","tag-android-popwindow","tag-674","tag-594","tag-apk","tag-414"],"_links":{"self":[{"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/posts\/29362","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/users\/24"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/comments?post=29362"}],"version-history":[{"count":0,"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/posts\/29362\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/media?parent=29362"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/categories?post=29362"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/tags?post=29362"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/topic?post=29362"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}