如何让 WebView OSX Xcode 项目在启动时加载 URL?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4777490/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make WebView OSX Xcode project load a URL on launch?
提问by 0x60
Okay, well I am trying to learn how to develop mac apps. I have done making a web browser and all, but I really want to know how to make the WebView load a URL (lets say http://google.com/) when the apps start. How do I do that?
好的,我正在努力学习如何开发 mac 应用程序。我已经完成了一个网络浏览器的制作,但我真的很想知道如何在应用程序启动时让 WebView 加载一个 URL(比如http://google.com/)。我怎么做?
回答by Yuji
In your app delegate, implement
在您的应用委托中,实现
-(void)applicationDidFinishLaunching:(NSNotification*) notification
{
....
}
This method is called automaticallyby the Cocoa system when the app did finish launching, quite obvious, right?
这个方法是在应用启动完成后 Cocoa 系统自动调用的,很明显吧?
Suppose you have IBOutlet WebView*webview
in your app delegate. Then all you have to do is, inside applicationDidFinishLaunching:
, to call
假设您IBOutlet WebView*webview
的应用程序委托中有。然后你所要做的就是在里面applicationDidFinishLaunching:
调用
NSURL*url=[NSURL URLWithString:@"http://www.google.com"];
NSURLRequest*request=[NSURLRequest requestWithURL:url];
[[webview mainFrame] loadRequest:request];
That's all!
就这样!
回答by Anonymous
You could just put
你可以把
[webview setMainFrameURL:@"http://www.google.com/"];
回答by Evsq2
Both of the answers should work but if your making it for mac with Xcode 4,(im not sure about the older versions). Three things, one you have to declare an IBOutlet for your webview called webview (because that is what it is in the two examples) and you have to synthesize it in the .m file. Two instead of [[webview mainFrame] loadRequest:request] it has to be [[_webview mainFrame] loadRequest:request] (because thats what its synthesized as). And last of all you need to add the webKit framework, and import it in the header file with #import
这两个答案都应该有效,但是如果您使用 Xcode 4 为 mac 制作它,(我不确定旧版本)。三件事,一件事您必须为名为 webview 的 webview 声明一个 IBOutlet(因为这就是两个示例中的内容),并且您必须在 .m 文件中合成它。两个而不是 [[webview mainFrame] loadRequest:request] 它必须是 [[_webview mainFrame] loadRequest:request] (因为那是它合成的)。最后,您需要添加 webKit 框架,并使用 #import 将其导入到头文件中
-Thats all you need to do to get it to work on mac with Xcode 4
- 这就是使用 Xcode 4 让它在 mac 上运行所需要做的所有事情