Cristian
Cristian

Reputation: 7145

WebView Open '_blank' Links

I have a WebView which loads content from the Internet. If I have a link which uses the '_blank' attribute, nothing happens.

So far I have tried setting my WebView delegate but I can't get it to register, I can't even get an NSLog back so I must be using it wrong. The code is as follows:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    [tmpView setUIDelegate:self];
}

- (WebView *)webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request
{
    NSLog(@"HEY THERE");
    return sender;
}

Ideally what I'd like, is that whenever a link is clicked on a WebView, the link is always opened within that WebView, nowhere else.

Is this possible?

Through research I found other methods but have no idea how to use them to get the result I desire:

webView:decidePolicyForNewWindowAction:request:newFrameName:decisionListener:

I'm completely stumped so I'd really appreciate your help.

Thanks in advance everyone.

Upvotes: 1

Views: 2451

Answers (3)

user1670927
user1670927

Reputation: 1

you can call

(WebView *)webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request

it did work.

you can also implement

(void)webView:(WebView *)sender decidePolicyForNewWindowAction:(NSDictionary *)actionInformation request:(NSURLRequest *)request newFrameName:(NSString *)frameName decisionListener:(id<WebPolicyDecisionListener>)listener

to do what you want to achieve.

Upvotes: 0

Rob Keniger
Rob Keniger

Reputation: 46020

If you don't want to open a new window but just open the request in your current WebView object, you just need to do this in your delegate:

- (WebView *)webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request
{
    [[sender mainFrame] loadRequest:request];
    return sender;
}

Upvotes: 3

Peter Willsey
Peter Willsey

Reputation: 1098

This blog post provides a solution using JavaScript to basically remove the target attribute from links.

http://blog.mikeweller.com/2009/06/uiwebview-doesnt-open-targetblank-links.html

Upvotes: 1

Related Questions