【问题标题】:Phonegap 3.0.0 Calendar PluginPhonegap 3.0.0 日历插件
【发布时间】:2013-09-04 19:22:50
【问题描述】:

我正在寻找一个专门与 iOS 配合使用的日历插件,但我也对一个与 Phonegap 3.0.0 兼容的 Android 插件感兴趣。我见过旧的,但据我所知,它不适用于最新版本的 Phonegap。有谁知道我在哪里可以获得适用于 Phonegap 3.0.0 的插件?

【问题讨论】:

    标签: cordova phonegap-plugins


    【解决方案1】:

    如果有人仍在寻找适用于 PhoneGap 3.0(也适用于 PhoneGap Build)的跨平台(iOS 和 Android)日历,请查看此处:https://github.com/EddyVerbruggen/Calendar-PhoneGap-Plugin

    【讨论】:

    • HI Eddy,日历插件适用于安卓和IOS。但不适用于黑莓。
    • 正确,仅适用于 iOS 和 Android
    • 感谢 eddy,关于黑莓的任何其他想法。
    • 它与 Apache Cordova 兼容吗?
    • 是的,它确实是一个 Cordova 插件。
    【解决方案2】:

    PhoneGap 插件部分有一个“官方的”第 3 方日历插件:

    https://build.phonegap.com/plugins/168

    插件 ID nl.x-services.plugins.calendar

    版本 3.2

    平台 安卓、iOS

    PhoneGap 版本 3.0.0, 3.1.0

    许可证 麻省理工学院

    【讨论】:

      【解决方案3】:

      嗨,我只是分别为 android 和 ios 构建了一个插件,我从 Felix Montanez 更改了插件,以便它与 phonegap 3.0.0 for android 一起使用平台:

      calendar.js:

      function CalendarPlugin() {}
      
      CalendarPlugin.prototype.createEvent = function(title, location, notes, startDate, endDate, successCallback, errorCallback) {
          if (typeof errorCallback != "function") {
              console.log("CalendarPlugin.createEvent failure: errorCallback parameter must be a function");
              return;
          }
      
          if (typeof successCallback != "function") {
              console.log("CalendarPlugin.createEvent failure: successCallback parameter must be a function");
              return;
          }
          cordova.exec(successCallback, errorCallback, "CalendarPlugin", "createEvent", [title, location, notes, startDate, endDate, {
              "title": title,
              "description": notes,
              "eventLocation": location,
              "startTimeMillis": startDate.getTime(),
              "endTimeMillis": endDate.getTime()
          }]);
      };
      
      CalendarPlugin.install = function() {
          if (!window.plugins) {
              window.plugins = {};
          }
      
          window.plugins.CalendarPlugin = new CalendarPlugin();
          return window.plugins.CalendarPlugin;
      };
      
      cordova.addConstructor(CalendarPlugin.install);
      

      日历插件.h

      #import <Foundation/Foundation.h>
      #import <Cordova/CDVPlugin.h>
      #import <Cordova/CDV.h>
      #import <EventKitUI/EventKitUI.h>
      #import <EventKit/EventKit.h>
      
      
      @interface CalendarPlugin : CDVPlugin
      
      @property (nonatomic, retain) EKEventStore* eventStore;
      
      - (void)initEventStoreWithCalendarCapabilities;
      
      -(NSArray*)findEKEventsWithTitle: (NSString *)title
                              location: (NSString *)location
                               message: (NSString *)message
                             startDate: (NSDate *)startDate
                               endDate: (NSDate *)endDate;
      
      // Calendar Instance methods
      
      - (void)createEvent:(CDVInvokedUrlCommand*)command;
      - (void)modifyEvent:(CDVInvokedUrlCommand*)command;
      - (void)findEvent:(CDVInvokedUrlCommand*)command;
      - (void)deleteEvent:(CDVInvokedUrlCommand*)command;
      
      @end
      

      日历插件.m

      #import "CalendarPlugin.h"
      #import <EventKitUI/EventKitUI.h>
      #import <EventKit/EventKit.h>
      
      #define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
      
      @implementation CalendarPlugin
      @synthesize eventStore;
      
      #pragma mark Initialisation functions
      
      - (CDVPlugin*) initWithWebView:(UIWebView*)theWebView
      {
          self = (CalendarPlugin*)[super initWithWebView:theWebView];
          if (self) {
              //[self setup];
              [self initEventStoreWithCalendarCapabilities];
          }
          return self;
      }
      
      - (void)initEventStoreWithCalendarCapabilities {
          __block BOOL accessGranted = NO;
          eventStore= [[EKEventStore alloc] init];
          if([eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)]) {
              dispatch_semaphore_t sema = dispatch_semaphore_create(0);
              [eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
                  accessGranted = granted;
                  dispatch_semaphore_signal(sema);
              }];
              dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
          } else { // we're on iOS 5 or older
              accessGranted = YES;
          }
      
          if (accessGranted) {
              self.eventStore = eventStore;
          }
      }
      
      #pragma mark Helper Functions
      
      -(NSArray*)findEKEventsWithTitle: (NSString *)title
                              location: (NSString *)location
                               message: (NSString *)message
                             startDate: (NSDate *)startDate
                               endDate: (NSDate *)endDate {
      
          // Build up a predicateString - this means we only query a parameter if we actually had a value in it
          NSMutableString *predicateString= [[NSMutableString alloc] initWithString:@""];
          if (title.length > 0) {
              [predicateString appendString:[NSString stringWithFormat:@"title == '%@'" , title]];
          }
          if (location.length > 0) {
              [predicateString appendString:[NSString stringWithFormat:@" AND location == '%@'" , location]];
          }
          if (message.length > 0) {
              [predicateString appendString:[NSString stringWithFormat:@" AND notes == '%@'" , message]];
          }
      
          NSPredicate *matches = [NSPredicate predicateWithFormat:predicateString];
      
          NSArray *datedEvents = [self.eventStore eventsMatchingPredicate:[eventStore predicateForEventsWithStartDate:startDate endDate:endDate calendars:nil]];
      
      
          NSArray *matchingEvents = [datedEvents filteredArrayUsingPredicate:matches];
      
      
          return matchingEvents;
      }
      
      #pragma mark Cordova functions
      
      - (void)createEvent:(CDVInvokedUrlCommand*)command {
          // Import arguments
      
          NSArray *arguments = command.arguments;
      
          NSString* title      = [arguments objectAtIndex:0];
          NSString* location   = [arguments objectAtIndex:1];
          NSString* message    = [arguments objectAtIndex:2];
          NSString *startDate  = [arguments objectAtIndex:3];
          NSString *endDate    = [arguments objectAtIndex:4];
      
          //creating the dateformatter object
          NSDateFormatter *df = [[NSDateFormatter alloc] init];
          [df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
          NSDate *myStartDate = [df dateFromString:startDate];
          NSDate *myEndDate = [df dateFromString:endDate];
      
      
          EKEvent *myEvent = [EKEvent eventWithEventStore: self.eventStore];
          myEvent.title = title;
          myEvent.location = location;
          myEvent.notes = message;
          myEvent.startDate = myStartDate;
          myEvent.endDate = myEndDate;
          myEvent.calendar = self.eventStore.defaultCalendarForNewEvents;
      
      
          EKAlarm *reminder = [EKAlarm alarmWithRelativeOffset:-2*60*60];
      
          [myEvent addAlarm:reminder];
      
          NSError *error = nil;
          [self.eventStore saveEvent:myEvent span:EKSpanThisEvent error:&error];
      
          // Check error code + return result
          CDVPluginResult* pluginResult = nil;
      
          if (!error) {
              pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
          } else {
              pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
          }
      
          [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
      
      }
      -(void)deleteEvent:(CDVInvokedUrlCommand*)command {
          // Import arguments
      
          NSArray *arguments = command.arguments;
      
          NSString* title      = [arguments objectAtIndex:0];
          NSString* location   = [arguments objectAtIndex:1];
          NSString* message    = [arguments objectAtIndex:2];
          NSString *startDate  = [arguments objectAtIndex:3];
          NSString *endDate    = [arguments objectAtIndex:4];
          bool delAll = [arguments objectAtIndex:5];
      
          NSDateFormatter *df = [[NSDateFormatter alloc] init];
          [df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
          NSDate *myStartDate = [df dateFromString:startDate];
          NSDate *myEndDate = [df dateFromString:endDate];
      
          NSArray *matchingEvents = [self findEKEventsWithTitle:title location:location message:message startDate:myStartDate endDate:myEndDate];
      
      
          if (delAll || matchingEvents.count == 1) {
              // Definitive single match - delete it!
              NSError *error = NULL;
              bool hadErrors = false;
              if (delAll) {
                  for (EKEvent * event in matchingEvents) {
                      [self.eventStore removeEvent:event span:EKSpanThisEvent error:&error];
                      // Check for error codes and return result
                      if (error) {
                          hadErrors = true;
                      }
                  }
              }
              else {
                  [self.eventStore removeEvent:[matchingEvents lastObject] span:EKSpanThisEvent error:&error];
              }
              // Check for error codes and return result
              CDVPluginResult* pluginResult = nil;
              if (error || hadErrors) {
                  NSString *messageString;
                  if (hadErrors) {
                      messageString = @"Error deleting events";
                  }
                  else {
                      messageString = error.userInfo.description;
                  }
                      pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR
                                                                    messageAsString:messageString];
      
              }
              else {
                      pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
              }
              [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
          }
      
      }
      
      -(void)findEvent:(CDVInvokedUrlCommand*)command {
          // Import arguments
      
          NSArray *arguments = command.arguments;
      
          NSString* title      = [arguments objectAtIndex:0];
          NSString* location   = [arguments objectAtIndex:1];
          NSString* message    = [arguments objectAtIndex:2];
          NSString *startDate  = [arguments objectAtIndex:3];
          NSString *endDate    = [arguments objectAtIndex:4];
      
          NSDateFormatter *df = [[NSDateFormatter alloc] init];
          [df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
          NSDate *myStartDate = [df dateFromString:startDate];
          NSDate *myEndDate = [df dateFromString:endDate];
      
          NSArray *matchingEvents = [self findEKEventsWithTitle:title location:location message:message startDate:myStartDate endDate:myEndDate];
      
          NSMutableArray *finalResults = [[NSMutableArray alloc] initWithCapacity:matchingEvents.count];
      
      
          // Stringify the results - Cordova can't deal with Obj-C objects
          for (EKEvent * event in matchingEvents) {
              NSMutableDictionary *entry = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
                                            event.title, @"title",
                                            event.location, @"location",
                                            event.notes, @"message",
                                            [df stringFromDate:event.startDate], @"startDate",
                                            [df stringFromDate:event.endDate], @"endDate", nil];
              [finalResults addObject:entry];
          }
          CDVPluginResult *pluginResult = nil;
          if (finalResults.count > 0) {
              // Return the results we got
              pluginResult = [CDVPluginResult
                                         resultWithStatus: CDVCommandStatus_OK
                                         messageAsArray:finalResults
                                         ];
              [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
          }
          else {
              pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_NO_RESULT];
          }
          [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
      
      }
      
      
      -(void)modifyEvent:(CDVInvokedUrlCommand*)command {
          // Import arguments
      
          NSArray *arguments = command.arguments;
      
          NSString* title      = [arguments objectAtIndex:0];
          NSString* location   = [arguments objectAtIndex:1];
          NSString* message    = [arguments objectAtIndex:2];
          NSString *startDate  = [arguments objectAtIndex:3];
          NSString *endDate    = [arguments objectAtIndex:4];
      
          NSString* ntitle      = [arguments objectAtIndex:5];
          NSString* nlocation   = [arguments objectAtIndex:6];
          NSString* nmessage    = [arguments objectAtIndex:7];
          NSString *nstartDate  = [arguments objectAtIndex:8];
          NSString *nendDate    = [arguments objectAtIndex:9];
      
          // Make NSDates from our strings
          NSDateFormatter *df = [[NSDateFormatter alloc] init];
          [df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
          NSDate *myStartDate = [df dateFromString:startDate];
          NSDate *myEndDate = [df dateFromString:endDate];
      
          // Find matches
          NSArray *matchingEvents = [self findEKEventsWithTitle:title location:location message:message startDate:myStartDate endDate:myEndDate];
      
          CDVPluginResult *pluginResult = nil;
      
          if (matchingEvents.count == 1) {
              // Presume we have to have an exact match to modify it!
              // Need to load this event from an EKEventStore so we can edit it
              EKEvent *theEvent = [self.eventStore eventWithIdentifier:((EKEvent*)[matchingEvents lastObject]).eventIdentifier];
              if (ntitle) {
                  theEvent.title = ntitle;
              }
              if (nlocation) {
                  theEvent.location = nlocation;
              }
              if (nmessage) {
                  theEvent.notes = nmessage;
              }
              if (nstartDate) {
                  NSDate *newMyStartDate = [df dateFromString:nstartDate];
                  theEvent.startDate = newMyStartDate;
              }
              if (nendDate) {
                  NSDate *newMyEndDate = [df dateFromString:nendDate];
                  theEvent.endDate = newMyEndDate;
              }
      
              // Now save the new details back to the store
              NSError *error = nil;
              [self.eventStore saveEvent:theEvent span:EKSpanThisEvent error:&error];
      
              // Check error code + return result
              if (error) {
                  pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR
                                                                     messageAsString:error.userInfo.description];
      
              }
              else {
                  pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
              }
          }
          else {
              // Otherwise return a no result error
              pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_NO_RESULT];
          }
          [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
      }
      @end
      

      CalendarPlugin.java

      package de.drid.calendarPlugin;
      
      import org.apache.cordova.CallbackContext;
      import org.apache.cordova.CordovaPlugin;
      import org.json.JSONObject;
      import org.json.JSONArray;
      import org.json.JSONException;
      import android.content.Intent;
      
      public class CalendarPlugin extends CordovaPlugin {
          public static final String ACTION_ADD_CALENDAR_ENTRY = "createEvent";
      
          @Override
          public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
              try {
                  if (ACTION_ADD_CALENDAR_ENTRY.equals(action)) { 
                      JSONObject arg_object = args.getJSONObject(5);
                      Intent calIntent = new Intent(Intent.ACTION_EDIT)
                          .setType("vnd.android.cursor.item/event")
                          .putExtra("beginTime", arg_object.getLong("startTimeMillis"))
                          .putExtra("endTime", arg_object.getLong("endTimeMillis"))
                          .putExtra("title", arg_object.getString("title"))
                          .putExtra("description", arg_object.getString("description"))
                          .putExtra("eventLocation", arg_object.getString("eventLocation"));
      
                         this.cordova.getActivity().startActivity(calIntent);
                         callbackContext.success();
                         return true;
                  }
                  callbackContext.error("Invalid action");
                  return false;
              } catch(Exception e) {
                  System.err.println("Exception: " + e.getMessage());
                  callbackContext.error(e.getMessage());
                  return false;
              } 
          }
      }
      

      【讨论】:

        【解决方案4】:

        Here is a PhoneGap Calendar Plugin for Android 适用于 3.0

        对于 PG 3.0,您需要修改 .java 类中的导入语句,因为它是从 2.9 更改的:

        从此……

        import org.apache.cordova.api.CallbackContext;
        import org.apache.cordova.api.CordovaPlugin;
        

        到这个……

        import org.apache.cordova.CallbackContext;
        import org.apache.cordova.CordovaPlugin;
        

        【讨论】:

        • 有什么可以跨平台工作的吗?我也需要它来支持 iOS。
        • @NathanStanfordII 我没有找到一个支持这两个平台并且具有与本机环境(Java/Objective-C)接口的相同 JavaScript API 的插件。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-08-11
        • 1970-01-01
        • 1970-01-01
        • 2017-04-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多