Search code examples
objective-cswiftsfsafariviewcontroller

Can I preload the web content for 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)

Is there any way I can preload the URL before I present the view controller to the user?

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. Workarounds/hacks are also acceptable. For example, using cache or starting the view controller in background, etc.

EDIT: please consider SFSafariViewController only.


Solution

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