如何让 react-native-calendar-events 在 Android 平台 React-Native 60+ 上运行?

Dan*_*iel 5 javascript android reactjs react-native

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

authorizeEventStoreauthorizationStatus像这样检查之前,我能够通过重构一些代码来让它在 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");
      });
  }
}
Run Code Online (Sandbox Code Playgroud)

不幸的是,这在应用程序的 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

在 Android 端 JavaScript 执行此代码时:

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);
  }
}
Run Code Online (Sandbox Code Playgroud)

它继续打印出来alert("Oops! Something has gone wrong.");,而不是打印出来的 iOS 端alert("Event Saved");

djo*_*nkc 3

通过删除说明中所说的所有手动修改,我能够使其在 RN v0.61.4 上适用于 Android。通过自动链接,似乎无需对 gradle 文件和 *.java 文件进行所有这些编码修改即可工作。它似乎也可以在 iOS 上正常工作。