【iOS】ATS

UIWebView, WKWebView, Safari, SFSafariViewController

UIWebView

since iOS 2, deprecated now.

WKWebView

Since iOS 8, for replacing the UIWebView, faster, lower memory costs, less bugs to make crash, and much more safty.

import WebKit

let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration: webConfiguration)
let myURL = URL(string: "https://www.apple.com")
let myRequest = URLRequest(url: myURL!)
webView.load(myRequest)
view.addSubview(webView)

Safari

An outside browser to make it easy to view a web page without interaction with app.

@IBAction func showWebSite() {
    let url = NSURL(string: "https://www.google.com")
    UIApplication.sharedApplication().openURL(url!)
}

SFSafariViewController

Since iOS 9, an embedded Safari browser can be used to view a web page in Safari like UI, wihtout leaving our app, with sharing all user data from Safari, but with less content controlling from our app.

SFSafariViewControllerはアプリ内でWeb画面を表示したいときに使うことをAppleが奨励しています。これにより、今まで自作アプリからSafariを起動させていたのに対して、アプリ内でSafariを呼び出すことが可能となりました。
気をつけて欲しいのは、 カスタマイズはできない ということです。
例えば、UIWebViewやWKWebViewでやっていたような『Web側から document.location = …. を実行することでネイティブ側で何か処理をさせる』といったことはできません。
あくまでも 単なるWebサイトの表示 に利用します。

needs: SafariServices.framework

@IBAction func showWebSite() {
    let url = NSURL(string: "https://www.google.com")
    let safariVC = SFSafariViewController(URL: url!)
    self.showViewController(safariVC, sender: nil)
}

ATS

App Transport Security, since iOS 9.

ATS設定の適用範囲

  • NSURLRequest
  • NSURLConnection
  • NSURLSession
  • UIWebView
  • WKWebView
  • CFNetwork

ATSの対象外

  • Appleが提供している低レベルネットワークAPI(具体的な記述はない)
  • サードパーティのネットワークライブラリを利用
  • SFSafariViewController / Safari

ATS protections are not available when using lower-level networking APIs provided by Apple, or when using third-party networking libraries.

Note: Consider risks carefully before opting to use lower-level networking APIs provided by Apple, or opting to use third-party networking libraries. Such approaches lose App Transport Security protections, putting your app and your user’s data at risk.

If you’re using SFSafariViewController, you shouldn’t need any ATS exceptions; SFSafariViewController acts just like Safari with regards ATS.

Disable ATS

  • If you’re using UIWebView, you will need to use NSAllowsArbitraryLoads. In this case you should include an explanation as to why it’s necessary for you to continue using UIWebView rather than WKWebView.
  • If you’re using WKWebView, take advantage of NSAllowsArbitraryLoadsInWebContent.
  • If you’re using SFSafariViewController, you shouldn’t need any ATS exceptions; SFSafariViewController acts just like Safari with regards ATS.

iOS 9

info.plist: NSAppTransportSecurity > NSAllowsArbitraryLoads:true

ATSを無効にする(Appleは非推奨)

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

ドメインを指定してATSを無効にする(推奨ではないが、暫定対応としてはこちらを推奨していると思われる)

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSExceptionDomains</key>
    <dict>
        <key>xxx.co.jp</key>
        <dict>
            <key>NSIncludesSubdomains</key>
            <true/>
            <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
            <true/>
            <key>NSTemporaryExceptionRequiresForwardSecrecy</key>
            <false/>
        </dict>
    </dict>
</dict>

iOS 10

info.plist: NSAppTransportSecurity > NSAllowsArbitraryLoadsInWebContent:true

<key>NSAppTransportSecurity</key>
<dict>
    <key> NSAllowsArbitraryLoadsInWebContent</key>
    <true/>
</dict>

References

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1.暗闇より夜魔来たる-1あなたはきっとこんな私をお許しにはならないでしょう…ですが、私はあなたを守る以外の何かを...
    波沙诺瓦阅读 5,914评论 1 2
  • 陽の光 闇の月 陽も月も異なれど、同じように地上を照らす。けれど、両者は決してまみえることはない。陽が輝くとき月は...
    波沙诺瓦阅读 6,698评论 0 7
  • 1.暗闇より夜魔来たる-1あなたはきっとこんな私をお許しにはならないでしょう…ですが、私はあなたを守る以外の何かを...
    波沙诺瓦阅读 8,487评论 0 7
  • 我这一生,生在北方活在北方 死后自然也会葬在北方。 喜欢了一个人,总以为, 自己去了点不一样地方, 只是那里已没有...
    张家有子名良阅读 1,540评论 0 6
  • 我是一个透明人 如同一台路由器 接到什么就转手 不留一物惹尘埃
    荒芜葳蕤阅读 2,600评论 1 1