Javascript RNCalendarEvents.saveEvent()问题错误:未设置日历
我的Javascript RNCalendarEvents.saveEvent()问题错误:未设置日历,javascript,react-native,Javascript,React Native,我的react native calendar events代码抛出了一个错误,错误是没有设置日历,但我不知道为什么会发生这种情况。我的应用程序的旧版本上的相同代码在相同的设备上工作 export async function createCalendarEvent(event) { const status = await RNCalendarEvents.authorizationStatus(); console.log(status); if (status === "aut
react native calendar events
代码抛出了一个错误,错误是没有设置日历
,但我不知道为什么会发生这种情况。我的应用程序的旧版本上的相同代码在相同的设备上工作
export async function createCalendarEvent(event) {
const status = await RNCalendarEvents.authorizationStatus();
console.log(status);
if (status === "authorized" || status === "undetermined") {
addToCalendar(event);
} else {
RNCalendarEvents.authorizeEventStore()
.then(auth => {
// handle status
if (auth === "authorized" || auth === "undetermined") {
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("Oops! Something has gone wrong with this request.");
}
}
我最近在status==“authorized”| | status==“undetermined”
中添加了Or,这在某种程度上防止了iOS端在出错时完全崩溃
我认为这可能是一个版本问题,但我回到了我们的版本,我仍然有这个问题。所以承诺是错误的,但为什么
我设计了一个最低限度可行的产品,如下所示:
import React from "react";
import { Text, StyleSheet, View, TouchableOpacity } from "react-native";
import RNCalendarEvents from "react-native-calendar-events";
import parse from "date-fns/parse";
import format from "date-fns/format";
const ComponentsScreen = () => {
async function addToCalendar(event) {
console.log(RNCalendarEvents);
try {
RNCalendarEvents.saveEvent("Title of event", {
startDate: "2016-08-19T19:26:00.000Z",
endDate: "2017-08-19T19:26:00.000Z"
})
.then(() => {
alert("Event Saved");
})
.catch(rejectionReason => {
console.log(rejectionReason);
alert("Oops! Something has gone wrong.");
});
} catch (e) {
alert(e.message);
}
而saveEvent()
的RNCalendarEvents
方法返回未定义,即使RNCalendarEvents
I控制台日志返回此API可用的所有方法。我原以为这会导致链接问题,但它应该是自动链接,无论如何,我尝试了react native link
,但仍然没有定义它的方法
我刚刚在调试过程中注意到,react native calendar events
仍在使用index.ios.js
。这就是为什么我要返回RNCalendarEvents
API的方法,但是当我尝试实现它们时,我返回了undefined
更新:
我在MainActivity.java
中添加了以下内容:
package com.nfibengage;
import android.os.Bundle;
import com.facebook.react.ReactActivity;
import com.facebook.react.ReactActivityDelegate;
import org.devio.rn.splashscreen.SplashScreen;
import android.view.MotionEvent;
import com.facebook.react.ReactRootView;
import com.swmansion.gesturehandler.react.RNGestureHandlerEnabledRootView;
public class MainActivity extends ReactActivity {
/**
* Returns the name of the main component registered from JavaScript.
* This is used to schedule rendering of the component.
*/
@Override
protected String getMainComponentName() {
return "NFIBEngage";
}
/*
* this is for https://github.com/crazycodeboy/react-native-splash-screen
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
SplashScreen.show(this);
super.onCreate(savedInstanceState);
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
return super.dispatchTouchEvent(ev);
}
@Override
public void onRequestPermissionResult(int RequestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionResult(RequestCode, permissions, grantResults);
CalendarEventsPackage.onRequestPermissionResult(RequestCode, permissions, grantResults);
}
我仍然收到相同的错误消息,即未设置日历
所以我想我需要添加导入:import com.calendarevents.CalendarEventsPackage代码>。在我将其添加到MainActivity.java
并再次运行Android emulator后,在构建时我遇到了以下错误:
错误:方法不重写或实现超类型中的方法
错误:找不到符号方法
onRequestPermissionResult(int,String[],int[])错误:找不到
符号方法onRequestPermissionResult(int,String[],int[])
我能够解决第一个错误:error:method不重写或实现超类型中的方法
,只需从以下位置删除@override
:
@Override
public void onRequestPermissionResult(int RequestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionResult(RequestCode, permissions, grantResults);
CalendarEventsPackage.onRequestPermissionResult(RequestCode, permissions, grantResults);
}
我只能通过以下重构在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");
});
}
}
我只能通过以下重构在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");
});
}
}
看起来@KaramanOkan的问题是一样的,是的,有一点不同,在我的项目中,我在plist
中有必要的配置。我想你有权限问题,你能删除| status==“待定”
部分并重试吗?@KaramanOkan,这是有意义的,因为在今天的Android方面,我收到一个警告,上面写着:你没有从用户日历中读取事件的权限
,我不认为这个警告是硬编码到这个应用程序中的,它可能来自RNCalendarEvents
property?@KaramanOkan,我删除了| status==“待定”
,在Android上,它请求我允许访问日历,我单击OK,然后再次单击add to calendar,我得到了警报(“哎呀!出了点问题。”)代码>错误。看起来@KaramanOkan的问题是一样的,是的,有一点不同,在我的项目中,我在plist
中有必要的配置。我想你有权限问题,你能删除| status==“待定”
部分并重试吗?@KaramanOkan,这很有意义,因为今天在Android端,我收到一个警告,上面写着:你没有从用户日历中读取事件的权限
,而且我不认为这个警告是硬编码到这个应用程序中的,它可能来自RNCalendarEvents
property?@KaramanOkan,我删除了| status==“待定”
,在Android上,它请求我允许访问日历,我单击OK,然后再次单击add to calendar,我得到了警报(“哎呀!出了点问题。”)代码>错误。