{"id":190960,"date":"2023-09-01T07:26:21","date_gmt":"2023-08-31T23:26:21","guid":{"rendered":"https:\/\/www.idc.net\/help\/190960\/"},"modified":"2023-09-01T07:26:21","modified_gmt":"2023-08-31T23:26:21","slug":"%e5%90%8e%e6%b5%aa%e4%ba%91react%e6%95%99%e7%a8%8b%ef%bc%9aios-%e9%93%be%e6%8e%a5","status":"publish","type":"post","link":"https:\/\/idc.net\/help\/190960\/","title":{"rendered":"\u540e\u6d6a\u4e91React\u6559\u7a0b\uff1aiOS \u94fe\u63a5"},"content":{"rendered":"<p style=\"margin: 0px 0px 10px;line-height: 26px;font-size: 14px;font-style: normal;font-variant: normal;font-weight: normal;letter-spacing: normal;text-align: start;text-indent: 0px;text-transform: none\"><code>LinkingIOS<\/code><span>&nbsp;<\/span>\u7ed9\u4f60\u63d0\u4f9b\u4e86\u4e00\u4e2a\u901a\u7528\u63a5\u53e3\uff0c\u7528\u6765\u8fde\u63a5\u63a5\u6536\u548c\u53d1\u9001\u5e94\u7528\u7a0b\u5e8f\u7684\u94fe\u63a5\u3002<\/p>\n<h2>\u57fa\u672c\u7528\u6cd5<\/h2>\n<h3>\u5904\u7406\u6df1\u5ea6\u94fe\u63a5<\/h3>\n<p style=\"margin: 0px 0px 10px;line-height: 26px;font-size: 14px;font-style: normal;font-variant: normal;font-weight: normal;letter-spacing: normal;text-align: start;text-indent: 0px;text-transform: none\">\u5982\u679c\u4f60\u7684\u5e94\u7528\u7a0b\u5e8f\u662f\u4ece\u4e00\u4e2a\u5916\u90e8\u94fe\u63a5\u542f\u52a8\u7684\uff0c\u5e76\u4e14\u8fd9\u4e2a\u5916\u90e8\u94fe\u63a5\u662f\u6ce8\u518c\u5230\u4f60\u7684\u5e94\u7528\u7a0b\u5e8f\u91cc\u7684\uff0c\u90a3\u4e48\u4f60\u5c31\u53ef\u4ee5\u5229\u7528\u4efb\u610f\u4f60\u60f3\u8981\u7684\u7ec4\u4ef6\u53bb\u8bbf\u95ee\u5e76\u4e14\u5904\u7406\u5b83<\/p>\n<pre>&nbsp;&nbsp;&nbsp;&nbsp;componentDidMount()&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;url&nbsp;=&nbsp;LinkingIOS.popInitialURL();\n&nbsp;&nbsp;&nbsp;&nbsp;}<\/pre>\n<p style=\"margin: 0px 0px 10px;line-height: 26px;font-size: 14px;font-style: normal;font-variant: normal;font-weight: normal;letter-spacing: normal;text-align: start;text-indent: 0px;text-transform: none\">\u5728\u4f60\u7684\u5e94\u7528\u7a0b\u5e8f\u8fd0\u884c\u671f\u95f4\uff0c\u5982\u679c\u4f60\u4e5f\u60f3\u76d1\u542c\u4f20\u5165\u5e94\u7528\u7a0b\u5e8f\u7684\u94fe\u63a5\uff0c\u90a3\u4e48\u4f60\u9700\u8981\u5c06\u4ee5\u4e0b\u51e0\u884c\u6dfb\u52a0\u5230\u4f60\u7684&nbsp;<code>*AppDelegate.m<\/code>\uff1a<\/p>\n<pre>&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;(BOOL)application:(UIApplication&nbsp;*)application&nbsp;openURL:(NSURL&nbsp;*)url&nbsp;sourceApplication:(NSString&nbsp;*)sourceApplication&nbsp;annotation:(id)annotation&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;[RCTLinkingManager&nbsp;application:application&nbsp;openURL:url&nbsp;sourceApplication:sourceApplication&nbsp;annotation:annotation];\n&nbsp;&nbsp;&nbsp;&nbsp;}<\/pre>\n<p style=\"margin: 0px 0px 10px;line-height: 26px;font-size: 14px;font-style: normal;font-variant: normal;font-weight: normal;letter-spacing: normal;text-align: start;text-indent: 0px;text-transform: none\">\u90a3\u4e48\uff0c\u5728\u4f60\u7684 React \u7ec4\u4ef6\u4e2d\uff0c\u4f60\u53ef\u4ee5\u76d1\u542c&nbsp;<code>LinkingIOS<\/code>&nbsp;\u4e0a\u7684\u4e8b\u4ef6\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>&nbsp;&nbsp;&nbsp;&nbsp;componentDidMount()&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;LinkingIOS.addEventListener('url',&nbsp;this._handleOpenURL);\n&nbsp;&nbsp;&nbsp;&nbsp;},\n&nbsp;&nbsp;&nbsp;&nbsp;componentWillUnmount()&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;LinkingIOS.removeEventListener('url',&nbsp;this._handleOpenURL);\n&nbsp;&nbsp;&nbsp;&nbsp;},\n&nbsp;&nbsp;&nbsp;&nbsp;_handleOpenURL(event)&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;console.log(event.url);\n&nbsp;&nbsp;&nbsp;&nbsp;}<\/pre>\n<h3>\u89e6\u53d1\u5e94\u7528\u7a0b\u5e8f\u94fe\u63a5<\/h3>\n<p style=\"margin: 0px 0px 10px;line-height: 26px;font-size: 14px;font-style: normal;font-variant: normal;font-weight: normal;letter-spacing: normal;text-align: start;text-indent: 0px;text-transform: none\">\u4e3a\u4e86\u89e6\u53d1\u4e00\u4e2a\u5e94\u7528\u7a0b\u5e8f\u7684\u94fe\u63a5\uff08\u6d4f\u89c8\u5668\uff0c\u7535\u5b50\u90ae\u4ef6\uff0c\u6216\u8005\u81ea\u5b9a\u4e49\u6a21\u5f0f\uff09\uff0c\u4f60\u9700\u8981\u8c03\u7528<\/p>\n<pre>LinkingIOS.openURL(url)<\/pre>\n<p style=\"margin: 0px 0px 10px;line-height: 26px;font-size: 14px;font-style: normal;font-variant: normal;font-weight: normal;letter-spacing: normal;text-align: start;text-indent: 0px;text-transform: none\">\u5982\u679c\u4f60\u60f3\u8981\u68c0\u67e5\u4e00\u4e2a\u5df2\u7ecf\u5b89\u88c5\u7684\u5e94\u7528\u7a0b\u5e8f\u662f\u5426\u53ef\u4ee5\u63d0\u524d\u5904\u7406\u4e00\u4e2a\u7ed9\u5b9a\u7684\u94fe\u63a5\uff0c\u4f60\u53ef\u4ee5\u8c03\u7528<\/p>\n<pre>&nbsp;&nbsp;&nbsp;&nbsp;LinkingIOS.canOpenURL(url,&nbsp;(supported)&nbsp;=&gt;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(!supported)&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AlertIOS.alert('Can\\'t&nbsp;handle&nbsp;url:&nbsp;'&nbsp;+&nbsp;url);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;LinkingIOS.openURL(url);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;});<\/pre>\n<h2>\u65b9\u6cd5<\/h2>\n<p style=\"margin: 0px 0px 10px;line-height: 26px;font-size: 14px;font-style: normal;font-variant: normal;font-weight: normal;letter-spacing: normal;text-align: start;text-indent: 0px;text-transform: none\">static<span>&nbsp;<\/span><strong>addEventListener<\/strong>(type: string, handler: Function)<\/p>\n<p style=\"margin: 0px 0px 10px;line-height: 26px;font-size: 14px;font-style: normal;font-variant: normal;font-weight: normal;letter-spacing: normal;text-align: start;text-indent: 0px;text-transform: none\">\u901a\u8fc7\u76d1\u542c<span>&nbsp;<\/span><code>url<\/code><span>&nbsp;<\/span>\u4e8b\u4ef6\u7c7b\u578b\u548c\u63d0\u4f9b\u5904\u7406\u7a0b\u5e8f\uff0c\u5c06\u4e00\u4e2a\u5904\u7406\u7a0b\u5e8f\u6dfb\u52a0\u5230 LinkingIOS changes<\/p>\n<p style=\"margin: 0px 0px 10px;line-height: 26px;font-size: 14px;font-style: normal;font-variant: normal;font-weight: normal;letter-spacing: normal;text-align: start;text-indent: 0px;text-transform: none\">static<span>&nbsp;<\/span><strong>removeEventListener<\/strong>(type: string, handler: Function)<\/p>\n<p style=\"margin: 0px 0px 10px;line-height: 26px;font-size: 14px;font-style: normal;font-variant: normal;font-weight: normal;letter-spacing: normal;text-align: start;text-indent: 0px;text-transform: none\">\u901a\u8fc7\u4f20\u9012<span>&nbsp;<\/span><code>url<\/code><span>&nbsp;<\/span>\u4e8b\u4ef6\u7c7b\u578b\u548c\u5904\u7406\u7a0b\u5e8f\uff0c\u5220\u9664\u4e00\u4e2a\u5904\u7406\u7a0b\u5e8f<\/p>\n<p style=\"margin: 0px 0px 10px;line-height: 26px;font-size: 14px;font-style: normal;font-variant: normal;font-weight: normal;letter-spacing: normal;text-align: start;text-indent: 0px;text-transform: none\">static<span>&nbsp;<\/span><strong>openURL<\/strong>(url: string)<\/p>\n<p style=\"margin: 0px 0px 10px;line-height: 26px;font-size: 14px;font-style: normal;font-variant: normal;font-weight: normal;letter-spacing: normal;text-align: start;text-indent: 0px;text-transform: none\">\u5c1d\u8bd5\u901a\u8fc7\u4efb\u610f\u5df2\u7ecf\u5b89\u88c5\u7684\u5e94\u7528\u7a0b\u5e8f\u6253\u5f00\u7ed9\u5b9a\u7684<span>&nbsp;<\/span><code>url<\/code><\/p>\n<p style=\"margin: 0px 0px 10px;line-height: 26px;font-size: 14px;font-style: normal;font-variant: normal;font-weight: normal;letter-spacing: normal;text-align: start;text-indent: 0px;text-transform: none\">static<span>&nbsp;<\/span><strong>canOpenURL<\/strong>(url: string, callback: Function)<\/p>\n<p style=\"margin: 0px 0px 10px;line-height: 26px;font-size: 14px;font-style: normal;font-variant: normal;font-weight: normal;letter-spacing: normal;text-align: start;text-indent: 0px;text-transform: none\">\u51b3\u5b9a\u4e00\u4e2a\u5df2\u7ecf\u5b89\u88c5\u7684\u5e94\u7528\u7a0b\u5e8f\u662f\u5426\u53ef\u4ee5\u5904\u7406\u4e00\u4e2a\u7ed9\u5b9a\u7684<span>&nbsp;<\/span><code>url<\/code>\uff0c\u8be5\u65b9\u6cd5\u4e2d\u56de\u8c03\u51fd\u6570\u5c06\u88ab\u8c03\u7528\uff0c\u5e76\u4e14\u4ec5\u901a\u8fc7\u4e00\u4e2a<span>&nbsp;<\/span><code>bool supported<\/code><span>&nbsp;<\/span>\u7684\u53c2\u6570\u3002<\/p>\n<p style=\"margin: 0px 0px 10px;line-height: 26px;font-size: 14px;font-style: normal;font-variant: normal;font-weight: normal;letter-spacing: normal;text-align: start;text-indent: 0px;text-transform: none\">static<span>&nbsp;<\/span><strong>popInitialURL<\/strong>()<\/p>\n<p style=\"margin: 0px 0px 10px;line-height: 26px;font-size: 14px;font-style: normal;font-variant: normal;font-weight: normal;letter-spacing: normal;text-align: start;text-indent: 0px;text-transform: none\">\u5982\u679c\u5e94\u7528\u7a0b\u5e8f\u542f\u52a8\u662f\u901a\u8fc7\u4e00\u4e2a\u5e94\u7528\u7a0b\u5e8f\u94fe\u63a5\u89e6\u53d1\u7684\uff0c\u90a3\u4e48\u5b83\u5c06\u5f39\u51fa\u8fd9\u4e2a\u94fe\u63a5\u7684 url\uff0c\u5426\u5219\u5b83\u5c06\u8fd4\u56de<span>&nbsp;<\/span><code>null<\/code>\u3002<\/p>\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>LinkingIOS&nbsp;\u7ed9\u4f60\u63d0\u4f9b\u4e86\u4e00\u4e2a\u901a\u7528\u63a5\u53e3\uff0c\u7528\u6765\u8fde\u63a5\u63a5\u6536\u548c\u53d1\u9001\u5e94\u7528\u7a0b\u5e8f\u7684\u94fe\u63a5\u3002 \u57fa\u672c\u7528\u6cd5 \u5904\u7406\u6df1\u5ea6 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[182398],"tags":[],"class_list":["post-190960","post","type-post","status-publish","format-standard","hentry","category-mini-program"],"_links":{"self":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/190960","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/comments?post=190960"}],"version-history":[{"count":0,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/190960\/revisions"}],"wp:attachment":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media?parent=190960"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/categories?post=190960"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/tags?post=190960"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}