2017-02-24 75 views
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)) 
     } 
    } 

} 

该网站是一个html5 + CSS3。从themeforest购买了一个模板,但开发人员表示他不提供对xcode的支持。 至今为止,所有链接都在webview内部打开。我如何在safari中打开它们,以及转到应用程序的url,以打开已安装的应用程序,例如facebook,twitter? 我只能在safari中打开所有URL。 L.E.谢谢!所以我想加载在url处托管的模板,但要加载到safari中的模板中的链接更好,比如来自Swift Open Link in Safari的3票的答案。当我尝试使用该代码时,出现错误。对不起,谢谢!我根本不知道(或编码很多)。swift 3在Safari或原生应用中打开网址

+0

检查:http://stackoverflow.com/questions/10416338/open-a-facebook-link-by- native-facebook-app-on-ios和this:http://stackoverflow.com/questions/30794552/open-link-on-twitter-app-ios – Priyal

+0

'UIApplication.shared.open(NSURL(string:“https: //url.tld/to/the/site“)as!URL,options:[:],completionHandler:nil)' – viral

+0

感谢iOS App Dev。我的应用程序包含加载https://url.tld/to/t他/网站。所以,当我加载它,它加载html5模板。我想要在safari中打开旁边的所有其他URL或者在webview中使用某种初始webview?这甚至有可能吗? –

回答

2

如果你想打开URL如Safari不是您可以使用添加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) 
} 

} 
+0

safariVC.delegate = self 这里我得到预期的声明错误。 –

+0

//评论并检查。你有没有添加委托和框架 –

+0

错误移动到上面和下面的代码行...当我评论一个出来。 –