我花了几个星期的时间才得到 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/