Last active
August 29, 2015 14:07
-
-
Save yannickl/4a3a11073d3cedf7e7ec to your computer and use it in GitHub Desktop.
[rdar://18512488] WKWebView script message handler bug in iOS 8 and Xcode 6.0.1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.swift | |
// TestWKWebView | |
// | |
// Created by Yannick Loriot on 01/10/14. | |
// Copyright (c) 2014 Yannick Loriot. All rights reserved. | |
// | |
import UIKit | |
import WebKit | |
let htmlContent = "<!DOCTYPE html><html><head><script>webkit.messageHandlers.event.postMessage('message 1'); setTimeout(function() { webkit.messageHandlers.event.postMessage('message 2'); }, 2000);</script></head></html>" | |
class MyObject: NSObject, WKScriptMessageHandler { | |
var webView: WKWebView | |
override init() { | |
var config = WKWebViewConfiguration() | |
webView = WKWebView(frame: CGRectZero, configuration: config) | |
webView.loadHTMLString(htmlContent, baseURL: nil) | |
super.init() | |
config.userContentController.addScriptMessageHandler(self, name: "event") | |
} | |
// MARK: - WKScriptMessageHandler Methods | |
func userContentController(userContentController: WKUserContentController!, didReceiveScriptMessage message: WKScriptMessage!) { | |
println("\(message.name) received: \(message.body)") | |
} | |
} | |
class ViewController: UIViewController { | |
var strongObject: MyObject? | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
strongObject = MyObject() | |
// Everything is OK with the simulator but if I comment the line below it does not work on my iPad Mini Retina (iOS 8.0.2) | |
view.addSubview(strongObject!.webView) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment