NativeScript Calendar
与本地日历交互。添加、更新、读取,你想要的功能都有。
npm i --save nativescript-calendar

NativeScript Calendar 插件

Build Status NPM version Downloads Twitter Follow

该插件允许您操作用户本地日历中的事件。您可以在默认或自定义日历中查找、创建和删除事件。

如果您在寻找一个出色的本地日历内联UI,那么请查看这个

安装

从命令提示符进入您的应用程序根文件夹并执行

NativeScript 7

tns plugin add nativescript-calendar

NativeScript 6

tns plugin add [email protected]

iOS 运行时权限原因

您可能之前已经见过类似的权限提示(此插件也会自动触发一个)

iOS 10+不仅需要这个提示,还需要一个原因。在这种情况下是“来自 App_Resources 的自定义消息”。

您可以通过将类似以下内容添加到app/App_Resources/ios/Info.plist来提供访问日历的自己的原因

  <key>NSCalendarsUsageDescription</key>
<string>My reason justifying fooling around with your calendar</string>

为了避免忘记提供原因而使您的应用程序崩溃,此插件在构建期间将一个空原因添加到.plist。此值会被您指定的任何内容覆盖。欢迎您。

TypeScript 使用

当然,您可以使用 TypeScript 使用此插件,只需导入插件并像下面这样使用汇总的函数

import * as Calendar from "nativescript-calendar";

// example for listCalendars:
Calendar.listCalendars().then(/* .. */);

用法

如果您想要快速入门,请克隆我们的演示应用程序

createEvent

  var Calendar = require("nativescript-calendar");

// Only the `title`, `startDate` and `endDate` are mandatory, so this would suffice:
var options = {
title: 'Get groceries',
// Make sure these are valid JavaScript Date objects.
// In this case we schedule an Event for now + 1 hour, lasting 1 hour.
startDate: new Date(new Date().getTime() + (60*60*1000)),
endDate: new Date(new Date().getTime() + (2*60*60*1000))
};

// You can however add lots of properties to enrich the Event:
options.location = 'The shop';
options.notes = 'This event has reminders';

// iOS has a separate 'url' field, but on Android the plugin appends this to the 'notes' field.
options.url = 'http://my.shoppinglist.com';

// You can also override the default reminder(s) of the Calendar (in minutes):
options.reminders = {
first: 30,
second: 10
};

// You can make this Event recurring (this one repeats every other day for 10 days):
options.recurrence = {
frequency: "daily", // daily | weekly | monthly | yearly
interval: 2, // once every 2 days
endDate: new Date(new Date().getTime() + (10*24*60*60*1000)) // 10 days
};

// Want to use a custom calendar for your app? Pass in the 'name'.
// If the name doesn't yet exist the plugin will create it for you.
options.calendar = {
name: "NativeScript Cal",
// the color, in this case red
color: "#FF0000",
// Can be used on Android to group the calendars. Examples: Your app name, or an emailaddress
accountName: "My App Name"
};

Calendar.createEvent(options).then(
function(createdId) {
console.log("Created Event with ID: " + createdId);
},
function(error) {
console.log("Error creating an Event: " + error);
}
);

如果您想要一个“全天事件”,请确保将日期设置为午夜,如下所示

  var d = new Date();
d.setHours(0);
d.setMinutes(0);
d.setSeconds(0);

// this will create an 'all day event' for tomorrow
var startDate = new Date(d.getTime() + (24*60*60*1000));
var endDate = new Date(d.getTime() + (2*24*60*60*1000));
// .. now use these properties in the options object

findEvents

  var options = {
// when searching, dates are mandatory - the event must be within this interval
startDate: new Date(new Date().getTime() - (50*24*60*60*1000)),
endDate: new Date(new Date().getTime() + (50*24*60*60*1000))
};

// if you know the Event ID, set it here:
options.id = '123456';

// you can optionally pass in a few other properties, any event containing these will be returned:
options.title = 'groceries';
options.location = 'foo';
options.notes = 'bar'; // iOS only

Calendar.findEvents(options).then(
function(events) {
console.log(JSON.stringify(events));
},
function(error) {
console.log("Error finding Events: " + error);
}
);

返回的 'events' 对象是一个具有以下属性的 JSON 事件数组

id
title
location
notes
url
startDate
endDate
allDay
calendar {id, name}
reminders {minutes}
recurrence {frequency, interval, endDate}
attendees {name, email, url, status, role, type}

deleteEvents

用法与 findEvents 类似,只是结果略有不同 ;)

  var options = {
// when searching, dates are mandatory - the event must be within this interval
startDate: new Date(new Date().getTime() - (50*24*60*60*1000)),
endDate: new Date(new Date().getTime() + (50*24*60*60*1000))
};

// if you know the Event ID, set it here:
options.id = '123456';

// you can optionally pass in a few other properties, any event containing these will be deleted:
options.title = 'groceries'; // events _including_ this string will be included in the selection
options.location = 'foo';
options.notes = 'bar'; // iOS only

Calendar.deleteEvents(options).then(
function(deletedEventIds) {
console.log(JSON.stringify(deletedEventIds));
},
function(error) {
console.log("Error deleting Events: " + error);
}
)

listCalendars

  Calendar.listCalendars().then(
function(calendars) {
// a JSON array of Calendar objects is returned, each with an 'id' and 'name'
console.log("Found these Calendars on the device: " + JSON.stringify(calendars));
},
function(error) {
console.log("Error while listing Calendars: " + error);
}
)

deleteCalendar

TypeScript
import * as Calendar from "nativescript-calendar";

Calendar.deleteCalendar({
name: "My Calendar name"
}).then(id => {
// id is null if nothing was deleted
console.log(`Deleted Calendar with id ${id}`);
});

2.0.0 版本的重大更改

CHANGELOG.md