开发者

iOS - UIWebView - Need Submitted form to Open in Safari

I have been developing an HTML5 / Javascript webapp for the iOS platform. I am relatively new to Objective and XCode, but I know my way around the program. I am attempting to have the UIWebView open a form, once submitted (submit button) in Safari as opposed to opening it in the current UIWebView.

The code below is suggested for opening standard <a> tags in Safari:

-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
if ( inType == UIWebViewNavigationTypeLinkClicked ) {
    [[UIApplication sharedApplication] openURL:[inRequest URL]];
    return NO;
}

return YES;

}

( original post here )

I have tried adapting this code specifically for submitted forms like so:

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

if ( inType == UIWebViewNavigationTypeFormSubmitted ) {
    [[UIApplication sharedApplication] openURL:[request URL]];
    return YES;
}
if ( inType == UIWebViewNavigationTypeFormResubmitted ) {
    [[UIAp开发者_开发百科plication sharedApplication] openURL:[request URL]];
    return YES;
}

return YES;

}

And FYI I have switched the return values of each IF statement to YES / NO, however this doesn't seem to be the issue... Any thoughts? Code? Theory? Thank You!


The following achieved the "Open in Safari" result I was looking for.

First I implemented UIWebViewDelegate protocol by adding webView.delegate = self; to my viewDidLoad declaration:

appViewController.m

- (void)viewDidLoad 
{
    [super viewDidLoad];
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];  
    NSData *htmlData = [NSData dataWithContentsOfFile:filePath];  
    if (htmlData) {  
        NSBundle *bundle = [NSBundle mainBundle]; 
        NSString *path = [bundle bundlePath];
        NSString *fullPath = [NSBundle pathForResource:@"index" ofType:@"html" inDirectory:path];
        [webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:fullPath]]];
        webView.delegate = self;
    }
}

Secondly I added the following:

-(BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request 
                                               navigationType:(UIWebViewNavigationType)navigationType 
{
    NSURL* url = [[request URL] retain];
    if (navigationType == UIWebViewNavigationTypeFormSubmitted || navigationType ==     UIWebViewNavigationTypeFormSubmitted)
    {
        return ![[UIApplication sharedApplication] openURL:url];
    }
    return YES;
}

One point of curiosity was that XCode returned the following warning flag:

warning: class 'appViewController' does not implement the 'UIWebViewDelegate' protocol

Not sure why... but! The app now opens the results of the submitted form in Safari.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜