0

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 iPhone 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
Community
  • 1
  • 1
Rick
  • 1,153
  • 1
  • 18
  • 37

1 Answers1

0

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.

Cameron Spickert
  • 5,190
  • 1
  • 27
  • 34
  • 1) Thank you for having that much faith in me but can you point me in the direction of tutorial on the NSURLRequest extract thing. and 2) how will this work if there are several buttons calling the same code but sending different parameters? The buttons being on the html page. – Rick Aug 26 '11 at 20:36
  • Updated my answer with some additional advice. – Cameron Spickert Aug 26 '11 at 21:03
  • When using -stringByEvaluatingJavaScriptFromString: only one button can be used. So if there is more than one button on the page calling the same method but passing different values this will not work. Unless I am missing something. – Rick Aug 26 '11 at 22:25
  • I'm not sure what you mean. IF you use -stringByEvaluatingJavaScriptFromString:, it should be called from within the delegate method, on detecting that the button was pressed by checking the NSURLRequest. – Cameron Spickert Aug 26 '11 at 23:02
  • Right but if there are multiple buttons all calling this method but passing different parameters how does the NSURLRequest pull the right variables? – Rick Aug 28 '11 at 17:31