在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");