views:

54

answers:

1

I want to present an HTML-based form in a UIWebView with a date field. I want the OS to recognize this as a date and show the UIDatePickerView for entering the date similar to the way it presents a UIPickerView when entering data in a form select field. Is this possible?

+1  A: 

here is a javascript only control that might work... I saved the link for my reference, but never used it

http://cubiq.org/spinning-wheel-on-webkit-for-iphone-ipod-touch

And below is some code I just typed up on how you might do it. I have not tested this code, it is only for informational purposes. Basically just check all the links that are clicked for the specific criteria you require, pop up the picker and get the results

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
{
    NSURL *loadURL = [[request URL] retain];
    BOOL dateLinkClicked = // determine this somehow specific to your code
    // Check navigationType. Else this alert will be visible on every load of UIWebView 
    if (  navigationType == UIWebViewNavigationTypeLinkClicked  && dateLinkClicked )
    {
        // Initialization code
        datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 250, 325, 250)];
        datePicker.datePickerMode = UIDatePickerModeDate;
        datePicker.hidden = NO;
        datePicker.date = [NSDate date];
        [datePicker addTarget:self
               action:@selector(dateSelected:)
               forControlEvents:UIControlEventValueChanged];
        [self.view addSubview:datePicker];
        [datePicker release];
    } else {
        [loadURL release];
        return YES;
    }
    return NO;
}

Action Method for DataPicker

- (void)dateSelected:(id)sender{
    //Use NSDateFormatter to write out the date in a friendly format
    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    df.dateStyle = NSDateFormatterMediumStyle;

        // do something with date
    NSString * dateText = [NSString stringWithFormat:@"%@",
                 [df stringFromDate:datePicker.date]];
    [df release];
}
Aaron Saunders
Jon