Pass info from Angular4 (WKWebView) to Swift

725 Views Asked by At

Hi I have a web app that I am wrapping into a WKWebView to distribute through Google Play and App Store.

In order to get this working just the way I want, I need to be able to pass some text strings between Angular and Swift.

It is the passing of info from Angular -> Swift which is the most important step for me to solve right now, I've spent a couple of days googling possible solutions but there are not to many articles on this for Angular.

I have found a few that seem to be somewhat outdated with regards to code syntax, but the majority of the articles cover passing info from JavaScript to Swift and not TypeScript/Angular4 to Swift.

The best use case I have for this is that when a user enters his e-mail address in the sign in form in the WKWebView, I need to retrieve that e-mail in Swift in order to pick up what user is logging in, and to map that e-mail address to the Firebase Registration Token for the device logging in (in order to provide push messaging).

Does anyone have a solution for this? For instance, how would I go about retrieving the text entered in the following form in the web app;

<input class="form-control ng-dirty ng-valid ng-touched" formcontrolname="email" id="email" type="email">

EDIT: I do not believe this question to be a duplicate as the "window.webkit.messageHandler" function provides an error at compilation when trying to implement it in Angular/TypeScript. I will provide sample code and compilation error output once my dev can mail it to me.

1

There are 1 best solutions below

0
On BEST ANSWER

I ended up solving this by injecting a script to set, and read, data from the WKWebView Local storage.

This permits me to pick up tokens and strings set by the web app, and for me to pass info to the web app through local storage as well.

The following line will set a key named 'device' with the contents of the tokenFirebase variable, it will also read the 'token' and the 'userId' contents of the local storage which are set by the Angular Web app:

let addCookieScript="localStorage.setItem('device', '\(self.tokenFirebase)');\nconsole.log(localStorage.getItem('token'));\nconsole.log(localStorage.getItem('userId'));\n"