Multiple Windows
If you are building a simple browser application, you’ll want to allow the user to open multiple windows and type in URLs. You also need to make some policy decisions about how to handle new window requests.
Opening Windows
You can implement multiple windows in a WebKit application easily by beginning with a Cocoa document-based architecture as follows:
Using Xcode, create a document-based Cocoa application. Your new project file will already contain the needed classes and interface files to support multiple windows (namely
MyDocument.h
,MyDocument.m
, andMyDocument.nib
).Add the WebKit frameworks to your project.
Open
MyDocument.nib
using Interface Builder and drag a WebView from the Cocoa—GraphicsViews palette to your document window.Create a
webView
outlet inMyDocument.h
and read the file into Interface Builder. Connect thewebView
outlet of the File’s Owner to the WebView object you created in the previous step.Add code to
MyDocument.m
to load a default page. You can add this code fragment to thewindowControllerDidLoadNib:
method:NSString *urlText = [NSString stringWithString:@"http://www.apple.com"];
[[webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlText]]];
Build and run your application.
When you run your application you should see a window open displaying web content. You can also open multiple windows by selecting New from the File menu. This example demonstrates multiple WebView objects independently displaying web content.
Entering URLs
If you want a user to type in her own URL, add a text field to the window and make a new load request every time the user enters a new URL. Here are the steps you follow:
Add a text field to the window in
MyDocument.nib
.Add a
textField
outlet and aconnectURL:
action toMyDocument.h
. Read in the changes toMyDocument.h
from Interface Builder.In Interface Builder, make a connection from the text field you created to the
textField
outlet of the File’s Owner and set the action toconnectURL:
. This method is invoked when the user enters in a new URL.Implement the
connectURL:
method to load the URL typed in by the user:- (IBAction)connectURL:(id)sender{
[[webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[sender stringValue]]]];
}
Build and run your application.
Now when you enter a new URL in the text field, a new page is loaded.
Handling New Window Requests
By default, if you click a link that requests a new window be opened to display the content of that link, nothing happens. If you want to change this behavior, your application needs to make a policy decision about new window requests. You need to implement a WebUIDelegate
object to handle this case. Again, implement the delegate methods you want. Here are the steps to follow:
Set the delegate after
MyDocument.nib
is loaded. If you have a Cocoa document-based application, then you can add this code to MyDocument’swindowControllerDidLoadNib:
method:[webView setUIDelegate:self];
It is good practice for browser-like applications to set the group name of WebView objects after they are loaded from a nib file. Otherwise, clicking on some links may result in multiple new window requests because the HTML code for a link might not use the same frame name. The group name is an arbitrary identifier used to group related frames. For example, you can set the group name as follows:
[webView setGroupName:@"MyDocument"];
Implement the
webView:createWebViewWithRequest:
delegate method to create a window containing a web view, and load the request:- (WebView *)webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request
{
id myDocument = [[NSDocumentController sharedDocumentController] openUntitledDocumentAndDisplay:YES error:nil];
[[[myDocument webView] mainFrame] loadRequest:request];
return [myDocument webView];
}
Build and run your application.
Now, when you click a link that requests a new window, a new window is created to display the content of that link. This is just one example of many decisions your application can make about policies and the behavior of the user interface. You use the other WebUIDelegate methods and other WebView delegates to modify the behavior.
Copyright © 2003, 2012 Apple Inc. All Rights Reserved. Terms of Use | Privacy Policy | Updated: 2012-11-09