首页 > 解决方案 > React Native 如何在本机应用程序上打开社交媒体 URL

问题描述

我想在我的应用程序上添加我的社交媒体链接。但是当我用 url 添加它们时,它们会在浏览器上打开。当我使用 facebook:// 系统时,它在 Android 上不起作用。

我希望我的 URL 像“如果应用程序存在,则在应用程序上打开它”一样工作。

这在Android上不起作用:

import * as Linking from 'expo-linking'

          <TouchableOpacity style={[styles.iconButton, { backgroundColor: '#3b5998' }]} onPress={() => Linking.openURL('fb://page/blablabla')}>
            <FontAwesome name="facebook" size={26} color="#fff" />
          </TouchableOpacity>

这不会在他们的本机应用程序上打开 URL:

          <TouchableOpacity style={[styles.iconButton, { backgroundColor: '#3b5998' }]} onPress={() => Linking.openURL('https://www.facebook.com/blablabla')}>
            <FontAwesome name="facebook" size={26} color="#fff" />
          </TouchableOpacity>

标签: react-nativeexpo

解决方案


这就是您可以打开链接的方式。

import { View, Text, Image, Linking, TouchableOpacity} from 'react-native'


<TouchableOpacity style={{ marginVertical: 5 }} onPress={()=>{ Linking.openURL(item.link)}}>
   <Text style={{ alignSelf: 'center', color: '#2575FC' }}>
       Click here to read more
   </Text>
</TouchableOpacity>

推荐阅读