如何link UIWebView 到网站
How to link UIWebView to website
这里是非常基本的问题:我的视图控制器中有一个 Web 视图,但不知道如何让它显示网站。 None 我找到的解决方案对我有用。我在网上看到的大部分内容都告诉我输入下面的代码(示例 url),但我收到错误消息,告诉我删除“@”并添加几个“;”但这样做并不能解决问题。
- (void)viewDidLoad {
[super viewDidLoad];
NSString *fullURL = @"http://conecode.com";
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_viewWeb loadRequest:requestObj];
我不确定为什么我在处理看起来如此简单的事情时遇到这么多困难...这就是我的代码中的全部内容。如何让网页视图显示网站?提前致谢!我已经从这个社区学到了很多东西。
import UIKit
class SecondViewController: UIViewController {
@IBOutlet var webView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
经过更多挖掘后,我发现这似乎有效:
var url = NSURL(string: "https://www.google.com")
var request = NSURLRequest(URL: url!)
webView.loadRequest(request)
我不确定这与我之前找到的代码片段有什么关系,而且它不适用于 http(与 https)网站,所以我仍然需要弄清楚这一点...
下面是如何将页面加载到 UIWebView。
@IBOutlet var webView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
var url = NSURL(string: "https://www.google.com")
var request = NSURLRequest(URL: url!)
webView.loadRequest(request)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
关于您的 info.plist
的 http NSAppTransportSecurity 密钥问题
示例如下:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
<key>NSExceptionDomains</key>
<dict>
<key>google.com</key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSTemporaryExceptionMinimumTLSVersion</key>
<string>TLSv1.1</string>
</dict>
</dict>
</dict>
NSAllowsArbitraryLoads - 它允许连接到任何 http 站点。
或者您可以使用 NSExceptionDomains 指定站点 - 就像我在上面的代码中 post 一样。
更多的东西 - 检查 WKWebView - 它比显示页面的 UIWebView 好得多。 - https://developer.apple.com/library/ios/documentation/WebKit/Reference/WKWebView_Ref/
这里是非常基本的问题:我的视图控制器中有一个 Web 视图,但不知道如何让它显示网站。 None 我找到的解决方案对我有用。我在网上看到的大部分内容都告诉我输入下面的代码(示例 url),但我收到错误消息,告诉我删除“@”并添加几个“;”但这样做并不能解决问题。
- (void)viewDidLoad {
[super viewDidLoad];
NSString *fullURL = @"http://conecode.com";
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_viewWeb loadRequest:requestObj];
我不确定为什么我在处理看起来如此简单的事情时遇到这么多困难...这就是我的代码中的全部内容。如何让网页视图显示网站?提前致谢!我已经从这个社区学到了很多东西。
import UIKit
class SecondViewController: UIViewController {
@IBOutlet var webView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
经过更多挖掘后,我发现这似乎有效:
var url = NSURL(string: "https://www.google.com")
var request = NSURLRequest(URL: url!)
webView.loadRequest(request)
我不确定这与我之前找到的代码片段有什么关系,而且它不适用于 http(与 https)网站,所以我仍然需要弄清楚这一点...
下面是如何将页面加载到 UIWebView。
@IBOutlet var webView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
var url = NSURL(string: "https://www.google.com")
var request = NSURLRequest(URL: url!)
webView.loadRequest(request)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
关于您的 info.plist
的 http NSAppTransportSecurity 密钥问题示例如下:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
<key>NSExceptionDomains</key>
<dict>
<key>google.com</key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSTemporaryExceptionMinimumTLSVersion</key>
<string>TLSv1.1</string>
</dict>
</dict>
</dict>
NSAllowsArbitraryLoads - 它允许连接到任何 http 站点。
或者您可以使用 NSExceptionDomains 指定站点 - 就像我在上面的代码中 post 一样。 更多的东西 - 检查 WKWebView - 它比显示页面的 UIWebView 好得多。 - https://developer.apple.com/library/ios/documentation/WebKit/Reference/WKWebView_Ref/