iOS 鏈接

2019-08-14 14:23 更新

LinkingIOS 給你提供了一個(gè)通用接口,用來(lái)連接接收和發(fā)送應(yīng)用程序的鏈接。

基本用法

處理深度鏈接

如果你的應(yīng)用程序是從一個(gè)外部鏈接啟動(dòng)的,并且這個(gè)外部鏈接是注冊(cè)到你的應(yīng)用程序里的,那么你就可以利用任意你想要的組件去訪(fǎng)問(wèn)并且處理它

    componentDidMount() {     var url = LinkingIOS.popInitialURL();
    }

在你的應(yīng)用程序運(yùn)行期間,如果你也想監(jiān)聽(tīng)傳入應(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)聽(tīng) LinkingIOS 上的事件,如下所示:

    componentDidMount() {
      LinkingIOS.addEventListener('url', this._handleOpenURL);
    },
    componentWillUnmount() {
      LinkingIOS.removeEventListener('url', this._handleOpenURL);
    },
    _handleOpenURL(event) {
      console.log(event.url);
    }

觸發(fā)應(yīng)用程序鏈接

為了觸發(fā)一個(gè)應(yīng)用程序的鏈接(瀏覽器,電子郵件,或者自定義模式),你需要調(diào)用

LinkingIOS.openURL(url)

如果你想要檢查一個(gè)已經(jīng)安裝的應(yīng)用程序是否可以提前處理一個(gè)給定的鏈接,你可以調(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)

通過(guò)監(jiān)聽(tīng) url 事件類(lèi)型和提供處理程序,將一個(gè)處理程序添加到 LinkingIOS changes

static removeEventListener(type: string, handler: Function)

通過(guò)傳遞 url 事件類(lèi)型和處理程序,刪除一個(gè)處理程序

static openURL(url: string)

嘗試通過(guò)任意已經(jīng)安裝的應(yīng)用程序打開(kāi)給定的 url

static canOpenURL(url: string, callback: Function)

決定一個(gè)已經(jīng)安裝的應(yīng)用程序是否可以處理一個(gè)給定的 url,該方法中回調(diào)函數(shù)將被調(diào)用,并且僅通過(guò)一個(gè) bool supported 的參數(shù)。

static popInitialURL()

如果應(yīng)用程序啟動(dòng)是通過(guò)一個(gè)應(yīng)用程序鏈接觸發(fā)的,那么它將彈出這個(gè)鏈接的 url,否則它將返回 null。


以上內(nèi)容是否對(duì)您有幫助:
在線(xiàn)筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)