elsurudo elsurudo - 25 days ago 6
iOS Question

Turbolinks-ios: Metadata from web->native

I'm using Turbolinks 5 on the backend, and turbolinks-ios in my iOS app.

When a page loads in a

VisitableViewController
, I'd like to have the native app receive (or retrieve?) some metadata from the web page, in order to offer some additional UI affordances using native code.

I know I can do this in a roundabout way by setting some
<meta>
tags on the server side, and then executing JS in the
WKWebView
and getting at the meta tags that way, but it seems like a hack.

Another option is executing a turbolinks callback message and receiving via
addScriptMessageHandler
, but that also seems like a hack.

Any help would be appreciated! Perhaps there is a better, sanctioned way to do this? It seems like it would be a common need.

Answer

I ended up going with my original idea with the meta tags. Then, what I do is on page load, invoke a message handler like so, passing the contents of those (filtered) meta tags to the handler:

$(function() { if (typeof webkit != 'undefined') { var metadata = {}; $('meta[data-scanner-app="true"]').each(function() { metadata[$(this).attr('property')] = $(this).attr('content'); }); webkit.messageHandlers.ScannerApp.postMessage(metadata); } });

Then I simply intercept that handler in turbolinks, and I have the data.