Xcode 4.5 - OS X Cocoa Application - Basic Web View: Load Google when opened

让人想犯罪 __ 提交于 2019-12-04 12:59:23

After adding the WebView to your main window, you'll want to make sure you've added the WebKit.framework to the Linked Frameworks and Libraries for your project otherwise you'll get a linking error.

.h:

@class WebView;

@interface MDAppDelegate : NSObject <NSApplicationDelegate>

@property (weak) IBOutlet WebView *webView;
@property (assign) IBOutlet NSWindow *window;

@end

Assuming you've created an IBOutlet for the WebView named webView like in the code above, you can load a URL using the code below:

.m:

@implementation MDAppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    NSURLRequest *request = [NSURLRequest requestWithURL:
                 [NSURL URLWithString:@"https://www.google.com/"]];
    [self.webView.mainFrame loadRequest:request];
}

@end

Sample GitHub project: https://github.com/NSGod/WebViewFinagler

add self keyword to following line

[myWebView loadRequest:myRequest];

like this

[self.myWebView loadRequest:myRequest];

try following code,it work for me,just tested

1) create new project
2) select ViewController ,goto xcode menu
Editor->Embed in->Navigation controller
3)declare myWebView property in ViewController.h

in ViewController.m write following code

   self.myWebView = [[UIWebView alloc] initWithFrame:self.view.bounds]; 
   self.myWebView.scalesPageToFit = YES;
   [self.view addSubview:self.myWebView];
   NSURL *myURL = [NSURL URLWithString:@"http://www.google.com"];
   NSURLRequest *myRequest = [NSURLRequest requestWithURL:myURL];
   [self.myWebView loadRequest:myRequest];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!