Skip to content
This repository was archived by the owner on Jan 19, 2025. It is now read-only.

Commit c897ca8

Browse files
committed
New post 🚀
1 parent 4e5b673 commit c897ca8

File tree

3 files changed

+6
-6
lines changed

3 files changed

+6
-6
lines changed

_images/posts/swift-js.jpg

-20.3 KB
Loading

_drafts/2019-08-08-html-javascript-to-native-communication-ios.md renamed to _posts/2019-08-03-html-javascript-to-native-communication-ios.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
---
22
layout: post
3-
title: "Web to native (Swift) communication on iOS using WKScriptMessageHandler"
4-
description: "Did you know that is it possible to call Swift code from the JavaScript code of a web page displayed inside a WKWebView?"
5-
date: 2019-08-08
3+
title: "Web to native code communication on iOS using WKScriptMessageHandler"
4+
description: "Did you know that is it possible to call Swift/Objective-C code from the JavaScript code of a web page displayed inside a WKWebView?"
5+
date: 2019-08-03
66
image: /assets/images/posts/swift-js.jpg
7-
tags: [swift, ios, apple, mobile application development]
7+
tags: [swift, objective-c, ios, apple, mobile application development, javascript, web development]
88
comments: true
99
seo:
1010
- type: "BlogPosting"
@@ -15,7 +15,7 @@ authors: [fabrizio_duroni]
1515

1616
---
1717

18-
Sooner or later every mobile developer in the world had the following specific need: integrate a website page inside an app :weary:. Usually the feature to be developed requires a deep integration between web and native: the app must react to some changes in the web page based on the user interactions or some other events (automatic refresh, geolocation etc.). The old way to do this integration was to catch some url change/page load using the classical UIWebView delegate methods. But starting from iOS 8 there's a better way to do this integration using `WKWebView`s and `WKScriptMessageHandler`. In this post I will show you how is it possible to use them to call Swift code from Javascript code inside a webpage.
18+
Sooner or later every mobile developer in the world had the following specific need: integrate a website page inside an app. Usually the integration to be developed requires a deep integration between web and native: the app must react to some changes in the web page based on the user interactions or some other events (automatic refresh, geolocation etc.). The old way to do this integration was to catch some url change/page load using the classical UIWebView delegate methods. But starting from iOS 8 (this is old but gold :trophy::stuck_out_tongue_winking_eye:) there's a better way to do this integration using `WKWebView`s and `WKScriptMessageHandler`. In this post I will show you how is it possible to use them to call Swift code from Javascript code inside a webpage.
1919

2020
#### Implementation
2121

@@ -158,7 +158,7 @@ Let's see now the implementation of the web page. It will contain a standard htm
158158
</html>
159159
```
160160

161-
One important note to thing: `window.webkit.messageHandlers` is expose on the `window` as a global only when your page is displayed on a iOS device inside a `WKWebView`. This means that if you're planning to show your webpage with this kind of binding also on other platform, you will need to implement a guard on the `webkit.messageHandlers` object.
161+
One important thing to note: `window.webkit.messageHandlers` is expose on the `window` as a global object only when your page is displayed on a iOS device inside a `WKWebView`. This means that if you're planning to show your webpage with this kind of binding also on other platform, you will need to implement a guard on the `webkit.messageHandlers` object.
162162
You can find the complete example in this [github repository](https://github.com/chicio/Explore-WKScriptMessageHandler "WKScriptMessageHandler github repository").
163163

164164
#### Conclusion

assets/images/posts/swift-js.jpg

-15.9 KB
Loading

0 commit comments

Comments
 (0)