Javascript 如何在Android平台react-native 60+;上获得react-native日历事件;?
在我的react native项目从0.53.3升级到0.60.4后,我花了几周时间才让Javascript 如何在Android平台react-native 60+;上获得react-native日历事件;?,javascript,android,reactjs,react-native,Javascript,Android,Reactjs,React Native,在我的react native项目从0.53.3升级到0.60.4后,我花了几周时间才让react native calendar events库工作 在检查authorizationStatus之前,我通过重构一些代码来执行authorizationeventstore,使其在iOS端正常工作,如下所示: export async function createCalendarEvent(event) { const store = await RNCalendarEvents.autho
react native calendar events
库工作
在检查authorizationStatus
之前,我通过重构一些代码来执行authorizationeventstore
,使其在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+,但我已经没有想法了:
果不其然,上述实现不起作用,并且没有更新的文档。不确定我遗漏了什么,我已经通过自动链接在Android上设置了这个,通过上面的实现,但仍然没有
我一直未能从lib作者的公开问题中得到任何回应:
在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);
}
}
它继续打印出
警报(“哎呀!出了点问题。”)代码>与输出警报(“已保存事件”)的iOS端相反代码>通过删除指令中所述的所有手动破解,我能够让它在带有RN v0.61.4的Android上运行。使用自动链接,似乎可以在没有对gradle文件和*.java文件进行所有编码攻击的情况下工作。它在iOS上似乎也能正常工作。您发现问题了吗?我还注意到,在iOS上一切都很好,在Android上,它没有加入我的案例,但事件仍然不存在…@MatthieuBrucher,如果你有解决方案,我洗耳恭听。虽然我很欣赏Djohnson KC的回答,但我对这个特定应用程序的担忧是,有很多不受支持的第三方库,因此将其从0.60.4升级到0.61.4只是为了让这一功能正常工作,这会带来麻烦,并要求推迟几个月,这是我们无法承受的。不幸的是,这里没有解决办法。当然,这让我更讨厌Javascript生态系统,那里没有稳定的东西。与其他语言相比,它真是一团糟@MatthieuBrucher,我经常听到。部分原因是因为概念是事后实现的,也就是在语言已经被广泛使用之后,例如类的概念。现在,使用React或React Native调试调用堆栈是相当罕见的,因为这两个库都会混淆(或隐藏)真正的调用堆栈。换句话说,这两个库都添加了太多额外的函数调用,所以弄清楚到底发生了什么变得非常困难。