我可以为Safari View Controller预加载Web内容吗?

Joe*_*ang 19 objective-c swift sfsafariviewcontroller

我可以毫无问题地创建Safari View Controller:

let svc = SFSafariViewController(URL: NSURL(string: remote_url)!, entersReaderIfAvailable: true)
self.presentViewController(svc, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)

在将视图控制器呈现给用户之前,有什么方法可以预先加载URL吗?

例如,我可以先在后台预加载URL(Web内容),在用户点击某些内容后,我可以立即向Safari View Controller显示内容.用户将感觉页面加载更快或更快.

PS Workarounds/hacks也是可以接受的.例如,使用缓存或在后台启动视图控制器等.

编辑:请仅考虑SFSafariViewController.

Mik*_*ael 11

这是一个解决方案.显然,如果你马上点击按钮,你会看到装载.但基本上,我加载浏览器并将视图放在另一个后面,我在另一个视图中放了一个按钮.

当您按下按钮时,浏览器将被带到前面,已经加载.这里唯一的问题是我没有使用任何转换,但这至少是一个解决方案.

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)
  }


}
Run Code Online (Sandbox Code Playgroud)