Upgrade to Pro — share decks privately, control downloads, hide ads and more …

自社コンテンツ配信 のためのWebView Tips

自社コンテンツ配信 のためのWebView Tips

Swift愛好会 vol18

Shinji Kobayashi

April 28, 2017
Tweet

More Decks by Shinji Kobayashi

Other Decks in Programming

Transcript

  1. // બ୒ېࢭ͢ΔCSS let css = "body{-webkit-user-select:none;-webkit-user-drag:none;- webkit-touch-callout: none;}" // CSSΛಡΈࠐΜͩHTMLʹద༻͢Δjavascript

    let js = "var style = document.createElement('style');" + "style.type = 'text/css';" + "var cssContent = document.createTextNode('\(css)');" + "style.appendChild(cssContent);" + "document.body.appendChild(style);" // javascriptΛϖʔδಡΈࠐΈ׬ྃ࣌ʹຖճ࣮ߦ͢ΔΑ͏ઃఆ let noneSelectScript = WKUserScript(source: js, injectionTime: WKUserScriptInjectionTime.atDocumentEnd, forMainFrameOnly: false) //WebView࡞੒ let config = WKWebViewConfiguration() config.userContentController.addUserScript(noneSelectScript) let webView = WKWebView.init(frame: rect, configuration: config) දࣔͷ֦େʗ௕ԡ͠ͷϝχϡʔ Λېࢭ͍ͨ͠
  2. // બ୒ېࢭ͢ΔCSS let css = "body{-webkit-user-select:none;-webkit-user-drag:none;- webkit-touch-callout: none;}" // CSSΛಡΈࠐΜͩHTMLʹద༻͢Δjavascript

    let js = "var style = document.createElement('style');" + "style.type = 'text/css';" + "var cssContent = document.createTextNode('\(css)');" + "style.appendChild(cssContent);" + "document.body.appendChild(style);" // javascriptΛϖʔδಡΈࠐΈ׬ྃ࣌ʹຖճ࣮ߦ͢ΔΑ͏ઃఆ let noneSelectScript = WKUserScript(source: js, injectionTime: WKUserScriptInjectionTime.atDocumentEnd, forMainFrameOnly: false) //WebView࡞੒ let config = WKWebViewConfiguration() config.userContentController.addUserScript(noneSelectScript) let webView = WKWebView.init(frame: rect, configuration: config) දࣔͷ֦େʗ௕ԡ͠ͷϝχϡʔ Λېࢭ͍ͨ͠ HTMLʹ௚઀CSS Λࢦఆͯ͠΋ྑ͍
  3. // બ୒ېࢭ͢ΔCSS let css = "body{-webkit-user-select:none;-webkit-user-drag:none;- webkit-touch-callout: none;}" // CSSΛಡΈࠐΜͩHTMLʹద༻͢Δjavascript

    let js = "var style = document.createElement('style');" + "style.type = 'text/css';" + "var cssContent = document.createTextNode('\(css)');" + "style.appendChild(cssContent);" + "document.body.appendChild(style);" // javascriptΛϖʔδಡΈࠐΈ׬ྃ࣌ʹຖճ࣮ߦ͢ΔΑ͏ઃఆ let noneSelectScript = WKUserScript(source: js, injectionTime: WKUserScriptInjectionTime.atDocumentEnd, forMainFrameOnly: false) //WebView࡞੒ let config = WKWebViewConfiguration() config.userContentController.addUserScript(noneSelectScript) let webView = WKWebView.init(frame: rect, configuration: config) දࣔͷ֦େʗ௕ԡ͠ͷϝχϡʔ Λېࢭ͍ͨ͠
  4. // બ୒ېࢭ͢ΔCSS let css = "body{-webkit-user-select:none;-webkit-user-drag:none;- webkit-touch-callout: none;}" // CSSΛಡΈࠐΜͩHTMLʹద༻͢Δjavascript

    let js = "var style = document.createElement('style');" + "style.type = 'text/css';" + "var cssContent = document.createTextNode('\(css)');" + "style.appendChild(cssContent);" + "document.body.appendChild(style);" // javascriptΛϖʔδಡΈࠐΈ׬ྃ࣌ʹຖճ࣮ߦ͢ΔΑ͏ઃఆ let noneSelectScript = WKUserScript(source: js, injectionTime: WKUserScriptInjectionTime.atDocumentEnd, forMainFrameOnly: false) //WebView࡞੒ let config = WKWebViewConfiguration() config.userContentController.addUserScript(noneSelectScript) let webView = WKWebView.init(frame: rect, configuration: config) දࣔͷ֦େʗ௕ԡ͠ͷϝχϡʔ Λېࢭ͍ͨ͠
  5. // બ୒ېࢭ͢ΔCSS let css = "body{-webkit-user-select:none;-webkit-user-drag:none;- webkit-touch-callout: none;}" // CSSΛಡΈࠐΜͩHTMLʹద༻͢Δjavascript

    let js = "var style = document.createElement('style');" + "style.type = 'text/css';" + "var cssContent = document.createTextNode('\(css)');" + "style.appendChild(cssContent);" + "document.body.appendChild(style);" // javascriptΛϖʔδಡΈࠐΈ׬ྃ࣌ʹຖճ࣮ߦ͢ΔΑ͏ઃఆ let noneSelectScript = WKUserScript(source: js, injectionTime: WKUserScriptInjectionTime.atDocumentEnd, forMainFrameOnly: false) //WebView࡞੒ let config = WKWebViewConfiguration() config.userContentController.addUserScript(noneSelectScript) config.selectionGranularity = .character let webView = WKWebView.init(frame: rect, configuration: config) NG: දࣔͷ֦େʗ௕ԡ͠ͷϝχϡʔ Λېࢭ͍ͨ͠ ͜ͷࢦఆ͸NG
  6. ಈըΛΠϯϥΠϯͰ࠶ੜ͍ͨ͠ ಈըΛࣗಈ࠶ੜ͍ͨ͠ //WebViewઃఆ let config = WKWebViewConfiguration() config.allowsInlineMediaPlayback = true

    config.userContentController.addUserScript(noneSelectScript) if #available(iOS 10.0, *) { config.mediaTypesRequiringUserActionForPlayback = [] } else { config.requiresUserActionForMediaPlayback = false } //WebView࡞੒ let config = WKWebViewConfiguration() let webView = WKWebView.init(frame: rect, configuration: config)
  7. ಈըΛΠϯϥΠϯͰ࠶ੜ͍ͨ͠ ಈըΛࣗಈ࠶ੜ͍ͨ͠ //WebViewઃఆ let config = WKWebViewConfiguration() config.allowsInlineMediaPlayback = true

    config.userContentController.addUserScript(noneSelectScript) if #available(iOS 10.0, *) { config.mediaTypesRequiringUserActionForPlayback = [] } else { config.requiresUserActionForMediaPlayback = false } //WebView࡞੒ let config = WKWebViewConfiguration() let webView = WKWebView.init(frame: rect, configuration: config) ΠϯϥΠϯಈը࠶ੜΛڐՄ͢Δ
  8. ಈըΛΠϯϥΠϯͰ࠶ੜ͍ͨ͠ ಈըΛࣗಈ࠶ੜ͍ͨ͠ //WebViewઃఆ let config = WKWebViewConfiguration() config.allowsInlineMediaPlayback = true

    config.userContentController.addUserScript(noneSelectScript) if #available(iOS 10.0, *) { config.mediaTypesRequiringUserActionForPlayback = [] } else { config.requiresUserActionForMediaPlayback = false } //WebView࡞੒ let config = WKWebViewConfiguration() let webView = WKWebView.init(frame: rect, configuration: config) ࣗಈ࠶ੜΛڐՄ͢Δ (iOS ʙ9)
  9. ಈըΛΠϯϥΠϯͰ࠶ੜ͍ͨ͠ ಈըΛࣗಈ࠶ੜ͍ͨ͠ //WebViewઃఆ let config = WKWebViewConfiguration() config.allowsInlineMediaPlayback = true

    config.userContentController.addUserScript(noneSelectScript) if #available(iOS 10.0, *) { config.mediaTypesRequiringUserActionForPlayback = [] } else { config.requiresUserActionForMediaPlayback = false } //WebView࡞੒ let config = WKWebViewConfiguration() let webView = WKWebView.init(frame: rect, configuration: config) ࣗಈ࠶ੜΛڐՄ͢Δ (iOS 10ʙ)
  10. ಈըΛΠϯϥΠϯͰ࠶ੜ͍ͨ͠ NG: ಈըΛࣗಈ࠶ੜ͍ͨ͠ //WebViewઃఆ let config = WKWebViewConfiguration() config.allowsInlineMediaPlayback =

    true config.userContentController.addUserScript(noneSelectScript) if #available(iOS 10.0, *) { config.mediaTypesRequiringUserActionForPlayback = [] } else { config.requiresUserActionForMediaPlayback = false } //WebView࡞੒ let config = WKWebViewConfiguration() let webView = WKWebView.init(frame: rect, configuration: config) ҰൠͷαΠτΛࢀর͢ΔWebViewʹ͸ ઃఆ͠ͳ͍ํ͕ྑ͍
  11. ը໘ભҠ͢Δલʹ࠶ੜதͷಈըΛ શͯఀࢭ͢Δ //࠶ੜதͷϜʔϏʔΛఀࢭ͢Δ func stopAllMovie() { let js = "var

    v = document.getElementsByTagName('video');" + "for (var i = 0; i < v.length; ++i) { v[i].pause(); }" webView.evaluateJavaScript(js, completionHandler: nil) } override func viewDidDisappear(_ animated: Bool) { super.viewDidDisappear(animated) self.stopAllMovie() }
  12. func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy)

    -> Void) { var result = WKNavigationActionPolicy.cancel defer { decisionHandler(result) } guard let url = navigationAction.request.url else { return } if let info = TLConfig.getIndexUrlInfo(url.absoluteString) { // TOPLOGͷURLͰ͋Δ هࣄ༻ͷ7JFX$POUSPMMFSΛ࡞੒͠/BWJHBUJPO$POUSPMMFSʹPush } else { // ֎෦ͷ63-Ͱ͋Δ ผͷ8FC7JFXΛ࡞੒͠ϞʔμϧͰදࣔ } ભҠઌʹΑͬͯը໘ͷಈ࡞Λมߋ͍ͨ͠
  13. ભҠઌʹΑͬͯը໘ͷಈ࡞Λมߋ͍ͨ͠ func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping

    (WKNavigationActionPolicy) -> Void) { var result = WKNavigationActionPolicy.cancel defer { decisionHandler(result) } guard let url = navigationAction.request.url else { return } if let info = TLConfig.getIndexUrlInfo(url.absoluteString) { // TOPLOGͷURLͰ͋Δ هࣄ༻ͷ7JFX$POUSPMMFSΛ࡞੒͠/BWJHBUJPO$POUSPMMFSʹPush } else { // ֎෦ͷ63-Ͱ͋Δ ผͷ8FC7JFXΛ࡞੒͠ϞʔμϧͰදࣔ } WKNavigationDelegateͷ decidePolicyFornavigationActionΛ࣮૷ WebViewͷભҠΛϑοΫ͢Δ
  14. ભҠઌʹΑͬͯը໘ͷಈ࡞Λมߋ͍ͨ͠ func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping

    (WKNavigationActionPolicy) -> Void) { var result = WKNavigationActionPolicy.cancel defer { decisionHandler(result) } guard let url = navigationAction.request.url else { return } if let info = TLConfig.getIndexUrlInfo(url.absoluteString) { // TOPLOGͷURLͰ͋Δ هࣄ༻ͷ7JFX$POUSPMMFSΛ࡞੒͠/BWJHBUJPO$POUSPMMFSʹPush } else { // ֎෦ͷ63-Ͱ͋Δ ผͷ8FC7JFXΛ࡞੒͠ϞʔμϧͰදࣔ } decisionHandlerʹWKNavigationActionPolicy.cancelΛ ηοτ͢ΔͱભҠ͠ͳ͍
  15. ભҠઌʹΑͬͯը໘ͷಈ࡞Λมߋ͍ͨ͠ func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping

    (WKNavigationActionPolicy) -> Void) { var result = WKNavigationActionPolicy.cancel defer { decisionHandler(result) } guard let url = navigationAction.request.url else { return } if let info = TLConfig.getIndexUrlInfo(url.absoluteString) { // TOPLOGͷURLͰ͋Δ هࣄ༻ͷ7JFX$POUSPMMFSΛ࡞੒͠/BWJHBUJPO$POUSPMMFSʹPush } else { // ֎෦ͷ63-Ͱ͋Δ ผͷ8FC7JFXΛ࡞੒͠ϞʔμϧͰදࣔ } deferͰηοτ͓ͯ͘͠ͱguardͰ൑ఆͨ͠ޙʹ return͢Δ͚ͩͰྑ͍ͷͰॻ͖΍͍͢
  16. ભҠઌʹΑͬͯը໘ͷಈ࡞Λมߋ͍ͨ͠ func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping

    (WKNavigationActionPolicy) -> Void) { var result = WKNavigationActionPolicy.cancel defer { decisionHandler(result) } guard let url = navigationAction.request.url else { return } if let info = TLConfig.getIndexUrlInfo(url.absoluteString) { // TOPLOGͷURLͰ͋Δ هࣄ༻ͷ7JFX$POUSPMMFSΛ࡞੒͠/BWJHBUJPO$POUSPMMFSʹPush } else { // ֎෦ͷ63-Ͱ͋Δ ผͷ8FC7JFXΛ࡞੒͠ϞʔμϧͰදࣔ } URL͕ࣗࣾͷ΋ͷ͔Ͳ͏͔Λ൑ఆ
  17. ಛघͳϦϯΫΛઃఆͯ͠ ΞϓϦʹಛఆͷಈ࡞Λ͍ͤͨ͞ // ֎෦ͷ63-Ͱ͋Δ switch url.scheme! { case "safari-http": fallthrough

    case "safari-https": let str = url.absoluteString guard let url = URL(string: str.substring(from: str.index(str.startIndex, offsetBy: "safari-".characters.count))) else { return } // Safariىಈ UIApplication.shared.openURL(url) case "http": fallthrough case "https": …
  18. ಛघͳϦϯΫΛઃఆͯ͠ ΞϓϦʹಛఆͷಈ࡞Λ͍ͤͨ͞ // ֎෦ͷ63-Ͱ͋Δ switch url.scheme! { case "safari-http": fallthrough

    case "safari-https": let str = url.absoluteString guard let url = URL(string: str.substring(from: str.index(str.startIndex, offsetBy: "safari-".characters.count))) else { return } // Safariىಈ UIApplication.shared.openURL(url) case "http": fallthrough case "https": … SafariΛىಈ͢ΔಛघURL
  19. ಛघͳϦϯΫΛઃఆͯ͠ ΞϓϦʹಛఆͷಈ࡞Λ͍ͤͨ͞ // ֎෦ͷ63-Ͱ͋Δ switch url.scheme! { case "safari-http": fallthrough

    case "safari-https": let str = url.absoluteString guard let url = URL(string: str.substring(from: str.index(str.startIndex, offsetBy: "safari-".characters.count))) else { return } // Safariىಈ UIApplication.shared.openURL(url) case "http": fallthrough case "https": … URL͔Βsafari-ΛऔΓআ͍ͯ OpenURLͰsafariΛىಈ
  20. ಛघͳϦϯΫΛઃఆͯ͠ ΞϓϦʹಛఆͷಈ࡞Λ͍ͤͨ͞ case "http": fallthrough case "https": if navigationAction.targetFrame?.isMainFrame ??

    false { // ϝΠϯը໘ͷભҠͷ৔߹ guard let host = url.host else { return } if directNavigationHosts.index(of: host) != nil { // ௚઀ભҠ UIApplication.shared.openURL(url) } else { // ֎෦ࢀর༻ϒϥ΢βΛϞʔμϧͰىಈ } } else { // ຒΊࠐΈͷ৔߹ result = WKNavigationActionPolicy.allow } …
  21. ಛघͳϦϯΫΛઃఆͯ͠ ΞϓϦʹಛఆͷಈ࡞Λ͍ͤͨ͞ case "http": fallthrough case "https": if navigationAction.targetFrame?.isMainFrame ??

    false { // ϝΠϯը໘ͷભҠͷ৔߹ guard let host = url.host else { return } if directNavigationHosts.index(of: host) != nil { // ௚઀ભҠ UIApplication.shared.openURL(url) } else { // ֎෦ࢀর༻ϒϥ΢βΛϞʔμϧͰىಈ } } else { // ຒΊࠐΈͷ৔߹ result = WKNavigationActionPolicy.allow } … ભҠ͢Δը໘͕ϝΠϯͷ ϑϨʔϜ͔Ͳ͏͔ΛνΣοΫ
  22. ಛघͳϦϯΫΛઃఆͯ͠ ΞϓϦʹಛఆͷಈ࡞Λ͍ͤͨ͞ case "http": fallthrough case "https": if navigationAction.targetFrame?.isMainFrame ??

    false { // ϝΠϯը໘ͷભҠͷ৔߹ guard let host = url.host else { return } if directNavigationHosts.index(of: host) != nil { // ௚઀ભҠ UIApplication.shared.openURL(url) } else { // ֎෦ࢀর༻ϒϥ΢βΛϞʔμϧͰىಈ } } else { // ຒΊࠐΈͷ৔߹ result = WKNavigationActionPolicy.allow } … ௚઀ભҠ͍ͤͨ͞HostΛ ϦετΞοϓͯ͠൑ఆ (itunes.apple.comͳͲ)
  23. ಛघͳϦϯΫΛઃఆͯ͠ ΞϓϦʹಛఆͷಈ࡞Λ͍ͤͨ͞ case "http": fallthrough case "https": if navigationAction.targetFrame?.isMainFrame ??

    false { // ϝΠϯը໘ͷભҠͷ৔߹ guard let host = url.host else { return } if directNavigationHosts.index(of: host) != nil { // ௚઀ભҠ UIApplication.shared.openURL(url) } else { // ֎෦ࢀর༻ϒϥ΢βΛϞʔμϧͰىಈ } } else { // ຒΊࠐΈͷ৔߹ result = WKNavigationActionPolicy.allow } … SFSafariViewController΍ผͷ֎෦ϒϥ΢β༻ ViewControllerΛϞʔμϧͰදࣔ
  24. ಛघͳϦϯΫΛઃఆͯ͠ ΞϓϦʹಛఆͷಈ࡞Λ͍ͤͨ͞ case "http": fallthrough case "https": if navigationAction.targetFrame?.isMainFrame ??

    false { // ϝΠϯը໘ͷભҠͷ৔߹ guard let host = url.host else { return } if directNavigationHosts.index(of: host) != nil { // ௚઀ભҠ UIApplication.shared.openURL(url) } else { // ֎෦ࢀর༻ϒϥ΢βΛϞʔμϧͰىಈ } } else { // ຒΊࠐΈͷ৔߹ result = WKNavigationActionPolicy.allow } … iFrameͳͲ
  25. case "http": fallthrough case "https": … case "globalsetting-toplog": //ઃఆΛ։͘ case

    "nextpage": //࣍ϖʔδ΁ case "toplog-login": //ϩάΠϯ case "toplog-display-close": //Ϟʔμϧը໘Λด͡Δ … default: //֎෦ΞϓϦΛىಈ UIApplication.shared.openURL(url) } ಛघͳϦϯΫΛઃఆͯ͠ ΞϓϦʹಛఆͷಈ࡞Λ͍ͤͨ͞ ΞϓϦʹಈ࡞ΛϑΟʔυόοΫ
  26. ಛघͳϦϯΫΛઃఆͯ͠ ΞϓϦʹಛఆͷಈ࡞Λ͍ͤͨ͞ case "http": fallthrough case "https": … case "globalsetting-toplog":

    //ઃఆΛ։͘ case "nextpage": //࣍ϖʔδ΁ case "toplog-login": //ϩάΠϯ case "toplog-display-close": //Ϟʔμϧը໘Λด͡Δ … default: //֎෦ΞϓϦΛىಈ UIApplication.shared.openURL(url) } ผΞϓϦͷURLεΩʔϜ