ChildBrowser Cordova 1.5 Support

ChildBrowser Cordova 1.5 Support
This commit is contained in:
Randy McMillan
2012-03-04 16:14:33 -05:00
parent 2dc6525b2c
commit acc439df9a
6 changed files with 143 additions and 70 deletions

View File

@@ -1,7 +1,7 @@
/* MIT licensed */
// (c) 2010 Jesse MacFadyen, Nitobi
/*global PhoneGap */
/*global PhoneGap/Cordova */
function ChildBrowser() {
// Does nothing
@@ -43,13 +43,31 @@ ChildBrowser._onJSCallback = function(js,loc)
// Show a webpage, will result in a callback to onLocationChange
ChildBrowser.prototype.showWebPage = function(loc)
{
PhoneGap.exec("ChildBrowserCommand.showWebPage", loc);
if (typeof PhoneGap !=== "undefined") {
PhoneGap.exec("ChildBrowserCommand.showWebPage", loc);
} else {
if (typeof Cordova !=== "undefined") {
Cordova.exec("ChildBrowserCommand.showWebPage", loc);
}
};
// close the browser, will NOT result in close callback
ChildBrowser.prototype.close = function()
{
PhoneGap.exec("ChildBrowserCommand.close");
if (typeof PhoneGap !=== "undefined") {
PhoneGap.exec("ChildBrowserCommand.close");
} else {
if (typeof Cordova !=== "undefined") {
Cordova.exec("ChildBrowserCommand.close");
}
};
// Not Implemented

View File

@@ -1,23 +1,25 @@
//
// PhoneGap ! ChildBrowserCommand
//
//
// Created by Jesse MacFadyen on 10-05-29.
// Copyright 2010 Nitobi. All rights reserved.
//
// Copyright 2012, Randy McMillan
// Continued maintainance @RandyMcMillan 2010/2011/2012
#import <Foundation/Foundation.h>
#ifdef PHONEGAP_FRAMEWORK
#import <PhoneGap/PGPlugin.h>
#else
#import "PGPlugin.h"
#import <PhoneGap/PGPlugin.h>
#endif
//#else
#ifdef CORDOVA_FRAMEWORK
#import <CORDOVA/CDVPlugin.h>
#endif
#import "ChildBrowserViewController.h"
@interface ChildBrowserCommand : PGPlugin <ChildBrowserDelegate> {
#ifdef PHONEGAP_FRAMEWORK
@interface ChildBrowserCommand : PGPlugin <ChildBrowserDelegate> {
#endif
#ifdef CORDOVA_FRAMEWORK
@interface ChildBrowserCommand : CDVPlugin <ChildBrowserDelegate> {
#endif
ChildBrowserViewController* childBrowser;
}

View File

@@ -1,19 +1,16 @@
//
//
//
// Created by Jesse MacFadyen on 10-05-29.
// Copyright 2010 Nitobi. All rights reserved.
// Copyright (c) 2011, IBM Corporation
// Copyright 2011, Randy McMillan
//
// Copyright 2012, Randy McMillan
// Continued maintainance @RandyMcMillan 2010/2011/2012
#import "ChildBrowserCommand.h"
#ifdef PHONEGAP_FRAMEWORK
#import <PhoneGap/PhoneGapViewController.h>
#else
#import "PhoneGapViewController.h"
#endif
//#else
#ifdef CORDOVA_FRAMEWORK
#import <Cordova/CDVViewController.h>
#endif
@@ -23,8 +20,7 @@
- (void) showWebPage:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options // args: url
{
if(childBrowser == NULL)
if(childBrowser == NULL)
{
childBrowser = [[ ChildBrowserViewController alloc ] initWithScale:FALSE ];
childBrowser.delegate = self;
@@ -34,25 +30,28 @@
NSString* strOrientations = [ options objectForKey:@"supportedOrientations"];
NSArray* supportedOrientations = [strOrientations componentsSeparatedByString:@","];
*/
PhoneGapViewController* cont = (PhoneGapViewController*)[ super appViewController ];
childBrowser.supportedOrientations = cont.supportedOrientations;
#ifdef PHONEGAP_FRAMEWORK
PhoneGapViewController* cont = (PhoneGapViewController*)[ super appViewController ];
childBrowser.supportedOrientations = cont.supportedOrientations;
[ cont presentModalViewController:childBrowser animated:YES ];
#endif
if ([cont respondsToSelector:@selector(presentViewController)]) {
//Reference UIViewController.h Line:179 for update to iOS 5 difference - @RandyMcMillan
[cont presentViewController:childBrowser animated:YES completion:nil];
} else {
[ cont presentModalViewController:childBrowser animated:YES ];
}
NSString *url = (NSString*) [arguments objectAtIndex:0];
[childBrowser loadURL:url ];
#ifdef CORDOVA_FRAMEWORK
CDVViewController* cont = (CDVViewController*)[ super viewController ];
childBrowser.supportedOrientations = cont.supportedOrientations;
[ cont presentModalViewController:childBrowser animated:YES ];
#endif
NSString *url = (NSString*) [arguments objectAtIndex:0];
[childBrowser loadURL:url ];
}
-(void) close:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options // args: url
{
[ childBrowser closeBrowser];
[ childBrowser closeBrowser];
}

View File

@@ -1,9 +1,7 @@
//
// ChildBrowserViewController.h
//
// Created by Jesse MacFadyen on 21/07/09.
// Copyright 2009 Nitobi. All rights reserved.
//
// Created by Jesse MacFadyen on 10-05-29.
// Copyright 2010 Nitobi. All rights reserved.
// Copyright 2012, Randy McMillan
// Continued maintainance @RandyMcMillan 2010/2011/2012
#import <UIKit/UIKit.h>

View File

@@ -1,11 +1,7 @@
//
// ChildBrowserViewController.m
//
// Created by Jesse MacFadyen on 21/07/09.
// Copyright 2009 Nitobi. All rights reserved.
// Copyright (c) 2011, IBM Corporation
// Copyright 2011, Randy McMillan
//
/// Created by Jesse MacFadyen on 10-05-29.
// Copyright 2010 Nitobi. All rights reserved.
// Copyright 2012, Randy McMillan
// Continued maintainance @RandyMcMillan 2010/2011/2012
#import "ChildBrowserViewController.h"
@@ -135,7 +131,7 @@
if(isImage)
{
NSURL* pURL = [[ [NSURL alloc] initWithString:imageURL ] autorelease];
NSURL* pURL = [ [NSURL alloc] initWithString:imageURL ];
[ [ UIApplication sharedApplication ] openURL:pURL ];
}
else
@@ -219,21 +215,5 @@
}
- (void)webView:(UIWebView *)wv didFailLoadWithError:(NSError *)error {
NSLog (@"webView:didFailLoadWithError");
[spinner stopAnimating];
addressLabel.text = @"Failed";
if (error != NULL) {
UIAlertView *errorAlert = [[UIAlertView alloc]
initWithTitle: [error localizedDescription]
message: [error localizedFailureReason]
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[errorAlert show];
[errorAlert release];
}
}
@end

View File

@@ -0,0 +1,76 @@
UPDATED March 2 2012 for Cordova 1.5 with limited backwards support for PhoneGap 1.4.1 (minor changes may be needed for anything earlier than 1.3) - @RandyMcMillan
=================================
The child browser allows you to display external webpages within your PhoneGap/Cordova application.
A simple use case would be:
- Users can follow links/buttons to view web content without leaving your app.
- Display web pages/images/videos/pdfs in the ChildBrowser.
This command creates a popup browser that is shown in front of your app, when the user presses the done button they are simply returned to your app ( actually they never left )
The ChildBrowser has buttons for refreshing, navigating back + forwards, as well as the option to open in Safari.
Note, because this is open source, I could not include the graphics I usually use for the back/forward and safari buttons. I have changed the XIB file to use system buttons ( rewind / fast-forward + action ) Ideally you should modify the XIB to use your own look.
Here is a sample command to open google in a ChildBrowser :
PhoneGap.exec("ChildBrowserCommand.showWebPage", "http://www.google.com" );
or
Cordova.exec("ChildBrowserCommand.showWebPage", "http://www.google.com" );
=================================
June 1, 2010
Added support for orientations, supportedOrientations are passed through to the child view controller. -jm
================================
Sept 13, 2010
+ added callbacks for location change, close, opening in safari,
+ added method to close the browser from js.
( This should allow easy additions for facebook connect as you can monitor the browser's address and respond accordingly. )
+ added images to the XIB, these need to be attached as resources in your xcode project.
Sample use:
<code>
var root = this;
/* When this function is called, PhoneGap has been initialized and is ready to roll */
function onDeviceReady()
{
var cb = ChildBrowser.install();
if(cb != null)
{
cb.onLocationChange = function(loc){ root.locChanged(loc); };
cb.onClose = function(){root.onCloseBrowser()};
cb.onOpenExternal = function(){root.onOpenExternal();};
window.plugins.childBrowser.showWebPage("http://google.com");
}
}
function onCloseBrowser()
{
alert("In index.html child browser closed");
}
function locChanged(loc)
{
alert("In index.html new loc = " + loc);
}
function onOpenExternal()
{
alert("In index.html onOpenExternal");
}
</code>