简体   繁体   中英

React Native WebView allow one domain

I have a simple webview,see below;

<WebView
            source={{ uri: 'https://example.com/',
                headers: {
                    Cookie: 'cookies=dismiss; ui=darkmode',
                },
            }}
            originWhitelist={['https://*']}
            allowsFullscreenVideo={true}
            pullToRefreshEnabled={true}
            sharedCookiesEnabled={true}
            geolocationEnabled={true}

        />

Now with inside my webview people can have profiles and links to third-party websites, how do I make those links to use an external web browser or expo web browser instead of loading them in my webview.

Using expo for the project.

if that url differs from the original domain, stops the loading, preventing page change, and opens it in the OS Navigator instead.

import * as Linking from 'expo-linking';

const webviewRef = useRef(null);

<WebView
        ref={webviewRef}
        source={{ uri : 'https://example.com/' }}
        onNavigationStateChange={(event) => {
          if (!event.url.includes("example.com")) {
            this.webview.stopLoading();
            Linking.openURL(event.url);
          }
        }}
      />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM