Ramesh
Ramesh

Reputation: 177

How to Load Only .html page in WebView and All www Page load in Safari in iOS?

i am newbie in iOS Development I load my HTML Data Into WebView But Some time it Contain only href link as .html link and some time website link like as www.google.co.in so i want to load only html data in to Webview and any website are load in to Safari for that i write a code like as

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSString *link = [[request URL] relativeString];
if ([link isEqualToString:@"module1learningobjectives.html"])
{
    return NO;
}
else
{
    [[UIApplication sharedApplication] openURL:[request URL]];
    return YES;
}
return YES;
}

then it load .html file in web view but site are open in safari and Webview both i want only site was open in safari please give me solution for that.

Upvotes: 0

Views: 637

Answers (3)

mahboudz
mahboudz

Reputation: 39376

I'd do something like this:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSLog("URL is %@, and has an extension of %@", request.URL, [request.URL pathExtension]);

    if ([[request.URL pathExtension] isEqualToString:@".html"])

        return YES;
    return NO;
}

Is this what you are asking?

If you want it to use Safari for non files and your own WebView for files, then try this:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    if ( ! ([request.URL isFileURL]) ) {
        [[UIApplication sharedApplication] openURL:[request URL]];
        return NO;
    }

    return YES;
}

Upvotes: 1

Pravin
Pravin

Reputation: 1

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
 NSURL *url = request.URL;
    NSString * temp = [NSString stringWithFormat:@"%@",url];
    if ([temp rangeOfString:@"www"].location != NSNotFound)
    {
        // show alert view for go to safari
        // i.e
        [[UIApplication sharedApplication] openURL:url];
    }
    else
    {
        // your regular html page pushed
    }

}

Upvotes: 0

Dipen Patel
Dipen Patel

Reputation: 933

Just modify your code to below code.

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    if (navigationType == UIWebViewNavigationTypeLinkClicked)
    {
        NSString *strLink=request.URL.absoluteString;
        if([strLink rangeOfString:@".html"].location!=NSNotFound)
        {
            [[UIApplication sharedApplication]openURL:[request URL]];
            return NO;
        }
        else
        {
            return TRUE;
        }
        return NO;
    }
    return YES;
}

Above code check if the link which is going to open has .html extension or not? and it works accordingly.

One more thing in your question you haven't mentioned that the href are link of local pages or external links.

Upvotes: 0

Related Questions