【发布时间】:2016-09-05 08:06:48
【问题描述】:
我目前正在将网站包装在本机 tabBar 中,它使用 didCommitNavigation/didFinishNavigation 和 didStartProvisionalNavigation/didFailProvisionalNavigation 处理导航,以根据点击的链接更改标签.当涉及到网站域内的链接时,这工作得很好,但点击 Youtube 链接时,webview 似乎根本没有反应。
I thought it might have to do with my security settings, but it seems like it doesn't
这是我的主视图控制器,它是项目中唯一的视图控制器:
import UIKit
import WebKit
class MainViewController: UIViewController, UITabBarDelegate, UIScrollViewDelegate, WKNavigationDelegate {
@IBOutlet weak var noConnectionLabel: UILabel!
@IBOutlet weak var tabBar: UITabBar!
@IBOutlet weak var statusImage: UIImageView!
@IBOutlet weak var viewForWeb: UIView!
@IBOutlet weak var retryButton: UIButton!
@IBOutlet weak var loadingIndicator: UIActivityIndicatorView!
let statusColors: [UIColor] = UIColor.getStatusColors()
var currentIndex: Int = 0
var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
webView = WKWebView(frame: CGRectMake(0, 0, viewForWeb.frame.size.width, viewForWeb.frame.size.height))
viewForWeb.addSubview(webView)
webView.scrollView.delegate = self
webView.navigationDelegate = self
loadingIndicator.startAnimating()
tabBar.delegate = self
tabBar.barTintColor = UIColor.tabBarBackgroundColor()
tabBar.tintColor = UIColor.iconColor()
for item in tabBar.items! {
item.titlePositionAdjustment = UIOffset(horizontal: 0, vertical: -3)
item.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.iconColor()], forState:.Normal)
item.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.iconColor()], forState:.Selected)
}
tabBar.selectionIndicatorImage = UIImage(named: "news-tab")
tabBar.selectedItem = tabBar.items![0]
statusImage.backgroundColor = UIColor.newsColor()
webView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
webView.loadRequest(NSURLRequest(URL: startURLs[0]))
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func retryConnection(sender: AnyObject) {
webView.loadRequest(NSURLRequest(URL: failedNavigationURL))
noConnectionLabel.hidden = true
retryButton.hidden = true
loadingIndicator.hidden = false
}
func webView(webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
print(webView.URL!)
}
func webView(webView: WKWebView, didCommitNavigation navigation: WKNavigation!) {
print(webView.URL!)
for view in views {
if webView.URL!.absoluteString.lowercaseString.rangeOfString(view) != nil {
tabBar.selectionIndicatorImage = UIImage(named: "\((view as NSString).substringToIndex(view.characters.count - 1))-tab")!
if tabBar.selectedItem != tabBar.items![views.indexOf(view)!] {
currentIndex = views.indexOf(view)!
tabBar.selectedItem = tabBar.items![views.indexOf(view)!]
statusImage.backgroundColor = statusColors[tabBar.items!.indexOf(tabBar.selectedItem!)!]
viewForWeb.hidden = true
}
}
}
}
func webView(webView: WKWebView, didFinishNavigation navigation: WKNavigation!) {
loadingIndicator.stopAnimating()
statusImage.backgroundColor = statusColors[tabBar.items!.indexOf(tabBar.selectedItem!)!]
loadingIndicator.hidden = true
noConnectionLabel.hidden = true
retryButton.hidden = true
viewForWeb.hidden = false
}
func webView(webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: NSError) {
failedNavigationURL = NSURL(string: error.userInfo[NSURLErrorFailingURLStringErrorKey]! as! NSString as String)!
statusImage.backgroundColor = UIColor.clearColor()
viewForWeb.hidden = true
noConnectionLabel.hidden = false
retryButton.hidden = false
loadingIndicator.hidden = true
}
func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
let indexOfItem = tabBar.items!.indexOf(item)!
if indexOfItem != currentIndex {
tabBar.selectionIndicatorImage = UIImage(named: "\(item.title!.lowercaseString)-tab")
statusImage.backgroundColor = statusColors[indexOfItem]
webView.evaluateJavaScript("document.open();document.close()", completionHandler: nil)
webView.loadRequest(NSURLRequest(URL: startURLs[indexOfItem]))
currentIndex = indexOfItem
loadingIndicator.hidden = false
loadingIndicator.startAnimating()
viewForWeb.hidden = true
}
}
func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
return nil
}
func scrollViewDidScroll(scrollView: UIScrollView) {
if scrollView.contentOffset.x>0 || scrollView.contentOffset.x<0 {
scrollView.contentOffset.x = 0
}
}
为了匿名,我省略了一些数组,但我认为它仍然可读。澄清一下,print() 只打印网站域内的 URL,单击 Yotube-links 时不打印任何内容。
【问题讨论】:
-
我将您的代码放入我的 XCode 中,并且 Youtube 链接运行良好。您是否有任何其他提示可以更好地重现您的问题?