简体   繁体   English

从使用 Expo SDK 构建的 React-Native 应用程序将照片分享到 Instagram

[英]Share photo to Instagram from React-Native app built with Expo SDK

I want my react-native app to share a photo with Instagram.我希望我的 react-native 应用程序与 Instagram 分享照片。 I know it's possible when writing in native code to open up Instagram's filter screen with a specified photo.我知道在用本机代码编写时可以用指定的照片打开 Instagram 的过滤器屏幕。 A restriction is that I'm using the Expo SDK, which doesn't allow 'npm link' for native dependencies.一个限制是我使用的是 Expo SDK,它不允许本地依赖项的“npm 链接”。

When this function is called, it will open Instagram:当这个函数被调用时,它会打开 Instagram:

  _handlePress = () => {
    Linking.openURL('instagram://app');
  }

This is the Button:这是按钮:

   <Button 
      onPress={this._handlePress}
      title='Open Instagram'
    />

The image is stored in the state:图像存储在状态:

  state = {
    image: null,
    uploading: false
  }

I can display the image in an Image tag just fine:我可以在 Image 标签中显示图像就好了:

<Image
  source={{uri: image}}
  style={{width: 300, height: 300}}
 />

But, I have no way passing the image along to Instagram.但是,我无法将图像传递给 Instagram。 Here is some failed research: Third party libraries: react-native-instagram-share: https://www.instagram.com/developer/mobile-sharing/iphone-hooks/以下是一些失败的研究:第三方库:react-native-instagram-share: https : //www.instagram.com/developer/mobile-sharing/iphone-hooks/

Because I can't use 'link' to use native dependencies.因为我不能使用“链接”来使用本机依赖项。 I am using the Expo SDK which doesn't allow the 'link' for native dependencies.我正在使用 Expo SDK,它不允许本地依赖项的“链接”。

The Instagram docs explain how to open Instagram, and that passing the image along can be done with native code. Instagram 文档解释了如何打开 Instagram,并且可以使用本机代码来传递图像。 Which, is to use “UIDocumentInteractionController”, which isn't available in JavaScript.其中,是使用“UIDocumentInteractionController”,它在 JavaScript 中不可用。
https://www.instagram.com/developer/mobile-sharing/iphone-hooks/ https://www.instagram.com/developer/mobile-sharing/iphone-hooks/

There aren't any other Stackoverflow answers to my question.我的问题没有任何其他 Stackoverflow 答案。

I put together an example that you can run on iOS here: https://snack.expo.io/rkbW-EG7-我在这里整理了一个可以在 iOS 上运行的示例: https : //snack.expo.io/rkbW-EG7-

Full code below:完整代码如下:

import React, { Component } from 'react';
import { Linking, Button, View, StyleSheet } from 'react-native';
import { ImagePicker } from 'expo';

export default class App extends Component {
  render() {
    return (
      <View style={styles.container}>
        <Button title="Open camera roll" onPress={this._openCameraRoll} />
      </View>
    );
  }

  _openCameraRoll = async () => {
    let image = await ImagePicker.launchImageLibraryAsync();
    let { origURL } = image;
    let encodedURL = encodeURIComponent(origURL);
    let instagramURL = `instagram://library?AssetPath=${encodedURL}`;
    Linking.openURL(instagramURL);
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
    backgroundColor: '#ecf0f1',
  },
});

This will let you open up your camera roll and pick an image, then open the Instagram app with that image selected.这将让您打开相机胶卷并选择一张图像,然后打开选择该图像的 Instagram 应用程序。 If the image isn't already on the camera roll, then you can use CameraRoll.saveToCameraRoll on the image ( link to React Native documentation ) to get it there.如果图像不在相机胶卷上,那么您可以在图像上使用CameraRoll.saveToCameraRoll链接到 React Native 文档)来获取它。

The approach used in this example will only work if you are OK with sharing from the camera roll, however, and I am unsure about how to do this on Android.但是,此示例中使用的方法仅在您可以通过相机胶卷进行共享时才有效,而且我不确定如何在 Android 上执行此操作。 I made an issue on Expo's feature request board to make sure that Instagram sharing works great out of the box.在 Expo 的功能请求板上提出了一个问题,以确保 Instagram 共享开箱即用。

Here is how you post这是你发帖的方式

import { CameraRoll } from 'react-native'

callThisFunction = async () => {
      await CameraRoll.saveToCameraRoll("https://images.unsplash.com/photo-1504807417934-b7fdec306bfd?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&w=1000&q=80", 'photo');
      let instagramURL = `instagram://library?AssetPath=null`;
      Linking.openURL(instagramURL);
  }

Here is how to share a file from a remote url: download it first!以下是从远程 url 共享文件的方法:首先下载它! :) :)

  const downloadPath = FileSystem.cacheDirectory + 'fileName.jpg';
  // 1 - download the file to a local cache directory
  const { uri: localUrl } = await FileSystem.downloadAsync(remoteURL, downloadPath);
  // 2 - share it from your local storage :)
  Sharing.shareAsync(localUrl, {
    mimeType: 'image/jpeg',            // Android
    dialogTitle: 'share-dialog title', // Android and Web
    UTI: 'image/jpeg'                  // iOS
  });

Worked For Me :)为我工作:)

onClick = () => {
  ImagePicker.launchImageLibrary(options, response =>{
    console.log(response);
    let instagramURL = `instagram://library?LocalIdentifier=`+response.origURL;
    Linking.openURL(instagramURL);
  })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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