开发者

how do i call this code from an html button and pass it parameters

OK I found this code here at stackoverflow It works nice. But how can I call it in a webview from a button on an html page that also has the event details (date time place)? Programmatically add custom event in the iP开发者_开发知识库hone Calendar

#import "EventTestViewController.h"
#import <EventKit/EventKit.h>

@implementation EventTestViewController

- (void)viewDidLoad {
[super viewDidLoad];

EKEventStore *eventStore = [[EKEventStore alloc] init];

EKEvent *event  = [EKEvent eventWithEventStore:eventStore];
event.title     = @"EVENT TITLE";

event.startDate = [[NSDate alloc] init];
event.endDate   = [[NSDate alloc] initWithTimeInterval:600 sinceDate:event.startDate];

[event setCalendar:[eventStore defaultCalendarForNewEvents]];
NSError *err;
[eventStore saveEvent:event span:EKSpanThisEvent error:&err];       
}



- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}

@end


You could probably accomplish this by implementing this UIWebViewDelegate method:

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

Have your button trigger a POST request containing any form data you want to get out of your web view, extract it from the NSURLRequest object you get back, execute your code, and return NO.

Let's say your button is used to submit a form (which it seems like it is). You will need to catch the request your form sends in the delegate method I mentioned, and parse the return value of NSURLRequest's -HTTPBody method, which will be a string containing the form data from the web view.

The best way to see what's going on here is to implement the method and examine the request object in the debugger. This should get you started:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSLog(@"%@", [request HTTPBody]);
    return NO;
}

Take a look at the output here, and see what the request body looks like. With a bit of searching you can probably find some code that can make parsing this string more straightforward.

Another route to take, if you'd rather not get the information from the NSURLRequest, would be to execute some javascript in the web view using -stringByEvaluatingJavaScriptFromString:. If you go that route, you'll only need to know when the button is pressed, and then you can go in and extract the values using JavaScript.

One other thing to note is that you will probably want to add a check in your delegate method to make sure you're intercepting the right request. One way to do this is by looking at the request's -URL.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜