W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
LinkingIOS
給你提供了一個通用接口,用來連接接收和發(fā)送應用程序的鏈接。
如果你的應用程序是從一個外部鏈接啟動的,并且這個外部鏈接是注冊到你的應用程序里的,那么你就可以利用任意你想要的組件去訪問并且處理它
componentDidMount() { var url = LinkingIOS.popInitialURL(); }
在你的應用程序運行期間,如果你也想監(jiān)聽傳入應用程序的鏈接,那么你需要將以下幾行添加到你的 *AppDelegate.m
:
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation { return [RCTLinkingManager application:application openURL:url sourceApplication:sourceApplication annotation:annotation]; }
那么,在你的 React 組件中,你可以監(jiān)聽 LinkingIOS
上的事件,如下所示:
componentDidMount() { LinkingIOS.addEventListener('url', this._handleOpenURL); }, componentWillUnmount() { LinkingIOS.removeEventListener('url', this._handleOpenURL); }, _handleOpenURL(event) { console.log(event.url); }
為了觸發(fā)一個應用程序的鏈接(瀏覽器,電子郵件,或者自定義模式),你需要調(diào)用
LinkingIOS.openURL(url)
如果你想要檢查一個已經(jīng)安裝的應用程序是否可以提前處理一個給定的鏈接,你可以調(diào)用
LinkingIOS.canOpenURL(url, (supported) => { if (!supported) { AlertIOS.alert('Can\'t handle url: ' + url); } else { LinkingIOS.openURL(url); } });
static addEventListener(type: string, handler: Function)
通過監(jiān)聽 url
事件類型和提供處理程序,將一個處理程序添加到 LinkingIOS changes
static removeEventListener(type: string, handler: Function)
通過傳遞 url
事件類型和處理程序,刪除一個處理程序
static openURL(url: string)
嘗試通過任意已經(jīng)安裝的應用程序打開給定的 url
static canOpenURL(url: string, callback: Function)
決定一個已經(jīng)安裝的應用程序是否可以處理一個給定的 url
,該方法中回調(diào)函數(shù)將被調(diào)用,并且僅通過一個 bool supported
的參數(shù)。
static popInitialURL()
如果應用程序啟動是通過一個應用程序鏈接觸發(fā)的,那么它將彈出這個鏈接的 url,否則它將返回 null
。
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: