我正在尝试在Android上保存日历活动和Expo应用。这是一些代码:
const details = {
endDate: "2018-05-16T20:00:00.000Z",
location: "...",
notes: "...",
startDate: "2018-05-16T19:00:00.000Z",
timeZone: "GMT-7",
title: "...",
url: "http://www..."
};
try {
console.log('Adding Event');
const eventId = await Calendar.createEventAsync(Calendar.DEFAULT, details);
console.log("Event Id", id);
}
catch(error) {
console.log('Error', error);
}
在这种情况下,会记录“添加事件”。但没什么。没有事件ID或错误。事件没有出现在我的日历中。
这是通过Android上的Expo应用程序运行的。
日历项目完全保存在iOS上。
我事先要求提供日历权限,所以我认为这不是问题所在。
这里可能出现什么问题?我该如何调试呢?
答案 0 :(得分:0)
尝试以这种方式创建事件。
var eventi= await Expo.Calendar.createEventAsync(Expo.Calendar.DEFAULT,{
startDate: new Date('2018-09-14'),
endDate: new Date('2018-12-31'),
title: "push",
timeZone: "GMT-7",
alarms:
})
.then( event => {
console.log('success',event);
})
.catch( error => {
console.log('failure',error);
});
在将事件作为const传递时,我也遇到了一些问题,但以这种方式解决了这些问题。 我不确定是什么引起了错误。
答案 1 :(得分:0)
如文档中所述,获取默认日历的功能仅在ios设备上可用:
Calendar.getDefaultCalendarAsync()仅iOS。获取的实例 默认日历对象。 https://docs.expo.io/versions/latest/sdk/calendar/#calendargetdefaultcalendarasync
在Android上,您可以遍历getCalendarsAsync的结果,直到找到带有isPrimary:true的结果为止。像这样:
const calendars = await Calendar.getCalendarsAsync();
const calendar = calendars.find(({isPrimary})=>isPrimary);
答案 2 :(得分:0)
我一直在尝试直接从本机应用程序执行此操作,但是由于一些简单的事情而变得如此复杂。
如果有人想要一个简单快速的解决方案,只需创建一个iCS(通用日历格式)文件并将其上传到某种直接文件下载云(我使用firebase)中,而当用户想要添加到日历中时,只需将其发送至ICS日历链接下载。
答案 3 :(得分:0)
当我自己尝试(2020年初)在Android上尝试时,我想出了设备中所有具有isPrimary:false的内部日历。
使用Android,您可以首先创建一个独立的日历,您的应用将对其进行更新(这将反映在设备日历应用中)。为避免每次尝试保存新事件时日历都会使设备device肿,您可以首先搜索以查看日历是否已存在于设备中。然后,您可以将其添加到该目录中,也可以创建一个新目录(初始运行)
var defaultCal //the calendar ID you are passing to createEventAsync()
if( Platform.OS == 'ios' ) {
defaultCal = getDefaultCalendarId()
} else {
defaultCal = getAndroidCal()
}
async function getAndroidCal() {
var androidCalendar
var foundCal = false
// retrieve internal calendars
var androidCalendars = await Calendar.getCalendarsAsync();
// iterate through them
for( var x=0; x<androidCalendars.length; x++ ) {
// check each to see if their source name matches what you have
if ( androidCalendars[x]["source"]["name"] == "*your identifier*" ) {
foundCal = true
// save the calendar id and stop searching
androidCalendar = androidCalendars[x]["id"]
break
}
}
// initial run
if( foundCal === false ) {
androidCalendar = await createCalendar()
}
return androidCalendar
}
// create a calendar for android
async function createCalendar() {
const newCalendarSource = {
isLocalAccount: true,
name: '*your identifier*'
}
const newCalendarID = await Calendar.createCalendarAsync({
title: *your title*,
color: *your color*,
entityType: Calendar.EntityTypes.EVENT,
sourceId: newCalendarSource.id,
source: newCalendarSource,
name: *calendar name*,
ownerAccount: 'personal',
accessLevel: Calendar.CalendarAccessLevel.OWNER,
});
return newCalendarID
}
希望这对某人有帮助!