0

我在 javascript 文件中有一个函数,该函数必须将数据(日期、开始时间和结束时间)传递给构建 ios 日历的函数。我能怎么做?

函数javascipt:

function AddItemToCalendarDevice(startdatetime,enddatetime,title){

if (android){
      AndroidFunction.AddCalendarItemDateTime(startdatetime,enddatetime,title);     
}
else if (ios){
    //How can I do?
}

}

ios功能:

 - (NSMutableArray *)fetchEvents
{
    NSDate *startDate = [NSDate date];

    //Create the end date components
   NSDateComponents *tomorrowDateComponents = [[NSDateComponents alloc] init];
   tomorrowDateComponents.day = 1;

   NSDate *endDate = [[NSCalendar currentCalendar]   
               dateByAddingComponents:tomorrowDateComponents
                                                                toDate:startDate
                                                               options:0];
// We will only search the default calendar for our events
NSArray *calendarArray = [NSArray arrayWithObject:self.defaultCalendar];

    // Create the predicate
NSPredicate *predicate = [self.eventStore predicateForEventsWithStartDate:startDate
                                                                  endDate:endDate
                                                                calendars:calendarArray];

// Fetch all events that match the predicate
NSMutableArray *events = [NSMutableArray arrayWithArray:[self.eventStore eventsMatchingPredicate:predicate]];
NSLog(@"l'array di eventi è %@", events);

return events;
 }
4

2 回答 2

0

您可以创建一个 javascript 函数,该函数将创建然后删除一个目标为“ios://YOUR_DATA_AS_AN_URL_ENCODED_STRING”的 iframe,并使用您的 UIWebViewDelegate

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType 

接听电话并完成您的工作

- (BOOL)webView:(UIWebView *)theWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{

    NSString *url = [request.URL absoluteString];

    if ([url hasPrefix:@"ios:"]) {
        NSArray *array = [url componentsSeparatedByString:@":"];
        NSLog(@"%@", array);
        // extract your data from the array            



        return NO;
    }
else {
    return YES;

}

javascript函数:

function jsToNative(nativeCmd) {
    var iframe = document.createElement("IFRAME");
    iframe.setAttribute("src", "ios:" + nativeCmd);
    document.documentElement.appendChild(iframe);
    iframe.parentNode.removeChild(iframe);
    iframe = null;
}
于 2013-08-08T10:04:50.663 回答
0

在您的 javascript AddItemToCalendarDevice 方法中,创建一个您可以在 Web 视图委托中解析的自定义 url:

function AddItemToCalendarDevice(startdatetime,enddatetime,title){

    if (android){
        AndroidFunction.AddCalendarItemDateTime(startdatetime,enddatetime,title);     
    }
    else if (ios){
        window.load.href = "file://yourappname/AddCalendarEntry?starttime=" + startdatetime + "&endtime=" + enddatetime + "&title=" + title;
    }

}

然后在您的 Web 视图委托中,实现 shouldStartLoadWithRequest:

-(BOOL)webView:(UIWebView *)webView 
shouldStartLoadWithRequest:(NSURLRequest *)request 
 navigationType:(UIWebViewNavigationType)navigationType 
{
    NSString *urlString = request.URL.absoluteString;

    if ([urlString hasPrefix:@"file://yourappname/AddCalendarEntry"])
    {
        // Parse out the query params
        NSDictionary *queryParams = [self parseQueryParams:[[request URL] query]];
        NSString *startTimeStr = [queryParams objectForKey:@"starttime"];
        NSString *endTimeStr = [queryParams objectForKey:@"endtime"];
        NSString *title = [queryParams objectForKey:@"title"];

        // Convert the start time and end time to NSDate
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"MM/dd/yyyy hh:mm a"  // Change this to match your date format
        NSDate *startTime = [dateFormatter dateFromString:startTimeStr];
        NSDate *endTime = [dateFormatter dateFromString:endTimeStr];
        [dateFormatter release];

        // Add the event to the calendar
        eventStore=[[EKEventStore alloc] init];
        EKEvent *addEvent=[EKEvent eventWithEventStore:eventStore];
        addEvent.title = eventTitle;
        addEvent.startDate = startDate;
        addEvent.endDate = endDate;
        [addEvent setCalendar:[eventStore defaultCalendarForNewEvents]];
        // uncomment if you want to set an alarm
        // addEvent.alarms=[NSArray arrayWithObject:[EKAlarm   alarmWithAbsoluteDate:addEvent.startDate]];
        [eventStore saveEvent:addEvent span:EKSpanThisEvent error:nil];
        [eventStore release];
    }
    return YES;            
}


-(NSDictionary *)parseQueryParams:(NSString *)queryString
{
    NSArray *params = [queryString componentsSeparatedByString:@"&"];
    NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
    for (NSString *param in params)
    {
        NSArray *pair = [param componentsSeparatedByString:@"="];
        NSString *key = [pair objectAtIndex:0];
        NSString *value = [pair objectAtIndex:1];
        [dictionary setObject:value forKey:key];
    }
    NSDictionary *queryParams = [NSDictionary dictionaryWithDictionary:dictionary];
    [dictionary release];
    return queryParams;
}
于 2013-08-08T15:10:11.597 回答