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

标签 javascript android reactjs react-native

我花了几个星期的时间才得到 react-native-calendar-events从 0.53.3 升级到 0.60.4 后,该库在我的 React Native 项目上工作。

通过重构一些代码来执行authorizeEventStore,我能够让它在iOS端工作。检查之前authorizationStatus像这样:

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 上设置了这个,通过上面的实现,但仍然没有。

我未能从该库作者的未解决问题中得到任何回复: 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);
  }
}

它继续打印出alert("Oops! Something has gone wrong.");而 iOS 端则打印出 alert("Event Saved");

最佳答案

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

关于javascript - 如何让react-native-calendar-events在Android平台React-Native 60+上运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58695437/

相关文章:

javascript - PHP 不会发布值表

reactjs - 使用 create-react-app 和 webpack 发出 CORS API 请求 - 无 express

reactjs - 如何使用 Electron 进行热模块替换

javascript - JS : Sum of span values in For loop

javascript - 如何从javascript中的路径获取图像大小?

android - 以编程方式更改工具栏主题

Android:meminfo_proc_show() 3.10.65+ 尝试执行 LOS12.1 端口时启动循环内核崩溃

android - Flutter - SQFlite - 类型 'String' 不是类型 'double' 的子类型

javascript - 未找到带有 svg 的 gatsby-background-image

javascript - 需要从父div中找出子div