LinkingIOS 給你提供了一個通用接口,用來連接接收和發(fā)送應(yīng)用程序的鏈接。
如果你的應(yīng)用程序是從一個外部鏈接啟動的,并且這個外部鏈接是注冊到你的應(yīng)用程序里的,那么你就可以利用任意你想要的組件去訪問并且處理它
componentDidMount() {
var url = LinkingIOS.popInitialURL();
}
在你的應(yīng)用程序運行期間,如果你也想監(jiān)聽傳入應(yīng)用程序的鏈接,那么你需要將以下幾行添加到你的 *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ā)一個應(yīng)用程序的鏈接(瀏覽器,電子郵件,或者自定義模式),你需要調(diào)用
LinkingIOS.openURL(url)
如果你想要檢查一個已經(jīng)安裝的應(yī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)安裝的應(yīng)用程序打開給定的 url
static canOpenURL(url: string, callback: Function)
決定一個已經(jīng)安裝的應(yīng)用程序是否可以處理一個給定的 url,該方法中回調(diào)函數(shù)將被調(diào)用,并且僅通過一個 bool supported 的參數(shù)。
static popInitialURL()
如果應(yīng)用程序啟動是通過一個應(yīng)用程序鏈接觸發(fā)的,那么它將彈出這個鏈接的 url,否則它將返回 null。