How to use WebView on xcode 4.5.1

I am new to programming and I need to embed a website in my application (this is empty, I only want to embed the website right now). I searched for it from 5:00 p.m. (now 9:30 p.m.), and so far I have not found anything about it.

What code do I need and in which file do I need to write it? What do I need to tie together?

I am using Xcode 4.5.1 and I am trying to make a Cocoa application for Mac OS X (not for iOS).

I apologize if some of my suggestions are not clear, but English is not my main language.

If you need more information to help me, just ask.

+7
source share
2 answers

In the AppDelegate.h file, add this line below the line #import <Cocoa/Cocoa.h> :

 #import <WebKit/WebKit.h> 

and add this line below the line @property (assign) IBOutlet NSWindow *window; :

 @property (assign) IBOutlet WebView *webView; 

Select the MainMenu.xib file.

Open the window inside it, then drag the WebView from the object library browser into the window. Align and size it.

There should be an icon representing your AppController object to the left of your user interface layout. Control the drag and drop from it to the WebView inside your window. (Do not drag the File Owner icon!) Release the mouse button. A context menu WebView containing the word WebView . Select it.

Add the WebKit.framework framework to your project. Right-click the Frameworks folder in the resource list on the left side of the Xcode window. Select "Add files to" <your project name> "... and select the structure using this path: /System/Library/Frameworks/WebKit.framework .

Select the AppDelegate.m file.

In your -applicationDidFinishLaunching: method, replace the comment with the following code:

 // I provided Apple URL, but this is where you provide your own instead. NSURL *url = [NSURL URLWithString:@"http://www.apple.com"]; NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url]; [[[self webView] mainFrame] loadRequest:urlRequest]; 

Create and run. When the window appears, you will see that it loads the web page specified in the URL.

A few final words:

I see that you are new here. What I just did, in the context of Stack Overflow, is to give you a present. You need to try looking a bit hard on the Internet. I found two myself, but since they are a bit old (and the development tools look different), I proceeded to this answer. I want you to promise that you will work harder to find the answers yourself. A great place to start is to read Apple's excellent documentation .

+17
source

You have found the Apple manual on this very issue :

 WebView *webview = [[WebView alloc] init]; // or initialise using the modern-equivalent of InterfaceBuilder [[webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlText]]]; 
+4
source

All Articles