Skip to content

Commit

Permalink
Upstream IPC: Web container => Jasonette (#253)
Browse files Browse the repository at this point in the history
  • Loading branch information
gliechtenstein authored Aug 19, 2017
1 parent 6b731be commit 0541088
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app/Jasonette/Jason.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

@import MediaPlayer;

@interface Jason : NSObject <UINavigationControllerDelegate, UIImagePickerControllerDelegate, UITabBarControllerDelegate, PBJVisionDelegate>
@interface Jason : NSObject <UINavigationControllerDelegate, UIImagePickerControllerDelegate, UITabBarControllerDelegate, PBJVisionDelegate, UIWebViewDelegate>

@property (strong, nonatomic) NSDictionary *parser;
@property (strong, nonatomic) NSDictionary *data;
Expand Down
33 changes: 32 additions & 1 deletion app/Jasonette/Jason.m
Original file line number Diff line number Diff line change
Expand Up @@ -1857,14 +1857,23 @@ - (void)drawAdvancedBackground:(NSDictionary*)bg{
VC.background = nil;
}
VC.background = [[UIWebView alloc] initWithFrame: [UIScreen mainScreen].bounds];
((UIWebView*)VC.background).delegate = self;

// Need to make the background transparent so that it doesn't flash white when first loading
VC.background.opaque = NO;
VC.background.backgroundColor = [UIColor clearColor];
}
if(bg[@"text"]){
NSString *html = bg[@"text"];
[((UIWebView*)VC.background) loadHTMLString:html baseURL:nil];
if(VC.background.payload && VC.background.payload[@"html"] && [VC.background.payload[@"html"] isEqualToString:html]) {
// same html, no need to reload
} else {
// different html, reload
[((UIWebView*)VC.background) loadHTMLString:html baseURL:nil];
}

VC.background.payload = @{@"html": html};

}

// allow autoplay
Expand All @@ -1890,6 +1899,28 @@ - (void)drawAdvancedBackground:(NSDictionary*)bg{
}
});
}
- (void) webViewDidFinishLoad:(UIWebView *)webView
{
NSString *summon = @"var JASON={call: function(e){var n=document.createElement(\"IFRAME\");n.setAttribute(\"src\",\"jason:\"+JSON.stringify(e)),document.documentElement.appendChild(n),n.parentNode.removeChild(n),n=null}};";
[webView stringByEvaluatingJavaScriptFromString:summon];
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {

if ([[[request URL] absoluteString] hasPrefix:@"jason:"]) {
// Extract the selector name from the URL
NSString *json = [[[request URL] absoluteString] substringFromIndex:6];
json = [json stringByRemovingPercentEncoding];

NSData *data = [json dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *action = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
[[Jason client] call: action];

return NO;
}

return YES;
}

- (void)drawBackground:(NSString *)bg{
dispatch_async(dispatch_get_main_queue(), ^{

Expand Down
20 changes: 20 additions & 0 deletions app/Jasonette/JasonHtmlComponent.m
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,26 @@ + (void)actionButtonClicked:(UIButton *)sender{
[[Jason client] call: sender.payload[@"action"]];
}
}
+ (void) webViewDidFinishLoad:(UIWebView *)webView
{
NSString *summon = @"var JASON={call: function(e){var n=document.createElement(\"IFRAME\");n.setAttribute(\"src\",\"jason:\"+JSON.stringify(e)),document.documentElement.appendChild(n),n.parentNode.removeChild(n),n=null}};";
[webView stringByEvaluatingJavaScriptFromString:summon];
}
+ (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {

if ([[[request URL] absoluteString] hasPrefix:@"jason:"]) {
// Extract the selector name from the URL
NSString *json = [[[request URL] absoluteString] substringFromIndex:6];
json = [json stringByRemovingPercentEncoding];

NSData *data = [json dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *action = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
[[Jason client] call: action];

return NO;
}

return YES;
}

@end

0 comments on commit 0541088

Please sign in to comment.