참고 : WWDC 2013 615 - Integrating JavaScript into Native Apps
Web Javascript 에서 ios 호출하는 방법 (iOS 7.0 이상)
@objc protocol ScriptProtocol : JSExport {
func toMoveScene(url:String)
func toLoading(show:String)
}
@objc class MyScript: NSObject {
var webView: UIWebView
init(webView: UIWebView) {
self.webView = webView
}
}
extension MyScript: ScriptProtocol {
func toMoveScene(url:String) {
print("url : \(url)")
}
func toLoading(show:String){
print("show : \(show) ")
}
}
WebViewDelegate 에서 다음과 같이 사용 하면 Web뷰에서 myScript.toLoading("show"); 사용할때
MyScript.oLoading() 함수가 호출된다.
func webViewDidFinishLoad(webView: UIWebView){
let key = "documentView.webView.mainFrame.javaScriptContext"
let webViewContext: AnyObject? = webView.valueForKeyPath(key)
if let context:JSContext = webViewContext as? JSContext {
context.setObject(MyScript(webView: self), forKeyedSubscript: "myScript")
context.evaluateScript("function toMoveScene(url) {toMoveScene(url);}" +
"function toLoading(show) {toLoading(show);}")
}
}
'iOS > 참고' 카테고리의 다른 글
iOS WebView 서버 로그 보려면 (0) | 2016.07.12 |
---|---|
iOS9 에서 http 사용하기 (0) | 2016.03.14 |
[ios] javascript -> ios 호출하기 (0) | 2015.11.17 |
MFMailComposeViewController 사용해서 메일 보내기 (0) | 2015.05.19 |
[iOS8] UITableView 에서 select cell color 적용하기 (0) | 2015.04.29 |
UIImageView에 tintColor 적용하기 (0) | 2015.04.28 |
댓글을 달아 주세요