Communicate to and from Hype and a native iOS app

Hi all,

I'm trying to set up communication between a Hype document and a native iOS app. Basically communicating between javascript and swift and back.

I've managed to achieve sending from hype using javascript to swift (WKSwiftMessageHandler) using:

window.webkit.messageHandlers.myMessageName.postMessage("message here");

However, I'm trying to send a message from swift to display in a hype document (web app converted to native app in Xcode using @Photics wrapping app - which is amazing! )

Has anyone managed to achieve this? My plan is once I have worked out how to do this, I am going to send data to and from a hype document to store in coreData on a iOS device. Therefore making apps that can save and reload information. I just have to work out the communication part first.

Thanks,

Chris

1 Like

The good news is this was the hard part!

Communicating the other way is done via calling WKWebView's evaluateJavaScript method. Just give it a string to do whatever you'd want, like call a JS function that was already defined in the head and does some work. If you need more substantial data transfer, you can first use NSJSONSerialization to convert objects into a JSON string to send across in the call.

2 Likes

Thanks! :slightly_smiling_face:

...and it looks like Jonathan already answered this one.

I got it working around two years ago...

1 Like

If you look at my xCode Template project. Example 2. This will show you how to two way communicate.

Also on GitHub

2 Likes

Hi all,

Thanks for your quick replies to this query. I've spent a few hours this morning trying to get this to work and I think its way above my ability. I just can't get my head around how messages are passed from Swift back into Hype. However, the other way round, I'm absolutely fine with. I just can't seam to work it out.

I've tried @MarkHunte method but I can't even seam to get that to work. When I run the app, it simply displays "Hello world" rather than the app interface. Any ideas what I'm doing wrong?

I won't give up but this is more difficult than I first imagine it would be!

Thanks,

Chris

Apple introduced SwiftUI.

It sounds like you have that selected when creating the App.

You should have UIKit App Delegate selected for the Life Cycle

1 Like

Thanks @MarkHunte - works perfectly now! However I still can't get it working how I need it to work so might have to go back to the drawing board on how I'm going to implement a save and load feature in my future apps.

What are you trying to send and what framework are you trying to use for saving

Hi @MarkHunte,

I'm trying to send a line of text to swift and back to store in CoreData to create a save and load feature within my app.

I am currently trying to update some apps that I have previously created. My apps help people with learning disabilities understand health appointments / procedures however I want to be able to let the user make choices which, after being save, will load on the next time the app loads up.

I've managed to use local.storage to save different lines of text within web apps. See an example of my work at www.chardie.co.uk. If you click on the settings button on the second scene, you'll be able to change the text within the easy read document to say whatever you need it to say.

I was hoping if I could get data to and from swift and hype, then I could use coreData instead of local.storage meaning that there is less chance of the data being deleted easy or timing out in a native app.

Any help I can get with this would be really appreciated.

Thanks,

Chris

So do you already have coreData setup to do a save?

Not just yet but I’ve looked into it. I just wanted to make sure I could send to and from swift first

Can I suggest you watch these. The are 4 short videos that take you through coreData.

Ignore the parts that talk about the table, you are not using one. Instead you will use your data from hype.

Here is a quick example ( very thrown together as proof of concept ) of using stuff from the video.

Personally though I would push the text that needs editing to the App and use the app to edit ( similar to what the video shows in the name edits ) rather than a textArea or whatever on the scene.

Archive.zip (513.1 KB)

1 Like

Hi @MarkHunte. Thanks you for putting this together for me and I'll watch the videos when I get a spare few minutes tonight.

I've attempted to run you demo you sent however it throws up an error in relation to:

webView!.evaluateJavaScript("HYPE.documents['(htmlName)'].getElementById('name').querySelector('textArea').value = '(st1! )'")

Fatal error: Unexpectedly found nil while unwrapping an Optional value

any ideas how I can correct this or is this something to do with how my Xcode is set up?

Thanks again for all your help,

Chris

hmm,

Although I expect bugs in it due to it just being a proof of concept, thats one is odd.

I am still on Xcode Version 12.4 (12D4e) and MacOs Catalina.

So that could be it.

I just re downloaded the one I posted and it works.

Did you edit anything ?
I am assuming it means the st1 value is returning nil ?



Note also I have been playing with a different approach which does away with the textAreas as editors and uses the App as the editor. ( as mentioned above )

Again proof of concept to get started. But not worth posting just yet also we kinda need to get the other one working first..

1 Like

Hi @MarkHunte - Here is a screenshot when I run the code. It does appear that the st1 value is returning nil.

Might try run the code on my partners laptop if I can Xcode on that to see if it throws up the same error.

Thanks,

Chris

Hi @MarkHunte,

Just a quick update. I have tried to load the files in Xcode on my partners laptop to run the code and the same error happens. I'll try play around with this next week to see if I can get any success with it working on my set up but it does appear to be something related to running it on a newer version of Xcode.

Thanks,

Chris

What is your setup?
Xcode etc..

As a test substitute the string value with a hard coded one and see what it does.

Just to show possibilities

The code is a bit of a jumble where I am playing at seeing how this could work for multiple entries being sent to and from hype/App.

As it is it would likely confuse more than help.

So not posting it...yet at least .. but it shows you what I mean about using the built in editing of the app rather than the buggy TextAreas.

1 Like

Hi @MarkHunte. Sorry I haven’t got back to you today. I’ve been really busy but will look at this tomorrow. I did managed to hard code the string value very briefly and it worked so I’m wondering if it’s something to do with my set ups.

Thanks again,

Chris

No prob.
What system and Xcode are you on..

1 Like