Xcode 9 模拟器中的特定 URL 未加载 WebView
WebView is not loading for specific URL in Xcode 9 simulator
我正在尝试使用 swift 中的 webView 加载 URL 4。我尝试了 https://www.google.co.in,它工作正常。并且特定 URL 的移动站点与 android 配合良好。
但是当我在模拟器中尝试这个时,它会永远加载。
我的代码如下:
import UIKit
class WebViewController: UIViewController, UIWebViewDelegate {
@IBOutlet weak var webView: UIWebView!
let activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.whiteLarge)
override func viewDidLoad() {
super.viewDidLoad()
webView.delegate = self
if let url = URL(string: "https://www.myurl.com") {
let request = URLRequest(url: url)
webView.loadRequest(request)
}
}
public func webViewDidStartLoad(_ webView: UIWebView)
{
activityIndicator.center = CGPoint(x: self.view.bounds.size.width/2, y: self.view.bounds.size.height/2)
self.view.addSubview(activityIndicator)
activityIndicator.color = UIColor.red
self.activityIndicator.startAnimating()
}
public func webViewDidFinishLoad(_ webView: UIWebView)
{
self.activityIndicator.stopAnimating()
}
}
我认为我的代码没有错。但我想知道我是否可以做任何事情让它在模拟器上运行。
谢谢
我刚刚用你的代码做了一个小项目,在模拟器中没有问题,https://www.google.co.in 加载得很好。
我注意到您提到您正在使用 Swift 4,我认为您应该考虑使用 WebKit View (WKWebView
),因为 UIWebView
已被弃用。在 Apple's documentation 你可以看到如何实现它,它非常简单。
如果您的证书不受信任,您必须将 Info.plist App Transport Security Settings
、Allow Arbitrary Loads
添加到 YES。 (不推荐这样做)。
代码很简单,试试看:
import UIKit
import WebKit
class WebViewController: UIViewController, WKUIDelegate, WKNavigationDelegate {
@IBOutlet weak var webView: WKWebView!
override func loadView() {
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self
webView.navigationDelegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let myURL = URL(string: "https://www.myurl.com")
let myRequest = URLRequest(url: myURL!)
webView.load(myRequest)
}
func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
if let serverTrust = challenge.protectionSpace.serverTrust {
completionHandler(.useCredential, URLCredential(trust: serverTrust))
}
}
}
我正在尝试使用 swift 中的 webView 加载 URL 4。我尝试了 https://www.google.co.in,它工作正常。并且特定 URL 的移动站点与 android 配合良好。 但是当我在模拟器中尝试这个时,它会永远加载。
我的代码如下:
import UIKit
class WebViewController: UIViewController, UIWebViewDelegate {
@IBOutlet weak var webView: UIWebView!
let activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.whiteLarge)
override func viewDidLoad() {
super.viewDidLoad()
webView.delegate = self
if let url = URL(string: "https://www.myurl.com") {
let request = URLRequest(url: url)
webView.loadRequest(request)
}
}
public func webViewDidStartLoad(_ webView: UIWebView)
{
activityIndicator.center = CGPoint(x: self.view.bounds.size.width/2, y: self.view.bounds.size.height/2)
self.view.addSubview(activityIndicator)
activityIndicator.color = UIColor.red
self.activityIndicator.startAnimating()
}
public func webViewDidFinishLoad(_ webView: UIWebView)
{
self.activityIndicator.stopAnimating()
}
}
我认为我的代码没有错。但我想知道我是否可以做任何事情让它在模拟器上运行。
谢谢
我刚刚用你的代码做了一个小项目,在模拟器中没有问题,https://www.google.co.in 加载得很好。
我注意到您提到您正在使用 Swift 4,我认为您应该考虑使用 WebKit View (WKWebView
),因为 UIWebView
已被弃用。在 Apple's documentation 你可以看到如何实现它,它非常简单。
如果您的证书不受信任,您必须将 Info.plist App Transport Security Settings
、Allow Arbitrary Loads
添加到 YES。 (不推荐这样做)。
代码很简单,试试看:
import UIKit
import WebKit
class WebViewController: UIViewController, WKUIDelegate, WKNavigationDelegate {
@IBOutlet weak var webView: WKWebView!
override func loadView() {
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self
webView.navigationDelegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let myURL = URL(string: "https://www.myurl.com")
let myRequest = URLRequest(url: myURL!)
webView.load(myRequest)
}
func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
if let serverTrust = challenge.protectionSpace.serverTrust {
completionHandler(.useCredential, URLCredential(trust: serverTrust))
}
}
}