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...
谢谢……
3 个解决方案
#1
27
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。将这个方法添加到视图控制器中。当按下按钮时,请执行此操作。
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)向视图控制器添加以下代码。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释放);在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的所有者,并将webView outlet连接到创建的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视图的子类上添加按钮,也可以在nib中定义单独的按钮,或者通过编程方式隐藏webView以“删除它”。
#2
1
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向app添加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 :)
简单到只有三行代码:)
Ref: UIWebView Example
裁判:UIWebView例子
#3
0
Since this is the top result on Google, if you can use WKWebView
instead of UIWebView
, you should.
因为这是谷歌的最上面的结果,如果你可以使用WKWebView而不是UIWebView,你应该这么做。
import WebKit
let webViewConfiguration = WKWebViewConfiguration()
let webView = WKWebView(frame: CGRect(), configuration: webViewConfiguration)
#1
27
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。将这个方法添加到视图控制器中。当按下按钮时,请执行此操作。
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)向视图控制器添加以下代码。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释放);在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的所有者,并将webView outlet连接到创建的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视图的子类上添加按钮,也可以在nib中定义单独的按钮,或者通过编程方式隐藏webView以“删除它”。
#2
1
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向app添加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 :)
简单到只有三行代码:)
Ref: UIWebView Example
裁判:UIWebView例子
#3
0
Since this is the top result on Google, if you can use WKWebView
instead of UIWebView
, you should.
因为这是谷歌的最上面的结果,如果你可以使用WKWebView而不是UIWebView,你应该这么做。
import WebKit
let webViewConfiguration = WKWebViewConfiguration()
let webView = WKWebView(frame: CGRect(), configuration: webViewConfiguration)