0

I have a UIViewController subclass which includes a UIWebView and implements the UIWebViewDelegate. What i want to do is make links pressed in the UIWebView to open in safari.
I've read past questions about this, but still can't make it work. Here is about what i've done:

In the - (void)viewDidLoad method of my class i use the following code:

[[self articleWebView] setDelegate: self];  
[articleWebView loadRequest:requestObj];

I don't want to display the whole html page that is loaded in the articleWebView object, so in the -(void)webViewDidFinishLoad:(UIWebView *)webView method i use this:
NSString *content = [articleWebView stringByEvaluatingJavaScriptFromString:@"document.getElementsByClassName('myDivId')[0].outerHTML;"];

Then i empty(release) the articleWebView and load the content:

[articleWebView release]; 
articleWebView= [[UIWebView alloc] initWithFrame:CGRectMake(0,0,320,380)];  
[articleWebView loadHTMLString:content baseURL:[NSURL URLWithString:@"http://www.mysite.gr/"]];  
self.view = articleWebView;

I tried to use the following, but it's not working

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

Any ideas what i am missing?
Thank you in advance.

EDIT: As i can see, the shouldStartLoadWithRequest does not get called, so i'm guessing there is something wrong with the delegate of my webView?

CdB
  • 4,738
  • 7
  • 46
  • 69

1 Answers1

1

I noticed that you are not setting the delegate of "articleWebView" after you release it.

Try using this instead:

[articleWebView release]; 

articleWebView= [[UIWebView alloc] initWithFrame:CGRectMake(0,0,320,380)];  

**articleWebView.delegate = self;** 

[articleWebView loadHTMLString:content baseURL:[NSURL 
URLWithString:@"http://www.mysite.gr/"]];  

self.view = articleWebView;
PengOne
  • 48,188
  • 17
  • 130
  • 149
Jimit
  • 652
  • 8
  • 12
  • thanx a lot for replying. I already tried it, but it's giving this problem: In the -(void)webViewDidStartLoad:(UIWebView *)webView method i have this:[activityIndicator startAnimating]; (nothing else, just this). In the -(void)webViewDidFinishLoad:(UIWebView *)webView method the first thing i do is [activityIndicator stopAnimating];[activityIndicator release]; When i add your solution i get this error and the app crashes: "[UIActivityIndicatorView startAnimating]: message sent to deallocated instance 0x4ef10c0". Do u have any clue why? It's driving me crazy all day today... – CdB May 05 '11 at 16:30
  • I would suggest you to release the activityIndicator in UIViewController's -(void)dealloc method and not in -(void)webViewDidFinishLoad:(UIWebView*)webView. You are getting this error because by the time articleWebView starts loading the second URL the 'ActivityIndicator' was already released when the first URL request was loaded. I would recommend that you release the activity indicator in -(void)dealloc method and use [activityIndicator setHidden:YES] or NO for showing and hiding the activity. – Jimit May 05 '11 at 17:40