Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
React native 响应本机openURL Twitter链接问题_React Native - Fatal编程技术网

React native 响应本机openURL Twitter链接问题

React native 响应本机openURL Twitter链接问题,react-native,React Native,我正在我的应用程序中显示一个twitter提要,该提要是从twitter API中拉入的。由于tweet没有显示全文,因此有一个twitter短代码URL(t.co)来查看完整的tweet 我遇到的问题是,当我使用openURL单击URL时,它会在浏览器中打开,从而重定向到twitter应用程序并重定向回浏览器以显示推文 如果我已经打开了twitter应用程序,那么这个方法就行了——如果它没有打开,最终重定向回浏览器不会加载页面 如果没有安装twitter应用程序,一切正常,它只需加载到浏览器中

我正在我的应用程序中显示一个twitter提要,该提要是从twitter API中拉入的。由于tweet没有显示全文,因此有一个twitter短代码URL(t.co)来查看完整的tweet

我遇到的问题是,当我使用openURL单击URL时,它会在浏览器中打开,从而重定向到twitter应用程序并重定向回浏览器以显示推文

如果我已经打开了twitter应用程序,那么这个方法就行了——如果它没有打开,最终重定向回浏览器不会加载页面

如果没有安装twitter应用程序,一切正常,它只需加载到浏览器中即可


有没有一种方法可以避免重定向,只要在twitter应用程序中打开t.co URL(如果安装了的话)?

因此我设法解决了这个问题,首先在PHP中创建了一个URL取消排序器:

<?php 
$url = $_GET['url'];

$context = stream_context_create(
array(
    'http' => array(
        'follow_location' => false
    )
)
);

$html = file_get_contents($url, false, $context);

$final = str_replace("location: ",'',$http_response_header[5]);

echo json_encode ($final,JSON_PRETTY_PRINT);
?>
将URL字符串中的“i/web”替换为“web”非常重要,这样应用程序就可以以本机方式打开链接,而不是打开移动链接

if (href.contains('https')) {

    var short = {
        getInfo() {

          var myUrl = 'expander.php?url='+href

            return fetch(myUrl).then((res) => res.json()).catch(function(error) {
                Alert.alert(error+''); 
            });
        }
    }

   short.getInfo().then((res) => {

    var final = res.replace('i/web','web');

    Linking.openURL(final);   

   })
} else {

  Linking.openURL(href)
}