开发者

How to programmatically set the URL of a WebView in an OSX Xcode project?

I've created a new Mac OSX Application in Xcode, included a WebView via Interface Builder and now I'm trying to programmatically tell it what URL to load. Here's how far I got:

  • Create the project
  • Include Webkit.framework
  • Include the WebView in Interface Builder

Then from there I've included what I think is necessary to get access to the WebView, including:

#import <WebKit/WebKit.h>
@synthesize webView;
// etc, I think this is all good

Where I fall over is getting access to the WebView itself and telling it what URL to load. I'm trying:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    NSString *urlAddress = @"http://www.example.com/";
    NSURL *url = [NSURL URLWithString:urlAddress];
    NSU开发者_高级运维RLRequest *requestObj = [NSURLRequest requestWithURL:url];
    [[webView mainFrame] loadRequest:requestObj];
}

But I think I'm supposed to connect some stuff in Interface Builder first?

My end goal is to create a Mac OSX Application that's essentially just a View-Based Application (which is simple to do using the iPhone SDK). From there I want to include a local htdocs folder with my own local .html files but the first step is to tell the WebView what URL to load :)

Thanks!


The code you've written above is correct, I just tested it then and successfully loaded a URL.

I'm not sure what you've declared in your header file but you should have something like this:

   //usual #import(s)
   #import <WebKit/WebKit.h>

   @interface myAppDelegate : NSObject <NSApplicationDelegate> {
       WebView *myWebView;
       //other instance variables
   }

   @property (retain, nonatomic) IBOutlet WebView *myWebView;
   //other properties and methods
   @end

As @pulazzo says, you also need to connect your code with the control defined in Interface Builder.
To do this, in Interface Builder, you need to find your application delegate class, right-click (or control-click) on it and find myWebView in the list of outlets. Drag from the circle on the same line as myWebView onto your web view. I don't think my explanation is much chop so you'll probably have a lot more luck reading the Interface Builder Quick Start Guide on the Apple developer site.

In your implementation file, you need to implement the getter and setter methods for your new property, myWebView. You (probably) don't need to do any customisation to this so you can just use @synthesize:

@implementation myAppDelegate
@synthesize window;
@synthesize myWebView;
//your function etc
@end


Did you declare your webView property as an IBOutlet and connect it to your WebView in Interface Builder?

If not, then webView isn't set and your code will just silently fail to do anything.


yes, you says right! he declared like this:

  IBOutlet WebView webView;

but it doesn't need necessary coding with @sinthesize, you can just use a awakeFromNib method in your appController implementation.

-(void)awakeFromNib{
    //etc etc
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜