1
import UIKit
import WebKit


// Add WKWebView in StoryBoard
class ViewController: UIViewController {

    @IBOutlet var webView: WebView!

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        webView.loadUrl(string: "https://url.to/my/template")
    }
}

class WebView: WKWebView {

    required init?(coder: NSCoder) {

        if let _view = UIView(coder: coder) {
            super.init(frame: _view.frame, configuration: WKWebViewConfiguration())
            autoresizingMask = _view.autoresizingMask
        } else {
            return nil
        }
    }

    func loadUrl(string: String) {
        if let url = URL(string: string) {
            load(URLRequest(url: url))
        }
    }

}

The website is a html5 + CSS3. Purchased a template from themeforest but the developer said he doesn't offer support for xcode. As for now, all links are opening inside the webview. How can I open them in safari, and urls that go to apps, to open the installed apps, such as facebook, twitter? All I could find is to open ALL URLS in safari. L.E. Thank you! So I want to load the template hosted at the url, but the links within the template to load in safari, even better, like the answer with 3 votes from Swift Open Link in Safari. When I try to use that code, I get an error. Sorry and thanks! I don't know swift at all (or coding much).

Community
  • 1
  • 1
J. Doe
  • 11
  • 3
  • Check : http://stackoverflow.com/questions/10416338/open-a-facebook-link-by-native-facebook-app-on-ios and this : http://stackoverflow.com/questions/30794552/open-link-on-twitter-app-ios – Priyal Feb 24 '17 at 11:03
  • `UIApplication.shared.open(NSURL(string:"https://url.tld/to/the/site") as! URL, options: [:], completionHandler: nil)` – viral Feb 24 '17 at 11:04
  • Thanks iOS App Dev. My app consists of loading the https://url.tld/to/t‌​he/site. So When I load that, it loads the html5 template. I want all other URLs beside that to be opened in safari OR some sort of inception webview in webview? Is that even possible? – J. Doe Feb 24 '17 at 13:26

1 Answers1

2

If you want to open url as safari than You can use Add SafariSevices.Framework

 import SafariServices
 class ViewController: UIViewController, SFSafariViewControllerDelegate
 {

@IBAction func btnClick(_ sender: Any)

{
    let safariVC = SFSafariViewController(url: NSURL(string: "https://www.google.co.in") as! URL)

    self.present(safariVC, animated: true, completion: nil)

}

 func safariViewControllerDidFinish(controller: SFSafariViewController)
{
    controller.dismiss(animated: true, completion: nil)
}

}
Lalit kumar
  • 1,797
  • 1
  • 8
  • 14