Reputation: 1333
webView which have hyperlinks to add the target = " _blank "
can not be opened.
I developing a webview-like application, but i have one problem. How to open link in new window in current application - not in safari?
Thanks for help.
@Rob Keniger The code is not running.why?
Upvotes: 2
Views: 1824
Reputation: 46020
You need to set an object as the UIDelegate
of your WebView
and in that object implement the webView:createWebViewWithRequest:
method.
In your implementation of that method, you need to open a new window containing a separate WebView
and then tell its mainFrame
to load the URLRequest
passed as a parameter to the method.
Update:
I've looked at your code. You need to assign an object as the web view's UIDelegate
, so add a [webView setUIDelegate:self]
line into applicationDidFinishLaunching:
.
A very simple example of how to implement the delegate would be:
- (WebView *)webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request
{
NSUInteger windowStyleMask = NSClosableWindowMask |
NSMiniaturizableWindowMask |
NSResizableWindowMask |
NSTitledWindowMask;
NSWindow* webWindow = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 800, 600) styleMask:windowStyleMask backing:NSBackingStoreBuffered defer:NO];
WebView* newWebView = [[WebView alloc] initWithFrame:[webWindow contentRectForFrameRect:webWindow.frame]];
[newWebView setAutoresizingMask:NSViewWidthSizable|NSViewHeightSizable];
[webWindow setContentView:newWebView];
[webWindow center];
[webWindow makeKeyAndOrderFront:self];
[[newWebView mainFrame] loadRequest:request];
return newWebView;
}
Upvotes: 5