diff --git a/Riot/Assets/js/ModularWebApp.js b/Riot/Assets/js/ModularWebApp.js new file mode 100644 index 000000000..806cb3db7 --- /dev/null +++ b/Riot/Assets/js/ModularWebApp.js @@ -0,0 +1,63 @@ +/* + Copyright 2017 Vector Creations Ltd + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + */ + +window.riotIOS = {}; + +// Generic JS -> ObjC bridge +window.riotIOS.sendObjectMessageToObjC = function(parameters) { + var iframe = document.createElement('iframe'); + iframe.setAttribute('src', 'js:' + JSON.stringify(parameters)); + + document.documentElement.appendChild(iframe); + iframe.parentNode.removeChild(iframe); + iframe = null; +}; + +window.riotIOS.events = {}; + +// Listen to messages posted by Modular +window.riotIOS.onMessage = function(event) { + + // Do not SPAM ObjC with event already managed + if (riotIOS.events[event.data._id]) { + return; + } + + // Keep this event for future usage + riotIOS.events[event.data._id] = event; + + riotIOS.sendObjectMessageToObjC({ + 'event.data': event.data, + }); +}; +window.addEventListener('message', riotIOS.onMessage, false); + + +// ObjC -> Modular JS bridge +window.riotIOS.sendResponse = function(eventId, res) { + + // Retrieve the correspong JS event + var event = riotIOS.events[eventId]; + + console.log(eventId + ": " + event); + + var data = JSON.parse(JSON.stringify(event.data)); + data.response = res; + event.source.postMessage(data, event.origin); + + // Mark this event as handled + riotIOS.events[eventId] = true; +} diff --git a/Riot/ViewController/Widgets/ModularWebAppViewController.m b/Riot/ViewController/Widgets/ModularWebAppViewController.m index 378af89a0..ba112f8bf 100644 --- a/Riot/ViewController/Widgets/ModularWebAppViewController.m +++ b/Riot/ViewController/Widgets/ModularWebAppViewController.m @@ -18,28 +18,7 @@ #import "WidgetManager.h" -// Generic method to make a bridge between JS and the UIWebView -NSString *kJavascriptSendObjectMessage = @" \ - window.riotIOS = {}; \ - window.riotIOS.sendObjectMessage = function(parameters) { \ - var iframe = document.createElement('iframe'); \ - iframe.setAttribute('src', 'js:' + JSON.stringify(parameters)); \ - \ - document.documentElement.appendChild(iframe); \ - iframe.parentNode.removeChild(iframe); \ - iframe = null; \ - }; \ -"; - -// The function to listen to the Modular webapp messages -NSString *kJavascriptListenToModularMessages = @" \ - window.riotIOS.onMessage = function(event) { \ - sendObjectMessage({ \ - 'event.data': event.data, \ - }); \ - }; \ - window.addEventListener('message', riotIOS.onMessage, false); \ -"; +NSString *kJavascriptSendResponseToModular = @"riotIOS.sendResponse('%@', %@);"; @interface ModularWebAppViewController () @@ -163,9 +142,10 @@ NSString *kJavascriptListenToModularMessages = @" -(void)webViewDidFinishLoad:(UIWebView *)theWebView { - // Setup Objs-JS bridging methods - [webView stringByEvaluatingJavaScriptFromString:kJavascriptSendObjectMessage]; - [webView stringByEvaluatingJavaScriptFromString:kJavascriptListenToModularMessages]; + // Setup js code + NSString *path = [[NSBundle mainBundle] pathForResource:@"ModularWebApp" ofType:@"js"]; + NSString *js = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil]; + [webView stringByEvaluatingJavaScriptFromString:js]; } - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType @@ -272,9 +252,13 @@ NSString *kJavascriptListenToModularMessages = @" return YES; } -- (void)sendResponse:(NSString*)response toEvent:(NSDictionary*)eventData +- (void)sendBoolResponse:(BOOL)response toEvent:(NSDictionary*)eventData { + NSString *js = [NSString stringWithFormat:kJavascriptSendResponseToModular, + eventData[@"_id"], + response ? @"true" : @"false"]; + [webView stringByEvaluatingJavaScriptFromString:js]; } #pragma mark - Modular postMessage API @@ -324,7 +308,7 @@ NSString *kJavascriptListenToModularMessages = @" if (canSend) { - [self sendResponse:@"true" toEvent:eventData]; + [self sendBoolResponse:YES toEvent:eventData]; } else {