如何将 UIWebView 添加到我的 iOS 应用程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5408746/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 19:20:25  来源:igfitidea点击:

How do I add a UIWebView to my iOS app?

objective-cwebviewios

提问by cdietschrun

I have an app at the moment that when a button is pushed on the first screen does some work and makes a URL, and then does

我现在有一个应用程序,当在第一个屏幕上按下一个按钮时,它会做一些工作并创建一个 URL,然后

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:currentURL]];

which launches Safari with my URL. I want instead to have a webview launch from here so the user can do some custom things with it. I am still having a hell of a time understanding MVC in iOS and need help. My project is minimal and consists of an AppDelegate.h/m and a ViewController.h/m, the.m of the view controller is where the function that does this Safari launch lives.

它使用我的 URL 启动 Safari。我想从这里启动一个 webview,这样用户就可以用它做一些自定义的事情。我仍然很难理解 iOS 中的 MVC 并需要帮助。我的项目很小,由一个 AppDelegate.h/m 和一个 ViewController.h/m 组成,视图控制器的 .m 是执行此 Safari 启动的函数所在的位置。

Can anyone help me understand how to do what I'm trying to d?

任何人都可以帮助我了解如何做我想做的事吗?

Thanks...

谢谢...

回答by Ned

The simplest way is just to add a UIWebView when the button gets pressed. Add this method to your ViewController.m and have this be performed when the button gets pressed.

最简单的方法是在按钮被按下时添加一个 UIWebView。将此方法添加到您的 ViewController.m 并在按钮被按下时执行此操作。

Programmatically:

以编程方式:

//This method should get called when you want to add and load the web view
- (void)loadUIWebView
{
    UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.bounds];  //Change self.view.bounds to a smaller CGRect if you don't want it to take up the whole screen
    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:currentURL]]];
    [self.view addSubview:webView];
    [webView release];
}

Using Interface Builder:

使用界面生成器:

1) Add a UIWebView object to your interface.

1) 将 UIWebView 对象添加到您的界面。

2) Set the "hidden" property to checked (in the "Attributes Inspector" window in Interface Builder). You'll keep it hidden until you want to show it.

2)将“隐藏”属性设置为选中(在界面生成器的“属性检查器”窗口中)。您会一直隐藏它,直到您想显示它为止。

3) Add the following code to your ViewController.h, below the other @property lines:

3) 将以下代码添加到您的 ViewController.h 中,在其他 @property 行下方:

@property (nonatomic, retain) IBOutlet UIWebView *webView;

4) Add the following line below the @synthesize in your ViewController.m

4) 在 ViewController.m 中的 @synthesize 下面添加以下行

@synthesize webView;

And add [webView release];in the dealloc method.

并添加[webView release];dealloc方法。

5) Go back into IB and click on File's Owner, and connect the webView outlet to the webView you created.

5) 返回 IB 并单击 File's Owner,并将 webView 插座连接到您创建的 webView。

6) Add the following method instead of the method I showed above (for the programmatic example):

6)添加以下方法而不是我上面显示的方法(对于程序示例):

//This method should get called when you want to add and load the web view
- (void)loadUIWebView
{
    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:currentURL]]];
    self.webView.hidden = NO; 
}

You could set up an IBAction method to respond to the button press, but it sounds like you already have the button press working, so I wouldn't bother with that now.

您可以设置一个 IBAction 方法来响应按钮按下,但听起来您已经使按钮按下工作,所以我现在不会打扰。

As far as adding a button above the web view, you can either subclass web view and add the button there, or just have a separate button you define in your nib or programmatically and have that hide the webView to "get rid of it".

至于在 web 视图上方添加一个按钮,您可以子类化 web 视图并在那里添加按钮,或者只在您的笔尖或以编程方式定义一个单独的按钮,并隐藏 webView 以“摆脱它”。

回答by Shaba Aafreen

For adding UIWebView using Swift to your app just drag the WebView to your story board and then using an assistant editor connect WebView to ViewController.swift And then Loading URL to WebView is very easy. Just create a WebView in your storyboard and then you can use the following code to load url.

要使用 Swift 将 UIWebView 添加到您的应用程序,只需将 WebView 拖到您的故事板,然后使用助手编辑器将 WebView 连接到 ViewController.swift 然后将 URL 加载到 WebView 非常简单。只需在您的故事板中创建一个 WebView,然后您就可以使用以下代码加载 url。

    let url = NSURL (string: "https://www.simplifiedios.net");
    let request = NSURLRequest(URL: url!);
    webView.loadRequest(request);

As simple as that only 3 lines of codes :)

就这么简单,只有 3 行代码:)

Ref: UIWebView Example

参考:UIWebView 示例

回答by Alan Zeino

Since this is the top result on Google, if you can use WKWebViewinstead of UIWebView, you should.

由于这是 Google 上的最佳结果,如果您可以使用WKWebView代替UIWebView,则应该使用。

import WebKit

let webViewConfiguration = WKWebViewConfiguration()
let webView = WKWebView(frame: CGRect(), configuration: webViewConfiguration)