0

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.

lundzern
  • 417
  • 3
  • 11
  • typescript compiles to javascript, and you'll have javascript at runtime. The javascript solutions should be what you are looking for. – toskv Aug 26 '17 at 16:02
  • I was told by the developer of the Angular app that it would be tricky, but I did suggest to him to try the "window.webkit.messageHandler" function, I guess I'll have to ask him again. – lundzern Aug 26 '17 at 16:04
  • Possible duplicate of [How to pass data from Javascript to Swift within a WKWebView?](https://stackoverflow.com/questions/35574539/how-to-pass-data-from-javascript-to-swift-within-a-wkwebview) – toskv Aug 26 '17 at 16:06
  • Not sure it is a duplicate, that thread is for Javascript only, I'm not quite sure that the solutions there are 100% applicable to my environment, but I will check this out and keep the thread updated. – lundzern Aug 26 '17 at 16:07
  • I just got a text from the angular dev saying that the "window.webkit.messageHandler" creates aan error when attempting to compile – lundzern Aug 26 '17 at 16:13

1 Answers1

0

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"
lundzern
  • 417
  • 3
  • 11