繁体   English   中英

如何在 Android 平台 React-Native 60+ 上获得 react-native-calendar-events?

[英]How do I get react-native-calendar-events working on Android platform React-Native 60+?

在我的 React Native 项目从 0.53.3 升级到 0.60.4 后,我苦苦挣扎了几个星期才能让react-native-calendar-events库在我的 React Native 项目上运行。

通过在检查authorizationStatus状态之前重构一些代码以执行authorizeEventStore ,我能够让它在 iOS 端工作,如下所示:

export async function createCalendarEvent(event) {
  const store = await RNCalendarEvents.authorizeEventStore();
  console.log(store);
  if (store === "authorize") {
    addToCalendar(event);
  } else {
    RNCalendarEvents.authorizationStatus()
      .then(auth => {
        // handle status
        if (auth === "authorized") {
          addToCalendar(event);
        }
      })
      .catch(() => {
        alert("This app needs calendar access");
      });
  }
}

不幸的是,这在应用程序的 Android 端不起作用。 我在这里遵循了 Android 设置指南,尽管由于自动链接,它现在不应该适用于 React-Native 60+,但我已经没有想法了:

https://github.com/wmcmahan/react-native-calendar-events/wiki/Android-setup

果然,上面的实现没有用,也没有更新的文档。 不知道我错过了什么,我已经通过自动链接在 Android 上设置了这个,通过上面的实现仍然没有。

我未能从 lib 的作者的一个未解决问题中获得任何回复: https://github.com/wmcmahan/react-native-calendar-events/issues/278

在 JavaScript 执行此代码时,在 Android 端:

export async function createCalendarEvent(event) {
  const store = await RNCalendarEvents.authorizeEventStore();
  console.log(store);
  if (store === "authorized") {
    addToCalendar(event);
  } else {
    RNCalendarEvents.authorizationStatus()
      .then(auth => {
        // handle status
        if (auth === "authorized") {
          addToCalendar(event);
        }
      })
      .catch(() => {
        alert("This app needs calendar access");
      });
  }
}

async function addToCalendar(event) {
  try {
    const startDate =
      Platform.OS === "ios"
        ? format(parse(event.StartDateLocal))
        : parse(event.StartDateLocal);
    const endDate =
      Platform.OS === "ios"
        ? format(parse(event.EndDateLocal))
        : parse(event.EndDateLocal);
    const allEvents = await RNCalendarEvents.fetchAllEvents(startDate, endDate);

    const calendarEvent = allEvents.find(e => e.title === event.Title);
    if (calendarEvent) {
      alert("You have already added this event to your calendar.");
    } else {
      const title = event.Title;

      const {
        Location: {
          AddressLine1: address,
          City: city,
          StateAbbreviation: state,
          PostalCode: zip
        }
      } = event;

      const location = `${address}, ${city}, ${state}, ${zip}`;

      const settings = {
        location,
        startDate,
        endDate
      };
      RNCalendarEvents.saveEvent(title, settings)
        .then(() => {
          alert("Event Saved");
        })
        .catch(rejectionReason => {
          console.log(rejectionReason);
          alert("Oops! Something has gone wrong.");
        });
    }
  } catch (e) {
    alert(e.message);
  }
}

它继续打印出alert("Oops. Something has gone wrong;"); 与打印出alert("Event Saved");

通过删除说明中说要做的所有手动黑客,我能够让它在 RN v0.61.4 的 Android 上工作。 使用自动链接,似乎可以在没有 gradle 文件和 *.java 文件的所有这些编码黑客的情况下工作。 它似乎也适用于 iOS。

暂无
暂无

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

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