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

react-native-calendar-events库从我的React Native项目从0.53.3升级到0.60.4之后,我努力工作了两周。

我能够通过在检查authorizeEventStore之前重构一些代码以执行authorizationStatus来使其在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.");,而不是iOS端打印出alert("Event Saved");

ranran_001 回答:如何在Android平台React-Native 60+上运行react-native-calendar-events?

通过删除说明中指出的所有手动技巧,我得以使其在RN v0.61.4上适用于Android。使用自动链接,似乎可以在不对gradle文件和* .java文件进行所有这些编码hack的情况下工作。在iOS上似乎也可以正常工作。

本文链接:https://www.f2er.com/3166012.html

大家都在问