IT이야기

기본 방식 대응 - 공유 방법 취소 이벤트

cyworld 2022. 3. 24. 21:42
반응형

기본 방식 대응 - 공유 방법 취소 이벤트

나는 Android에서 콘텐츠를 공유하기 위해 Resact Native Share Method(https://facebook.github.io/react-native/docs/share.html))를 사용하고 있다.

설명서에 따라 다음과 같은 방법으로 사용할 수 있다.

Share.share({
        message: 'React Native | A framework for building native apps using React'
    })
    .then((result) => {
        console.log(result.action) // returns sharedAction 
    })
    .catch((error) => {
        console.log(error)
    })

그래서 우리가 전화할 때Share방법, 우리는 결과를 얻을 것이다.then그리고 우리가 메시지 내용을 공유할 수 있는 앱 목록을 포함하는 팝업 창이 나타난다.

문제:

팝업 창에는 또한cancel버튼, 즉 사용자가 클릭하면 윈도우가 닫히지만 문서를 캡처할 수 있는 방법이 없다.

사용자가 클릭할 때 일부 작업을 수행하려고 하는데 취소 이벤트를 캡처할 수 있는 방법이 없을까?

조언해줘서 고마워 :)

share discledAction에는 옵션이 있지만 불행히도 이 옵션은 IOS에만 해당된다.prople "FailOnCancel"을 true와 함께 react-native-native-share를 사용할 수 있으며, 안드로이드와 ios 모두에서 작동한다.

샘플 코드

import React, { Component } from "react";
import { Button } from "react-native";

import Share from "react-native-share";

export default class ShareExample extends Component {
  onShare = async () => {
    const shareOptions = {
      title: "Share file",
      social: Share.Social.EMAIL,
      failOnCancel: true
    };

    try {
      const ShareResponse = await Share.open(shareOptions);
      //setResult(JSON.stringify(ShareResponse, null, 2));
    } catch (error) {
      console.log("Error =>", error);
    }
  };

  render() {
    return <Button onPress={this.onShare} title="Share" />;
  }
}

앱 미리보기

여기에 이미지 설명을 입력하십시오.

이것이 도움이 될 것이다.

import React, {Component} from 'react';
import {Share, Button} from 'react-native';

class ShareExample extends Component {
  onShare = async () => {
    try {
      const result = await Share.share({
        message:
          'React Native | A framework for building native apps using React',
      });

      if (result.action === Share.sharedAction) {
        if (result.activityType) {
          // shared with activity type of result.activityType
        } else {
          // shared
        }
      } else if (result.action === Share.dismissedAction) {
        // dismissed
      }
    } catch (error) {
      alert(error.message);
    }
  };

  render() {
    return <Button onPress={this.onShare} title="Share" />;
  }
}

참조URL: https://stackoverflow.com/questions/44095828/react-native-share-method-cancel-event

반응형