nithin
nithin

Reputation: 2467

iPhone:UIWebview open hyperlink in safari

In my app,I used UIwebview where I need to hyperlink some phone numbers,email & URLs.I did the following:

 - (void)viewDidLoad
    {
        credits.delegate=self;
    }



-(void)loadScreen
{
scroll=[[UIScrollView alloc]init];
 credits=[[UIWebView alloc]init];

NSString *Address = @"www.***.com/php/getjson.php?method=";


    jmax = [NSURL URLWithString:Address];

    NSData *Data =[ NSData dataWithContentsOfURL:jmax];


    String = [[NSString alloc] initWithData:Data encoding:NSUTF8StringEncoding];

    NSString * creditsHtml = [NSString stringWithFormat:@" <font face=\"Arial\"><H3 align=\"center\"> Credits </H3> %@ <br>Phone%@</font>", String,phone];

    credits.userInteractionEnabled = YES;
    credits.frame=CGRectMake(10, (frame1.size.height + 564), 300, 100);
    credits.dataDetectorTypes=UIDataDetectorTypeAll;
    credits loadHTMLString:creditsHtml baseURL:nil];

[scroll addSubview:credits];
}



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


        NSString *urls=  jmaxString;

        NSURL *requestURL =[NSURL URLWithString: urls]; 
        if ( ( [ [ requestURL scheme ] isEqualToString: @"http" ] || [ [ requestURL scheme ] isEqualToString: @"https" ] || [ [ requestURL scheme ] isEqualToString: @"mailto" ] ) 
            && ( navigationType == UIWebViewNavigationTypeLinkClicked ) ) { 
            return ![ [ UIApplication sharedApplication ] openURL: requestURL ]; 
        } 

        return YES; 
    }

Edited shouldStartLoadWithRequest: method

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

    NSURL *requestURL =[request URL];

    if ( ( [ [ requestURL scheme ] isEqualToString: @"http" ] || [ [ requestURL scheme ] isEqualToString: @"https" ] || [ [ requestURL scheme ] isEqualToString: @"mailto" ] ) 
        && ( navigationType == UIWebViewNavigationTypeLinkClicked ) ) { 
        return ![ [ UIApplication sharedApplication ] openURL: requestURL ]; 
    } 
    NSLog(@"URL %@",requestURL);


    return YES; 
}

Got error in NSURL *requestURL =[request URL]; with Expected ; at the end & Extraneous ')' before ';'

Upvotes: 2

Views: 1167

Answers (3)

Piyush Kashyap
Piyush Kashyap

Reputation: 1965

just paste this line below where you create your webview credits in loadScreen method

credits=[[UIWebView alloc]init];
credits.delegate=self;   //Add this line

Upvotes: 1

Malek_Jundi
Malek_Jundi

Reputation: 6160

you are alloc/init a new object of UIWebView in loadScreen function without set its delegate because you are suppose you did that in viewDidLoad .. you have to set the delegate after you define the instance "credits".

Upvotes: 1

rickster
rickster

Reputation: 126107

This looks like a duplicate of this answered question.

If your -webView:shouldStartLoadWithRequest:navigationType: method isn't being called at all, there's a problem with hooking up your delegate -- we can't really diagnose it further without seeing more of your work. (Are you sure it's not being called? Stick an NSLog at the top of the method body or put a breakpoint there in the debugger if you're not.)

Also, either you've stripped too much in the interest of brevity when posting that method's body in your question, or there's no connection between the parameters passed in and your logic for determining which URLs to open in Safari. I presume you mean to test the link the user tapped? That's in the request parameter.

Upvotes: 0

Related Questions