SFSafari显示网页——swift笔记

0x00 Something

最近写ios app时想跳转到网页,查询部分资料后发现了三种方法,safari,uiwebview以及下面提到的sfsafari。
关于三种方法的优缺点可以参考iOS 9之SFSafariViewController

0x01 SFSafariViewController

SFSafariViewController用于在应用中快速继承Safari浏览器的界面而无需跳出当前应用。

0x02 核心跳转代码
import SafariServices  

class ViewController: UIViewController {  
@IBAction func openWithSafariVC(sender: AnyObject) {  
    let sfsVC = SFSafariViewController(URL: NSURL(string: "https://google.com")!)  
    //vc.delegate = self//根据需要是否保留
    presentViewController(sfsVC, animated: true, completion: nil)  
}  
}  
0x03 Delegate
// SFSafariViewControllerDelegate
// web页面加载完成
func safariViewController(controller: SFSafariViewController, didCompleteInitialLoad didLoadSuccessfully: Bool) {
print(__FUNCTION__)
print(didLoadSuccessfully)
}

func safariViewController(controller: SFSafariViewController, activityItemsForURL URL: NSURL, title: String?) -> [UIActivity] {
print(__FUNCTION__)
print("\(URL)--\(title)")
return [YJUIActivity()]
}

// 退出页面
func safariViewControllerDidFinish(controller: SFSafariViewController) {
print(__FUNCTION__)
}
0x04 提示
  1. ios9后访问http开头而不是“https”的网页报错,
    打开 Info.plist,点击加号添加一项 App Transport Security Settings,在它下面添加一项 Allow Arbitrary Loads,并设置值为 YES
  2. SFSafariViewController 只能用于 iOS 9 以上

参考: SafariServices

xmsec

Updated At: Author:xmsec
Chief Water dispenser Manager of Lancet, delivering but striving.
Github
comments powered by Disqus