Skip to content

Instantly share code, notes, and snippets.

@vmussato
Created February 27, 2018 19:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vmussato/7442b83b4a17db4d52455f428a298259 to your computer and use it in GitHub Desktop.
Save vmussato/7442b83b4a17db4d52455f428a298259 to your computer and use it in GitHub Desktop.
CDVInnAppBrowser.m
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you 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.
*/
#import "CDVInAppBrowser.h"
#import <Cordova/CDVPluginResult.h>
#import <Cordova/CDVUserAgentUtil.h>
#define kInAppBrowserTargetSelf @"_self"
#define kInAppBrowserTargetSystem @"_system"
#define kInAppBrowserTargetBlank @"_blank"
#define kInAppBrowserToolbarBarPositionTop @"top"
#define TOOLBAR_HEIGHT 44.0
#define LOCATIONBAR_HEIGHT 21.0
#define FOOTER_HEIGHT ((TOOLBAR_HEIGHT) + (LOCATIONBAR_HEIGHT))
#pragma mark CDVInAppBrowser
NSString* _optTitle;
NSString* _optColor;
NSString* _optBackground;
NSString* _optLoading;
NSString* _optBanner;
NSString* _optUrlBanner;
@interface CDVInAppBrowser () {
NSInteger _previousStatusBarStyle;
}
@end
@implementation CDVInAppBrowser
- (void)pluginInitialize
{
_previousStatusBarStyle = -1;
_callbackIdPattern = nil;
}
- (void)onReset
{
[self close:nil];
}
- (void)close:(CDVInvokedUrlCommand*)command
{
if (self.inAppBrowserViewController == nil) {
NSLog(@"IAB.close() called but it was already closed.");
return;
}
// Things are cleaned up in browserExit.
[self.inAppBrowserViewController close];
}
- (BOOL) isSystemUrl:(NSURL*)url
{
if ([[url host] isEqualToString:@"itunes.apple.com"]) {
return YES;
}
return NO;
}
- (void)open:(CDVInvokedUrlCommand*)command
{
CDVPluginResult* pluginResult;
NSString* url = [command argumentAtIndex:0];
NSString* target = [command argumentAtIndex:1 withDefault:kInAppBrowserTargetSelf];
NSString* options = [command argumentAtIndex:2 withDefault:@"" andClass:[NSString class]];
_optTitle = [command argumentAtIndex:3];
_optColor = [command argumentAtIndex:4];
_optBackground = [command argumentAtIndex:5];
_optLoading = [command argumentAtIndex:6];
_optBanner = [command argumentAtIndex:7];
_optUrlBanner = [command argumentAtIndex:8];
self.callbackId = command.callbackId;
if (url != nil) {
#ifdef __CORDOVA_4_0_0
NSURL* baseUrl = [self.webViewEngine URL];
#else
NSURL* baseUrl = [self.webView.request URL];
#endif
NSURL* absoluteUrl = [[NSURL URLWithString:url relativeToURL:baseUrl] absoluteURL];
if ([self isSystemUrl:absoluteUrl]) {
target = kInAppBrowserTargetSystem;
}
if ([target isEqualToString:kInAppBrowserTargetSelf]) {
[self openInCordovaWebView:absoluteUrl withOptions:options];
} else if ([target isEqualToString:kInAppBrowserTargetSystem]) {
[self openInSystem:absoluteUrl];
} else { // _blank or anything else
[self openInInAppBrowser:absoluteUrl withOptions:options];
}
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
} else {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"incorrect number of arguments"];
}
[pluginResult setKeepCallback:[NSNumber numberWithBool:YES]];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
- (void)openInInAppBrowser:(NSURL*)url withOptions:(NSString*)options
{
CDVInAppBrowserOptions* browserOptions = [CDVInAppBrowserOptions parseOptions:options];
if (browserOptions.clearcache) {
NSHTTPCookie *cookie;
NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (cookie in [storage cookies])
{
if (![cookie.domain isEqual: @".^filecookies^"]) {
[storage deleteCookie:cookie];
}
}
}
if (browserOptions.clearsessioncache) {
NSHTTPCookie *cookie;
NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (cookie in [storage cookies])
{
if (![cookie.domain isEqual: @".^filecookies^"] && cookie.isSessionOnly) {
[storage deleteCookie:cookie];
}
}
}
if (self.inAppBrowserViewController == nil) {
NSString* originalUA = [CDVUserAgentUtil originalUserAgent];
self.inAppBrowserViewController = [[CDVInAppBrowserViewController alloc] initWithUserAgent:originalUA prevUserAgent:[self.commandDelegate userAgent] browserOptions: browserOptions];
self.inAppBrowserViewController.navigationDelegate = self;
if ([self.viewController conformsToProtocol:@protocol(CDVScreenOrientationDelegate)]) {
self.inAppBrowserViewController.orientationDelegate = (UIViewController <CDVScreenOrientationDelegate>*)self.viewController;
}
}
UIModalPresentationStyle presentationStyle = UIModalPresentationFullScreen; // default
if (browserOptions.presentationstyle != nil) {
if ([[browserOptions.presentationstyle lowercaseString] isEqualToString:@"pagesheet"]) {
presentationStyle = UIModalPresentationPageSheet;
} else if ([[browserOptions.presentationstyle lowercaseString] isEqualToString:@"formsheet"]) {
presentationStyle = UIModalPresentationFormSheet;
}
}
self.inAppBrowserViewController.modalPresentationStyle = presentationStyle;
// Set Transition Style
// UIModalTransitionStyle transitionStyle = UIModalTransitionStylePartialCurl; // default
//
// CATransition *transition = [CATransition animation];
// transition.duration = 0.4;
// transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
// transition.type = kCATransitionPush;
// transition.subtype = kCATransitionFromRight;
//
// self.inAppBrowserViewController.modalTransitionStyle = transition;
// prevent webView from bouncing
if (browserOptions.disallowoverscroll) {
if ([self.inAppBrowserViewController.webView respondsToSelector:@selector(scrollView)]) {
((UIScrollView*)[self.inAppBrowserViewController.webView scrollView]).bounces = NO;
} else {
for (id subview in self.inAppBrowserViewController.webView.subviews) {
if ([[subview class] isSubclassOfClass:[UIScrollView class]]) {
((UIScrollView*)subview).bounces = NO;
}
}
}
}
// UIWebView options
self.inAppBrowserViewController.webView.scalesPageToFit = browserOptions.enableviewportscale;
self.inAppBrowserViewController.webView.mediaPlaybackRequiresUserAction = browserOptions.mediaplaybackrequiresuseraction;
self.inAppBrowserViewController.webView.allowsInlineMediaPlayback = browserOptions.allowinlinemediaplayback;
if (IsAtLeastiOSVersion(@"6.0")) {
self.inAppBrowserViewController.webView.keyboardDisplayRequiresUserAction = browserOptions.keyboarddisplayrequiresuseraction;
self.inAppBrowserViewController.webView.suppressesIncrementalRendering = browserOptions.suppressesincrementalrendering;
}
[self.inAppBrowserViewController navigateTo:url background:_optBackground color:_optColor title:_optTitle loading:_optLoading];
if (!browserOptions.hidden) {
[self show:nil];
}
}
- (void)show:(CDVInvokedUrlCommand*)command
{
if (self.inAppBrowserViewController == nil) {
NSLog(@"Tried to show IAB after it was closed.");
return;
}
if (_previousStatusBarStyle != -1) {
NSLog(@"Tried to show IAB while already shown");
return;
}
// _previousStatusBarStyle = [UIApplication sharedApplication].statusBarStyle;
__block CDVInAppBrowserNavigationController* nav = [[CDVInAppBrowserNavigationController alloc]
initWithRootViewController:self.inAppBrowserViewController];
nav.orientationDelegate = self.inAppBrowserViewController;
nav.navigationBarHidden = YES;
__weak CDVInAppBrowser* weakSelf = self;
// Run later to avoid the "took a long time" log message.
dispatch_async(dispatch_get_main_queue(), ^{
if (weakSelf.inAppBrowserViewController != nil) {
CATransition *transition = [CATransition animation];
transition.duration = 0.3;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionPush;
transition.subtype = kCATransitionFromRight;
[self.webView.window.layer addAnimation:transition forKey:nil];
[weakSelf.viewController presentViewController:nav animated:NO completion:nil];
}
});
}
- (void)openInCordovaWebView:(NSURL*)url withOptions:(NSString*)options
{
NSURLRequest* request = [NSURLRequest requestWithURL:url];
#ifdef __CORDOVA_4_0_0
// the webview engine itself will filter for this according to <allow-navigation> policy
// in config.xml for cordova-ios-4.0
[self.webViewEngine loadRequest:request];
#else
if ([self.commandDelegate URLIsWhitelisted:url]) {
[self.webView loadRequest:request];
} else { // this assumes the InAppBrowser can be excepted from the white-list
[self openInInAppBrowser:url withOptions:options];
}
#endif
}
- (void)openInSystem:(NSURL*)url
{
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
} else { // handle any custom schemes to plugins
[[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:CDVPluginHandleOpenURLNotification object:url]];
}
}
// This is a helper method for the inject{Script|Style}{Code|File} API calls, which
// provides a consistent method for injecting JavaScript code into the document.
//
// If a wrapper string is supplied, then the source string will be JSON-encoded (adding
// quotes) and wrapped using string formatting. (The wrapper string should have a single
// '%@' marker).
//
// If no wrapper is supplied, then the source string is executed directly.
- (void)injectDeferredObject:(NSString*)source withWrapper:(NSString*)jsWrapper
{
if (!_injectedIframeBridge) {
_injectedIframeBridge = YES;
// Create an iframe bridge in the new document to communicate with the CDVInAppBrowserViewController
[self.inAppBrowserViewController.webView stringByEvaluatingJavaScriptFromString:@"(function(d){var e = _cdvIframeBridge = d.createElement('iframe');e.style.display='none';d.body.appendChild(e);})(document)"];
}
if (jsWrapper != nil) {
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:@[source] options:0 error:nil];
NSString* sourceArrayString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
if (sourceArrayString) {
NSString* sourceString = [sourceArrayString substringWithRange:NSMakeRange(1, [sourceArrayString length] - 2)];
NSString* jsToInject = [NSString stringWithFormat:jsWrapper, sourceString];
[self.inAppBrowserViewController.webView stringByEvaluatingJavaScriptFromString:jsToInject];
}
} else {
[self.inAppBrowserViewController.webView stringByEvaluatingJavaScriptFromString:source];
}
}
- (void)injectScriptCode:(CDVInvokedUrlCommand*)command
{
NSString* jsWrapper = nil;
if ((command.callbackId != nil) && ![command.callbackId isEqualToString:@"INVALID"]) {
jsWrapper = [NSString stringWithFormat:@"_cdvIframeBridge.src='gap-iab://%@/'+encodeURIComponent(JSON.stringify([eval(%%@)]));", command.callbackId];
}
[self injectDeferredObject:[command argumentAtIndex:0] withWrapper:jsWrapper];
}
- (void)injectScriptFile:(CDVInvokedUrlCommand*)command
{
NSString* jsWrapper;
if ((command.callbackId != nil) && ![command.callbackId isEqualToString:@"INVALID"]) {
jsWrapper = [NSString stringWithFormat:@"(function(d) { var c = d.createElement('script'); c.src = %%@; c.onload = function() { _cdvIframeBridge.src='gap-iab://%@'; }; d.body.appendChild(c); })(document)", command.callbackId];
} else {
jsWrapper = @"(function(d) { var c = d.createElement('script'); c.src = %@; d.body.appendChild(c); })(document)";
}
[self injectDeferredObject:[command argumentAtIndex:0] withWrapper:jsWrapper];
}
- (void)injectStyleCode:(CDVInvokedUrlCommand*)command
{
NSString* jsWrapper;
if ((command.callbackId != nil) && ![command.callbackId isEqualToString:@"INVALID"]) {
jsWrapper = [NSString stringWithFormat:@"(function(d) { var c = d.createElement('style'); c.innerHTML = %%@; c.onload = function() { _cdvIframeBridge.src='gap-iab://%@'; }; d.body.appendChild(c); })(document)", command.callbackId];
} else {
jsWrapper = @"(function(d) { var c = d.createElement('style'); c.innerHTML = %@; d.body.appendChild(c); })(document)";
}
[self injectDeferredObject:[command argumentAtIndex:0] withWrapper:jsWrapper];
}
- (void)injectStyleFile:(CDVInvokedUrlCommand*)command
{
NSString* jsWrapper;
if ((command.callbackId != nil) && ![command.callbackId isEqualToString:@"INVALID"]) {
jsWrapper = [NSString stringWithFormat:@"(function(d) { var c = d.createElement('link'); c.rel='stylesheet'; c.type='text/css'; c.href = %%@; c.onload = function() { _cdvIframeBridge.src='gap-iab://%@'; }; d.body.appendChild(c); })(document)", command.callbackId];
} else {
jsWrapper = @"(function(d) { var c = d.createElement('link'); c.rel='stylesheet', c.type='text/css'; c.href = %@; d.body.appendChild(c); })(document)";
}
[self injectDeferredObject:[command argumentAtIndex:0] withWrapper:jsWrapper];
}
- (BOOL)isValidCallbackId:(NSString *)callbackId
{
NSError *err = nil;
// Initialize on first use
if (self.callbackIdPattern == nil) {
self.callbackIdPattern = [NSRegularExpression regularExpressionWithPattern:@"^InAppBrowser[0-9]{1,10}$" options:0 error:&err];
if (err != nil) {
// Couldn't initialize Regex; No is safer than Yes.
return NO;
}
}
if ([self.callbackIdPattern firstMatchInString:callbackId options:0 range:NSMakeRange(0, [callbackId length])]) {
return YES;
}
return NO;
}
/**
* The iframe bridge provided for the InAppBrowser is capable of executing any oustanding callback belonging
* to the InAppBrowser plugin. Care has been taken that other callbacks cannot be triggered, and that no
* other code execution is possible.
*
* To trigger the bridge, the iframe (or any other resource) should attempt to load a url of the form:
*
* gap-iab://<callbackId>/<arguments>
*
* where <callbackId> is the string id of the callback to trigger (something like "InAppBrowser0123456789")
*
* If present, the path component of the special gap-iab:// url is expected to be a URL-escaped JSON-encoded
* value to pass to the callback. [NSURL path] should take care of the URL-unescaping, and a JSON_EXCEPTION
* is returned if the JSON is invalid.
*/
- (BOOL)webView:(UIWebView*)theWebView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL* url = request.URL;
BOOL isTopLevelNavigation = [request.URL isEqual:[request mainDocumentURL]];
// See if the url uses the 'gap-iab' protocol. If so, the host should be the id of a callback to execute,
// and the path, if present, should be a JSON-encoded value to pass to the callback.
if ([[url scheme] isEqualToString:@"gap-iab"]) {
NSString* scriptCallbackId = [url host];
CDVPluginResult* pluginResult = nil;
if ([self isValidCallbackId:scriptCallbackId]) {
NSString* scriptResult = [url path];
NSError* __autoreleasing error = nil;
// The message should be a JSON-encoded array of the result of the script which executed.
if ((scriptResult != nil) && ([scriptResult length] > 1)) {
scriptResult = [scriptResult substringFromIndex:1];
NSData* decodedResult = [NSJSONSerialization JSONObjectWithData:[scriptResult dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:&error];
if ((error == nil) && [decodedResult isKindOfClass:[NSArray class]]) {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArray:(NSArray*)decodedResult];
} else {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_JSON_EXCEPTION];
}
} else {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArray:@[]];
}
[self.commandDelegate sendPluginResult:pluginResult callbackId:scriptCallbackId];
return NO;
}
} else if ((self.callbackId != nil) && isTopLevelNavigation) {
// Send a loadstart event for each top-level navigation (includes redirects).
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
messageAsDictionary:@{@"type":@"loadstart", @"url":[url absoluteString]}];
[pluginResult setKeepCallback:[NSNumber numberWithBool:YES]];
[self.commandDelegate sendPluginResult:pluginResult callbackId:self.callbackId];
}
return YES;
}
- (void)webViewDidStartLoad:(UIWebView*)theWebView
{
_injectedIframeBridge = NO;
}
- (void)webViewDidFinishLoad:(UIWebView*)theWebView
{
if (self.callbackId != nil) {
// TODO: It would be more useful to return the URL the page is actually on (e.g. if it's been redirected).
NSString* url = [self.inAppBrowserViewController.currentURL absoluteString];
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
messageAsDictionary:@{@"type":@"loadstop", @"url":url}];
[pluginResult setKeepCallback:[NSNumber numberWithBool:YES]];
[self.commandDelegate sendPluginResult:pluginResult callbackId:self.callbackId];
}
}
- (void)webView:(UIWebView*)theWebView didFailLoadWithError:(NSError*)error
{
if (self.callbackId != nil) {
NSString* url = [self.inAppBrowserViewController.currentURL absoluteString];
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR
messageAsDictionary:@{@"type":@"loaderror", @"url":url, @"code": [NSNumber numberWithInteger:error.code], @"message": error.localizedDescription}];
[pluginResult setKeepCallback:[NSNumber numberWithBool:YES]];
[self.commandDelegate sendPluginResult:pluginResult callbackId:self.callbackId];
}
}
- (void)browserExit
{
if (self.callbackId != nil) {
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
messageAsDictionary:@{@"type":@"exit"}];
[self.commandDelegate sendPluginResult:pluginResult callbackId:self.callbackId];
self.callbackId = nil;
}
// Set navigationDelegate to nil to ensure no callbacks are received from it.
self.inAppBrowserViewController.navigationDelegate = nil;
// Don't recycle the ViewController since it may be consuming a lot of memory.
// Also - this is required for the PDF/User-Agent bug work-around.
self.inAppBrowserViewController = nil;
// if (IsAtLeastiOSVersion(@"7.0")) {
// if (_previousStatusBarStyle != -1) {
// [[UIApplication sharedApplication] setStatusBarStyle:_previousStatusBarStyle];
// }
// }
// _previousStatusBarStyle = -1; // this value was reset before reapplying it. caused statusbar to stay black on ios7
}
@end
#pragma mark CDVInAppBrowserViewController
@implementation CDVInAppBrowserViewController
@synthesize currentURL;
- (id)initWithUserAgent:(NSString*)userAgent prevUserAgent:(NSString*)prevUserAgent browserOptions: (CDVInAppBrowserOptions*) browserOptions
{
self = [super init];
if (self != nil) {
_userAgent = userAgent;
_prevUserAgent = prevUserAgent;
_browserOptions = browserOptions;
#ifdef __CORDOVA_4_0_0
_webViewDelegate = [[CDVUIWebViewDelegate alloc] initWithDelegate:self];
#else
_webViewDelegate = [[CDVWebViewDelegate alloc] initWithDelegate:self];
#endif
[self createViews];
}
return self;
}
- (BOOL)bannerView:(UIWebView*)webview shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{
if (navigationType == UIWebViewNavigationTypeLinkClicked ) {
[[UIApplication sharedApplication] openURL:[request URL]];
return NO;
}
return YES;
}
// Prevent crashes on closing xs
-(void)dealloc {
self.webView.delegate = nil;
}
- (UIColor *)getUIColorObjectFromHexString:(NSString *)hexStr alpha:(CGFloat)alpha
{
// Convert hex string to an integer
unsigned int hexint = [self intFromHexString:hexStr];
// Create color object, specifying alpha as well
UIColor *color =
[UIColor colorWithRed:((CGFloat) ((hexint & 0xFF0000) >> 16))/255
green:((CGFloat) ((hexint & 0xFF00) >> 8))/255
blue:((CGFloat) (hexint & 0xFF))/255
alpha:alpha];
return color;
}
- (unsigned int)intFromHexString:(NSString *)hexStr
{
unsigned int hexInt = 0;
// Create scanner
NSScanner *scanner = [NSScanner scannerWithString:hexStr];
// Tell scanner to skip the # character
[scanner setCharactersToBeSkipped:[NSCharacterSet characterSetWithCharactersInString:@"#"]];
// Scan hex value
[scanner scanHexInt:&hexInt];
return hexInt;
}
- (void)createViews
{
// We create the views in code for primarily for ease of upgrades and not requiring an external .xib to be included
// CGFloat labelInset = 5.0;
// float locationBarY = toolbarIsAtBottom ? self.view.bounds.size.height - FOOTER_HEIGHT : self.view.bounds.size.height - LOCATIONBAR_HEIGHT;
//
// BOOL toolbarIsAtBottom = ![_browserOptions.toolbarposition isEqualToString:kInAppBrowserToolbarBarPositionTop];
// ****** WEBVIEW ********
CGRect webViewBounds = self.view.bounds;
webViewBounds.size.height = self.view.bounds.size.height;
self.webView = [[UIWebView alloc] initWithFrame:webViewBounds];
self.webView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
self.webView.delegate = _webViewDelegate;
self.webView.backgroundColor = [UIColor whiteColor];
self.webView.clearsContextBeforeDrawing = YES;
self.webView.clipsToBounds = YES;
self.webView.contentMode = UIViewContentModeScaleToFill;
self.webView.multipleTouchEnabled = YES;
self.webView.opaque = NO;
self.webView.scalesPageToFit = NO;
self.webView.userInteractionEnabled = YES;
// ****** SPINNER ********
self.spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
self.spinner.alpha = 1.000;
self.spinner.autoresizesSubviews = YES;
self.spinner.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleRightMargin);
self.spinner.clearsContextBeforeDrawing = NO;
self.spinner.clipsToBounds = NO;
self.spinner.contentMode = UIViewContentModeScaleToFill;
self.spinner.frame = CGRectMake(CGRectGetMidX(self.webView.frame) - 44.0f, CGRectGetMidY(self.webView.frame), 24.0, 24.0);
self.spinner.hidden = NO;
self.spinner.hidesWhenStopped = YES;
self.spinner.multipleTouchEnabled = NO;
self.spinner.opaque = NO;
self.spinner.tintColor = [UIColor blackColor];
self.spinner.userInteractionEnabled = NO;
[self.spinner stopAnimating];
// ****** LOADING LABEL ********
self.loadingLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMidX(self.webView.frame) - 30.0f , CGRectGetMidY(self.webView.frame), 100.0f, 24.0f)];
self.loadingLabel.textAlignment = NSTextAlignmentCenter;
self.loadingLabel.font =[UIFont systemFontOfSize:14.0f];
self.loadingLabel.numberOfLines = 1;
self.loadingLabel.lineBreakMode = NSLineBreakByTruncatingTail;
self.loadingLabel.tintColor = [UIColor blackColor];
self.loadingLabel.text = @"carregando";
// ****** TOOLBAR TOP ********
CGRect toolbarFrameTop = CGRectMake(0.0, 0.0, self.view.bounds.size.width, 60.0f);
self.toolbarTop = [[UIToolbar alloc] initWithFrame:toolbarFrameTop];
// ****** BUTTONS ********
self.btnImage = [[UIImage imageNamed:@"ios7-arrow-left.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
self.backButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.backButton.backgroundColor = [UIColor redColor];
self.backButton.frame = CGRectMake(0.0f, 0.0f, 36.0f, 36.0f);
[self.backButton addTarget:self action:@selector(close) forControlEvents:UIControlEventTouchDown];
// ****** TITLE ********
self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 20, 10, 44.0f)];
self.titleLabel.textAlignment = NSTextAlignmentCenter;
self.titleLabel.font =[UIFont boldSystemFontOfSize:17.0f];
self.titleLabel.numberOfLines = 1;
self.titleLabel.lineBreakMode = NSLineBreakByTruncatingTail;
self.bannerButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.bannerButton.frame = CGRectMake(0.0f, 0.0f, self.view.bounds.size.width, self.view.bounds.size.width / 6.4f);
// self.bannerButton.backgroundColor = [UIColor whiteColor];
[self.bannerButton addTarget:self action:@selector(openBanner) forControlEvents:UIControlEventTouchDown];
NSString* _htmlCon = [NSString stringWithFormat:@"%@%@%@", @"<html><head><title></title></head><body style=\"background-color:'#333'; margin:0px; padding:0px;\"><img src=\"" , _optBanner , @"\" style=\"width:100%;\" /></body></html>"];
NSMutableString *html = [NSMutableString stringWithString:_htmlCon] ;
CGRect bannerViewBounds = self.view.bounds;
bannerViewBounds.size.height = self.view.bounds.size.width / 6.4f;
self.bannerView = [[UIWebView alloc] initWithFrame:bannerViewBounds];
self.bannerView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
self.bannerView.delegate = _webViewBannerDelegate;
self.bannerView.backgroundColor = [UIColor whiteColor];
self.bannerView.clearsContextBeforeDrawing = YES;
self.bannerView.clipsToBounds = YES;
self.bannerView.contentMode = UIViewContentModeScaleToFill;
self.bannerView.multipleTouchEnabled = YES;
self.bannerView.opaque = NO;
self.bannerView.scalesPageToFit = NO;
self.bannerView.userInteractionEnabled = YES;
[self.bannerView loadHTMLString:[html description] baseURL:nil];
// [self.bannerView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com"]]];
// ****** SETTERS ********
[self.toolbarTop addSubview:self.titleLabel];
[self.toolbarTop addSubview:self.backButton];
[self.view addSubview:self.webView];
[self.view addSubview:self.spinner];
[self.view addSubview:self.loadingLabel];
[self.view sendSubviewToBack:self.webView];
if(_optBanner != nil){
[self.view addSubview:self.bannerView];
[self.view addSubview:self.bannerButton];
}
// [self.view sendSubviewToBack:self.bannerView];
// [self.toolbarTop setItems:@[self.closeButton]];
self.view.backgroundColor = [UIColor grayColor];
[self.view addSubview:self.toolbarTop];
}
- (void) setWebViewFrame : (CGRect) frame {
NSLog(@"Setting the WebView's frame to %@", NSStringFromCGRect(frame));
[self.webView setFrame:frame];
}
- (void)setCloseButtonTitle:(NSString*)title
{
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)viewDidUnload
{
[self.webView loadHTMLString:nil baseURL:nil];
[CDVUserAgentUtil releaseLock:&_userAgentLockToken];
[super viewDidUnload];
}
- (UIStatusBarStyle)preferredStatusBarStyle
{
return UIStatusBarStyleDefault;
}
- (void)actionSheet:(UIActionSheet *)popup clickedButtonAtIndex:(NSInteger)buttonIndex {
switch (popup.tag) {
case 1: {
switch (buttonIndex) {
case 0:
[self intentShare];
break;
case 1:
[self openInBrowser];
break;
}
break;
}
default:
break;
}
}
- (void)intentShare{
NSMutableArray *sharingItems = [NSMutableArray new];
[sharingItems addObject:[NSURL URLWithString:self.webView.request.URL.absoluteString]];
UIActivityViewController *activityController = [[UIActivityViewController alloc] initWithActivityItems:sharingItems applicationActivities:nil];
[self presentViewController:activityController animated:YES completion:nil];
}
- (void)openInBrowser{
NSURL *url = [NSURL URLWithString:self.webView.request.URL.absoluteString];
if (![[UIApplication sharedApplication] openURL:url]) {
NSLog(@"%@%@",@"Failed to open url:",[url description]);
}
}
- (void) share {
UIActionSheet *popup = [[UIActionSheet alloc] initWithTitle:@"Opções:" delegate:self cancelButtonTitle:@"Voltar" destructiveButtonTitle:nil otherButtonTitles:
@"Compartilhar este link",
@"Abrir no navegador",
nil];
popup.tag = 1;
[popup showInView:self.view];
}
- (void)refresh{
[self.webView reload];
}
- (void)openBanner
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:_optUrlBanner]];
}
- (void)close
{
[CDVUserAgentUtil releaseLock:&_userAgentLockToken];
self.currentURL = nil;
if ((self.navigationDelegate != nil) && [self.navigationDelegate respondsToSelector:@selector(browserExit)]) {
[self.navigationDelegate browserExit];
}
__weak UIViewController* weakSelf = self;
// Run later to avoid the "took a long time" log message.
dispatch_async(dispatch_get_main_queue(), ^{
if ([weakSelf respondsToSelector:@selector(presentingViewController)]) {
CATransition *transition = [CATransition animation];
transition.duration = 0.3;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionPush;
transition.subtype = kCATransitionFromLeft;
[self.webView.window.layer addAnimation:transition forKey:nil];
[[weakSelf presentingViewController] dismissViewControllerAnimated:NO completion:nil];
} else {
CATransition *transition = [CATransition animation];
transition.duration = 0.3;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionPush;
transition.subtype = kCATransitionFromLeft;
[self.webView.window.layer addAnimation:transition forKey:nil];
[[weakSelf parentViewController] dismissViewControllerAnimated:NO completion:nil];
}
});
}
-(void)dismissViewControllerAnimated:(BOOL)flag completion:(void (^)(void))completion
{
if ( self.presentedViewController)
{
[super dismissViewControllerAnimated:flag completion:completion];
}
}
- (void)navigateTo:(NSURL*)url background:(NSString*) background color:(NSString*) color title:(NSString*) title loading:(NSString*) loading
{
NSURLRequest* request = [NSURLRequest requestWithURL:url];
UIColor *myBackground = [self getUIColorObjectFromHexString:background alpha:1];
UIColor *myColor = [self getUIColorObjectFromHexString:color alpha:1];
self.toolbarTop.barTintColor= myBackground;
self.backButton.tintColor = myColor;
[self.backButton setBackgroundImage:self.btnImage forState:UIControlStateNormal];
self.view.backgroundColor = myBackground;
[[UINavigationBar appearance] setBarTintColor:myColor];
self.loadingLabel.text = loading;
self.titleLabel.text = title;
self.titleLabel.textColor = myColor;
self.closeButton.tintColor = myColor;
if (_userAgentLockToken != 0) {
[self.webView loadRequest:request];
} else {
__weak CDVInAppBrowserViewController* weakSelf = self;
[CDVUserAgentUtil acquireLock:^(NSInteger lockToken) {
_userAgentLockToken = lockToken;
[CDVUserAgentUtil setUserAgent:_userAgent lockToken:lockToken];
[weakSelf.webView loadRequest:request];
}];
}
}
- (void)goBack:(id)sender
{
[self.webView goBack];
}
- (void)goForward:(id)sender
{
[self.webView goForward];
}
- (void)viewWillAppear:(BOOL)animated
{
if (IsAtLeastiOSVersion(@"7.0")) {
[[UIApplication sharedApplication] setStatusBarStyle:[self preferredStatusBarStyle]];
}
[self rePositionViews];
//
[super viewWillAppear:animated];
}
//
// On iOS 7 the status bar is part of the view's dimensions, therefore it's height has to be taken into account.
// The height of it could be hardcoded as 20 pixels, but that would assume that the upcoming releases of iOS won't
// change that value.
//
- (float) getStatusBarOffset {
CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame];
float statusBarOffset = IsAtLeastiOSVersion(@"7.0") ? MIN(statusBarFrame.size.width, statusBarFrame.size.height) : 0.0;
return statusBarOffset;
}
- (void) rePositionViews {
[self.webView setFrame:CGRectMake(0.0f, 44.0f, self.view.bounds.size.width , self.view.bounds.size.height - (44.0f + (self.view.bounds.size.width / 6.4f)))];
if(_optBanner != nil){
[self.webView setFrame:CGRectMake(0.0f, 44.0f, self.view.bounds.size.width , self.view.bounds.size.height - (44.0f + (self.view.bounds.size.width / 6.4f)))];
}
else {
[self.webView setFrame:CGRectMake(0.0f, 44.0f, self.view.bounds.size.width , self.view.bounds.size.height - 44.0f)];
}
[self.toolbarTop setFrame:CGRectMake(0.0f , 0.0f, self.view.bounds.size.width, 64.0f)];
[self.titleLabel setFrame:CGRectMake(floorf((self.webView.frame.size.width - 100.0f) / 2.0f) , 20.0f,
100.0f, 44.0f)];
[self.backButton setFrame:CGRectMake( 0.0f , 20.0f, 36.0f, 36.0f)];
[self.bannerView setFrame:CGRectMake(0.0f , 44.0f + self.webView.frame.size.height , self.view.bounds.size.width, (self.view.bounds.size.width / 6.4f))];
[self.bannerButton setFrame:CGRectMake(0.0f , 44.0f + self.webView.frame.size.height , self.view.bounds.size.width, (self.view.bounds.size.width / 6.4f))];
// if ([_browserOptions.toolbarposition isEqualToString:kInAppBrowserToolbarBarPositionTop]) {
//
//
//
// [self.toolbar setFrame:CGRectMake(self.toolbar.frame.origin.x, [self getStatusBarOffset], self.toolbar.frame.size.width, self.toolbar.frame.size.height)];
// }
//
}
#pragma mark UIWebViewDelegate
- (void)webViewDidStartLoad:(UIWebView*)theWebView
{
// loading url, start spinner, update back/forward
self.backButton.enabled = theWebView.canGoBack;
self.forwardButton.enabled = theWebView.canGoForward;
[[UIApplication sharedApplication] setStatusBarHidden:NO];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
NSDictionary* itemTextAttributesBlue = @{NSFontAttributeName:[UIFont systemFontOfSize:24.0f], NSForegroundColorAttributeName:[UIColor colorWithRed:19/255.0 green:144/255.0 blue:255/255.0 alpha:1.0], NSBackgroundColorAttributeName:[UIColor lightGrayColor]};
NSDictionary* itemTextAttributesGray = @{NSFontAttributeName:[UIFont systemFontOfSize:24.0f], NSForegroundColorAttributeName:[UIColor lightGrayColor], NSBackgroundColorAttributeName:[UIColor lightGrayColor]};
if(theWebView.canGoForward){
[self.forwardButton setTitleTextAttributes:itemTextAttributesBlue forState:UIControlStateNormal];
}
else{
[self.forwardButton setTitleTextAttributes:itemTextAttributesGray forState:UIControlStateNormal];
}
[self.spinner startAnimating];
return [self.navigationDelegate webViewDidStartLoad:theWebView];
}
- (BOOL)webView:(UIWebView*)theWebView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{
BOOL isTopLevelNavigation = [request.URL isEqual:[request mainDocumentURL]];
if (isTopLevelNavigation) {
self.currentURL = request.URL;
}
return [self.navigationDelegate webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType];
}
- (void)webViewDidFinishLoad:(UIWebView*)theWebView
{
// update url, stop spinner, update back/forward
// self.backButton.enabled = theWebView.canGoBack;
self.forwardButton.enabled = theWebView.canGoForward;
NSDictionary* itemTextAttributesBlue = @{NSFontAttributeName:[UIFont systemFontOfSize:24.0f], NSForegroundColorAttributeName:[UIColor colorWithRed:19/255.0 green:144/255.0 blue:255/255.0 alpha:1.0], NSBackgroundColorAttributeName:[UIColor lightGrayColor]};
NSDictionary* itemTextAttributesGray = @{NSFontAttributeName:[UIFont systemFontOfSize:24.0f], NSForegroundColorAttributeName:[UIColor lightGrayColor], NSBackgroundColorAttributeName:[UIColor lightGrayColor]};
if(theWebView.canGoForward){
[self.forwardButton setTitleTextAttributes:itemTextAttributesBlue forState:UIControlStateNormal];
}
else{
[self.forwardButton setTitleTextAttributes:itemTextAttributesGray forState:UIControlStateNormal];
}
[self.spinner stopAnimating];
self.loadingLabel.hidden = YES;
// Work around a bug where the first time a PDF is opened, all UIWebViews
// reload their User-Agent from NSUserDefaults.
// This work-around makes the following assumptions:
// 1. The app has only a single Cordova Webview. If not, then the app should
// take it upon themselves to load a PDF in the background as a part of
// their start-up flow.
// 2. That the PDF does not require any additional network requests. We change
// the user-agent here back to that of the CDVViewController, so requests
// from it must pass through its white-list. This *does* break PDFs that
// contain links to other remote PDF/websites.
// More info at https://issues.apache.org/jira/browse/CB-2225
BOOL isPDF = [@"true" isEqualToString :[theWebView stringByEvaluatingJavaScriptFromString:@"document.body==null"]];
if (isPDF) {
[CDVUserAgentUtil setUserAgent:_prevUserAgent lockToken:_userAgentLockToken];
}
[self.navigationDelegate webViewDidFinishLoad:theWebView];
}
- (void)webView:(UIWebView*)theWebView didFailLoadWithError:(NSError*)error
{
// log fail message, stop spinner, update back/forward
NSLog(@"webView:didFailLoadWithError - %ld: %@", (long)error.code, [error localizedDescription]);
// self.backButton.enabled = theWebView.canGoBack;
self.forwardButton.enabled = theWebView.canGoForward;
[self.spinner stopAnimating];
self.loadingLabel.hidden = YES;
[self.navigationDelegate webView:theWebView didFailLoadWithError:error];
}
#pragma mark CDVScreenOrientationDelegate
- (BOOL)shouldAutorotate
{
if ((self.orientationDelegate != nil) && [self.orientationDelegate respondsToSelector:@selector(shouldAutorotate)]) {
return [self.orientationDelegate shouldAutorotate];
}
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
if ((self.orientationDelegate != nil) && [self.orientationDelegate respondsToSelector:@selector(supportedInterfaceOrientations)]) {
return [self.orientationDelegate supportedInterfaceOrientations];
}
return 1 << UIInterfaceOrientationPortrait;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if ((self.orientationDelegate != nil) && [self.orientationDelegate respondsToSelector:@selector(shouldAutorotateToInterfaceOrientation:)]) {
return [self.orientationDelegate shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}
return YES;
}
@end
@implementation CDVInAppBrowserOptions
- (id)init
{
if (self = [super init]) {
// default values
self.location = YES;
self.toolbar = YES;
self.closebuttoncaption = nil;
self.clearcache = NO;
self.clearsessioncache = NO;
self.enableviewportscale = NO;
self.mediaplaybackrequiresuseraction = NO;
self.allowinlinemediaplayback = NO;
self.keyboarddisplayrequiresuseraction = YES;
self.suppressesincrementalrendering = NO;
self.hidden = NO;
self.disallowoverscroll = NO;
}
return self;
}
+ (CDVInAppBrowserOptions*)parseOptions:(NSString*)options
{
CDVInAppBrowserOptions* obj = [[CDVInAppBrowserOptions alloc] init];
// NOTE: this parsing does not handle quotes within values
NSArray* pairs = [options componentsSeparatedByString:@","];
// parse keys and values, set the properties
for (NSString* pair in pairs) {
NSArray* keyvalue = [pair componentsSeparatedByString:@"="];
if ([keyvalue count] == 2) {
NSString* key = [[keyvalue objectAtIndex:0] lowercaseString];
NSString* value = [keyvalue objectAtIndex:1];
NSString* value_lc = [value lowercaseString];
BOOL isBoolean = [value_lc isEqualToString:@"yes"] || [value_lc isEqualToString:@"no"];
NSNumberFormatter* numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setAllowsFloats:YES];
BOOL isNumber = [numberFormatter numberFromString:value_lc] != nil;
// set the property according to the key name
if ([obj respondsToSelector:NSSelectorFromString(key)]) {
if (isNumber) {
[obj setValue:[numberFormatter numberFromString:value_lc] forKey:key];
} else if (isBoolean) {
[obj setValue:[NSNumber numberWithBool:[value_lc isEqualToString:@"yes"]] forKey:key];
} else {
[obj setValue:value forKey:key];
}
}
}
}
return obj;
}
@end
@implementation CDVInAppBrowserNavigationController : UINavigationController
- (void) viewDidLoad {
CGRect frame = [UIApplication sharedApplication].statusBarFrame;
// simplified from: http://stackoverflow.com/a/25669695/219684
// UIToolbar* bgToolbar = [[UIToolbar alloc] initWithFrame:frame];
//
// bgToolbar.tintColor = [UIColor redColor];
// bgToolbar.barStyle = UIBarStyleDefault;
// [self.view addSubview:bgToolbar];
[super viewDidLoad];
}
-(void)dismissViewControllerAnimated:(BOOL)flag completion:(void (^)(void))completion
{
if ( self.presentedViewController)
{
[super dismissViewControllerAnimated:flag completion:completion];
}
}
#pragma mark CDVScreenOrientationDelegate
- (BOOL)shouldAutorotate
{
if ((self.orientationDelegate != nil) && [self.orientationDelegate respondsToSelector:@selector(shouldAutorotate)]) {
return [self.orientationDelegate shouldAutorotate];
}
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
if ((self.orientationDelegate != nil) && [self.orientationDelegate respondsToSelector:@selector(supportedInterfaceOrientations)]) {
return [self.orientationDelegate supportedInterfaceOrientations];
}
return 1 << UIInterfaceOrientationPortrait;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if ((self.orientationDelegate != nil) && [self.orientationDelegate respondsToSelector:@selector(shouldAutorotateToInterfaceOrientation:)]) {
return [self.orientationDelegate shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}
return YES;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment