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

ブラウザアプリ『Smooz』を作る中で会得した WKWebviewの3つのTips

Yuichi Kato
October 26, 2016

ブラウザアプリ『Smooz』を作る中で会得した WKWebviewの3つのTips

1. WebViewが真っ白になる問題
2. リンクのタッチアップではなくタッチダウンを取得したい
3. WKWebView内のJavaScriptをデバッグしたい
まとめ:WKWebViewを使ってよかったこと

http://smoozapp.com/

Yuichi Kato

October 26, 2016
Tweet

More Decks by Yuichi Kato

Other Decks in Technology

Transcript

  1. JSͰࢦఆͷҐஔʹϦϯΫ͕͋Δ͔֬ೝ͢Δ function getURLs(x, y) { var urls = { link:

    null, image: null }; var e = document.elementFromPoint(x,y); while (e) { var name = e.tagName if (name == 'A') { urls.link = e.getAttribute('href') || null; } e = e.parentNode; } return urls; } LinkDetection.js
  2. JSΛWKWebViewʹInject͢Δ class WebViewConfiguration: WKWebViewConfiguration { fileprivate class UserContentController: WKUserContentController {

    static let sharedInstance = UserContentController() fileprivate override init() { super.init() do { let scriptURL = Bundle.main.path(forResource:”LinkDetection”, ofType: "js") let scriptContent = try String(contentsOfFile: scriptURL!, encoding: String.Encoding.utf8) let script = WKUserScript(source: scriptContent, injectionTime: .atDocumentEnd, forMainFrameOnly: true) addUserScript(script) } catch { print(#function, error) } }
  3. ௕ԡ͠׬ྃͨ͠Βɺ͖ͬ͞ͷscriptΛ࣮ߦͯ͠λονҐஔʹ ϦϯΫ͕͋Δ͔Λ൑ఆ͢Δ extension WKWebView { internal func getHTMLElementAt(_ point: CGPoint)

    { let js = "getURLs(\(point.x), \(point.y))” evaluateJavaScript(js) { (urls, error) in url͕͋Ε͹৽͍͠λϒΛ։͘ } } }