问题描述
我可以毫无问题地创建Safari View Controller:
I can create Safari View Controller without problem:
let svc = SFSafariViewController(URL: NSURL(string: remote_url)!, entersReaderIfAvailable: true)
self.presentViewController(svc, animated: true, completion: nil)
在向用户展示视图控制器之前,有什么方法可以预加载URL?
Is there any way I can preload the URL before I present the view controller to the user?
例如,我可以先在后台预加载URL(Web内容),然后在用户单击某些内容后,立即显示带有内容的Safari View Controller.用户会感觉页面加载更快或更快速.
For example, I can preload the URL (web content) in the background first, and after the user clicks on something, I can show the Safari View Controller with the content right away. The user will feel the page loading is faster or instant.
P.S.解决方法/黑客攻击也是可以接受的.例如,使用缓存或在后台启动视图控制器等.
P.S. Workarounds/hacks are also acceptable. For example, using cache or starting the view controller in background, etc.
请仅考虑SFSafariViewController.
please consider SFSafariViewController only.
推荐答案
这是一个解决方案.显然,如果立即单击该按钮,您将看到负载.但是基本上,我加载浏览器并将视图放在另一个视图的后面,然后在另一个视图中放置一个按钮.
Here is a solution.Obviously, if you click on the button right away you'll see the loading.But basically, I load the Browser and put the view behind another one and I put a button in this other view.
当您按下按钮时,浏览器将显示在最前面,并且已经加载.这里唯一的问题是我没有使用任何过渡,但是至少这是一个解决方案.
When you press the button, the browser is bring to the front, already loaded.The only problem here is that I'm not using any transition but that's one solution at least.
import UIKit
import SafariServices
class ViewController: UIViewController {
var svc = SFSafariViewController(URL: NSURL(string: "https://microsoft.com/")!, entersReaderIfAvailable: true)
var safariView:UIView?
let containerView = UIView()
let btn = UIButton()
override func viewDidLoad() {
super.viewDidLoad()
//let tmpView = svc.view
addChildViewController(svc)
svc.didMoveToParentViewController(self)
svc.view.frame = view.frame
containerView.frame = view.frame
containerView.backgroundColor = UIColor.redColor()
safariView = svc.view
view.addSubview(safariView!)
view.addSubview(containerView)
btn.setTitle("Webizer", forState: UIControlState.Normal)
btn.titleLabel!.textColor = UIColor.blackColor()
btn.addTarget(self, action: "buttonTouched:", forControlEvents: .TouchUpInside)
btn.frame = CGRectMake(20, 50, 100, 100)
containerView.addSubview(btn)
view.sendSubviewToBack(safariView!)
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func buttonTouched(sender: AnyObject) {
view.bringSubviewToFront(safariView!)
//self.presentViewController(svc, animated: true, completion: nil)
}
}
这篇关于我可以预加载Safari View Controller的Web内容吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!