Skip to content

Commit

Permalink
Removed "online" mode, and cleaned up the source tree a little
Browse files Browse the repository at this point in the history
  • Loading branch information
NachoMan committed May 18, 2009
1 parent 582d67b commit 6cceade
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 140 deletions.
3 changes: 2 additions & 1 deletion iphone/Classes/Device.h
Expand Up @@ -11,7 +11,8 @@
#import "PhoneGapCommand.h"

@interface Device : PhoneGapCommand {
UIDevice *myCurrentDevice;
}

- (NSDictionary*) getDeviceProperties;

@end
28 changes: 12 additions & 16 deletions iphone/Classes/Device.m
Expand Up @@ -10,27 +10,23 @@

@implementation Device

/*
* init
* returns a JS String with various device settings
/**
* returns a dictionary with various device settings
* - gap (version)
* - Device model
* - Device version
* - Device uuid
*/
- (NSString *)init{
myCurrentDevice = [UIDevice currentDevice];
return [[NSString alloc]
initWithFormat:@"DeviceInfo={platform:'%s',version:'%s',uuid:'%s',gap:'0.8.0'};",
[[myCurrentDevice model] UTF8String],
[[myCurrentDevice systemVersion] UTF8String],
[[myCurrentDevice uniqueIdentifier] UTF8String]
];
}

- (void)dealloc {
[myCurrentDevice release];
[super dealloc];
- (NSDictionary*) getDeviceProperties
{
UIDevice *device = [UIDevice currentDevice];
NSMutableDictionary *devProps = [[NSMutableDictionary dictionaryWithCapacity:4] autorelease];
[devProps setObject:[device model] forKey:@"platform"];
[devProps setObject:[device systemVersion] forKey:@"systemVersion"];
[devProps setObject:[device uniqueIdentifier] forKey:@"uuid"];
[devProps setObject:[device systemName] forKey:@"systemName"];
[devProps setObject:[device name] forKey:@"deviceName"];
return [[NSDictionary dictionaryWithDictionary:devProps] autorelease];
}

@end
20 changes: 1 addition & 19 deletions iphone/Classes/PhoneGapDelegate.h
Expand Up @@ -21,49 +21,31 @@
UIApplicationDelegate,
UIWebViewDelegate,
UIAccelerometerDelegate,
//UIImagePickerControllerDelegate,
UIPickerViewDelegate,
UINavigationControllerDelegate
>
{

IBOutlet UIWindow *window;
IBOutlet PhoneGapViewController *viewController;
IBOutlet UIWebView *webView;
IBOutlet PhoneGapViewController *viewController;

IBOutlet UIImageView *imageView;
IBOutlet UIActivityIndicatorView *activityView;

UIImagePickerController *picker; // added by urbian
NSString *photoUploadUrl; // added by urbian
NSString *lastUploadedPhoto; // added by urbian
NSURLConnection *conn; // added by urbian
NSMutableData *receivedData; // added by urbian

//UIImagePickerController *imagePickerController;

UIInterfaceOrientation orientationType;
NSDictionary *settings;

NSURLConnection *callBackConnection;
Sound *sound;
Contacts *contacts;
Console *console;
NSURL* appURL;

NSMutableDictionary *commandObjects;
}

@property (nonatomic, retain) UIWindow *window;
@property (nonatomic, retain) PhoneGapViewController *viewController;
//@property (nonatomic, retain) UIImagePickerController *imagePickerController;
@property (nonatomic, retain) UIActivityIndicatorView *activityView;
@property (nonatomic, retain) NSMutableDictionary *commandObjects;
@property (nonatomic, retain) NSDictionary *settings;

//- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image2 editingInfo:(NSDictionary *)editingInfo;
//- (void) imagePickerControllerDidCancel:(UIImagePickerController *)picker;

-(id) getCommandInstance:(NSString*)className;
- (NSDictionary*)getBundlePlist:(NSString *)plistName;

Expand Down
142 changes: 59 additions & 83 deletions iphone/Classes/PhoneGapDelegate.m
Expand Up @@ -10,8 +10,6 @@ @implementation PhoneGapDelegate
@synthesize commandObjects;
@synthesize settings;

//@synthesize imagePickerController;

- (id) init
{
self = [super init];
Expand All @@ -21,6 +19,9 @@ - (id) init
return self;
}

/**
Returns an instance of a PhoneGapCommand object, based on its name. If one exists already, it is returned.
*/
-(id) getCommandInstance:(NSString*)className
{
id obj = [commandObjects objectForKey:className];
Expand All @@ -39,8 +40,7 @@ -(id) getCommandInstance:(NSString*)className
return obj;
}

/*
* applicationDidFinishLaunching
/**
* This is main kick off after the app inits, the views and Settings are setup here.
*/
- (void)applicationDidFinishLaunching:(UIApplication *)application
Expand All @@ -54,8 +54,6 @@ - (void)applicationDidFinishLaunching:(UIApplication *)application
NSDictionary *temp = [self getBundlePlist:@"PhoneGap"];
settings = [[NSDictionary alloc] initWithDictionary:temp];

NSNumber *offline = [settings objectForKey:@"Offline"];
NSString *url = [settings objectForKey:@"Callback"];
NSNumber *detectNumber = [settings objectForKey:@"DetectPhoneNumber"];
NSNumber *useLocation = [settings objectForKey:@"UseLocation"];
NSNumber *useAccelerometer = [settings objectForKey:@"EnableAcceleration"];
Expand All @@ -73,51 +71,21 @@ - (void)applicationDidFinishLaunching:(UIApplication *)application

webView.delegate = self;

/*
// Set up the image picker controller and add it to the view
imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.delegate = self;
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePickerController.view.hidden = YES;
*/

if ([useAccelerometer boolValue]) {
[[UIAccelerometer sharedAccelerometer] setUpdateInterval:1.0/40.0];
[[UIAccelerometer sharedAccelerometer] setDelegate:self];
}

[window addSubview:viewController.view];

/*
* We want to test the offline to see if this app should start in offline mode or online mode.
*
* YES - Offline
* NO - Online
*
* In Offline mode the index.html file is loaded from the www directly and serves as the entry point into the application
* In Online mode the starting point is a external FQDN, usually your server.
*/
if ([offline boolValue]) {
NSBundle * thisBundle = [NSBundle bundleForClass:[self class]];
appURL = [[NSURL fileURLWithPath:[thisBundle pathForResource:@"index" ofType:@"html" inDirectory:@"www"]] retain];
} else {
appURL = [[NSURL URLWithString:url] retain];
}

/*
* webView
* This is where we define the inital instance of the browser (WebKit) and give it a starting url/file.
*/
[webView loadRequest:[NSURLRequest
requestWithURL:appURL
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:20.0
]];
NSURL *appURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"index" ofType:@"html" inDirectory:@"www"]];
NSURLRequest *appReq = [NSURLRequest requestWithURL:appURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20.0];
[webView loadRequest:appReq];

/*
* detectNumber - If we want to Automagically convery phone numbers to links - Set in PhoneGap.plist
* Value should be BOOL (YES|NO)
*/
webView.detectsPhoneNumbers = [detectNumber boolValue];

/*
Expand Down Expand Up @@ -150,7 +118,7 @@ - (void)applicationDidFinishLaunching:(UIApplication *)application
}
[[UIApplication sharedApplication] setStatusBarOrientation:orientationType animated:NO];

/*
/*
* rotateOrientation - This option is only enabled when AutoRotate is enabled. If the phone is still rotated
* when AutoRotate is disabled, this will control what orientations will be rotated to. If you wish your app to
* only use landscape or portrait orientations, change the value in PhoneGap.plist to indicate that.
Expand Down Expand Up @@ -183,14 +151,13 @@ - (void)applicationDidFinishLaunching:(UIApplication *)application
}


/*
* When web application loads Add stuff to the DOM (HTML 5)
/**
When web application loads Add stuff to the DOM, mainly the user-defined settings from the Settings.plist file, and
the device's data such as device ID, platform version, etc.
*/
- (void)webViewDidStartLoad:(UIWebView *)theWebView {
/*
* This is the Device.platform information
*/
NSString *deviceStr = [[Device alloc] init];
NSDictionary *deviceProperties = [[self getCommandInstance:@"Device"] getDeviceProperties];
NSMutableString *result = [[[NSMutableString alloc] initWithFormat:@"DeviceInfo = %@;", [deviceProperties JSONFragment]] autorelease];

/* Settings.plist
* Read the optional Settings.plist file and push these user-defined settings down into the web application.
Expand All @@ -199,15 +166,16 @@ - (void)webViewDidStartLoad:(UIWebView *)theWebView {
*/
NSDictionary *temp = [self getBundlePlist:@"Settings"];
if ([temp respondsToSelector:@selector(JSONFragment)]) {
NSString *initString = [[NSString alloc] initWithFormat:@"%@\nwindow.Settings = %@;", deviceStr, [temp JSONFragment]];
NSLog(@"%@", initString);
[theWebView stringByEvaluatingJavaScriptFromString:initString];
[initString release];
} else {
[theWebView stringByEvaluatingJavaScriptFromString:deviceStr];
[result appendFormat:@"\nwindow.Settings = %@;", [temp JSONFragment]];
}

NSLog(@"Device initialization: %@", result);
[theWebView stringByEvaluatingJavaScriptFromString:result];
}

/**
Returns the contents of the named plist bundle, loaded as a dictionary object
*/
- (NSDictionary*)getBundlePlist:(NSString *)plistName
{
NSString *errorDesc = nil;
Expand All @@ -221,6 +189,9 @@ - (NSDictionary*)getBundlePlist:(NSString *)plistName
return temp;
}

/**
Called when the webview finishes loading. This stops the activity view and closes the imageview
*/
- (void)webViewDidFinishLoad:(UIWebView *)theWebView {
/*
* Hide the Top Activity THROBER in the Battery Bar
Expand All @@ -235,8 +206,8 @@ - (void)webViewDidFinishLoad:(UIWebView *)theWebView {
}


/*
* - Fail Loading With Error
/**
* Fail Loading With Error
* Error - If the webpage failed to load display an error with the reson.
*
*/
Expand All @@ -249,30 +220,24 @@ - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
}


/*
/**
* Start Loading Request
* This is where most of the magic happens... We take the request(s) and process the response.
* From here we can re direct links and other protocalls to different internal methods.
*
*/
- (BOOL)webView:(UIWebView *)theWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {

NSURL * url = [request URL];

// Check to see if the URL request is for the App URL.
// If it is not, then launch using Safari
// TODO: There was a suggestion to check this against a whitelist of urls, this would be a good place to do that.
NSString * urlHost = [url host];
NSString * appHost = [appURL host];
NSRange range = [urlHost rangeOfString:appHost options:NSCaseInsensitiveSearch];

if ([[url scheme] isEqualToString:@"gap"]) {
//NSLog(@"%@", [url description]); // Uncomment to watch gap: commands being issued
/*
* Get Command and Options From URL
* We are looking for URLS that match gap://<Class>.<command>/[<arguments>][?<dictionary>]
* We have to strip off the leading slash for the options.
*
- (BOOL)webView:(UIWebView *)theWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *url = [request URL];

/*
* Get Command and Options From URL
* We are looking for URLS that match gap://<Class>.<command>/[<arguments>][?<dictionary>]
* We have to strip off the leading slash for the options.
*/
if ([[url scheme] isEqualToString:@"gap"]) {
NSLog(@"%@", [url description]); // Uncomment to watch gap: commands being issued
/*
* Note: We have to go through the following contortions because NSURL "helpfully" unescapes
* certain characters, such as "/" from their hex encoding for us. This normally wouldn't
* be a problem, unless your argument has a "/" in it, such as a file path.
Expand Down Expand Up @@ -335,21 +300,33 @@ - (BOOL)webView:(UIWebView *)theWebView shouldStartLoadWithRequest:(NSURLRequest
[fullMethodName release];
}
return NO;
} else {
/*
* We don't have a PhoneGap request, it could be file or something else
*/
if (range.location == NSNotFound) {
[[UIApplication sharedApplication] openURL:url];
}
}

/*
* If a URL is being loaded that's a local file URL, just load it internally
*/
else if ([url isFileURL])
{
NSLog(@"File URL %@", [url description]);
return YES;
}

/*
* We don't have a PhoneGap or local file request, load it in the main Safari browser.
*/
else
{
NSLog(@"Unknown URL %@", [url description]);
[[UIApplication sharedApplication] openURL:url];
return NO;
}

return YES;
}


/*
* accelerometer - Sends Accel Data back to the Device.
/**
* Sends Accel Data back to the Device.
*/
- (void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
NSString * jsCallBack = nil;
Expand All @@ -366,7 +343,6 @@ - (void)dealloc
[[objects objectAtIndex:i] release];
}
[commandObjects release];
[appURL release];
[imageView release];
[viewController release];
[activityView release];
Expand Down
4 changes: 0 additions & 4 deletions iphone/PhoneGap.plist
Expand Up @@ -2,10 +2,6 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Callback</key>
<string>http://localhost/iphone/</string>
<key>Offline</key>
<true/>
<key>DetectPhoneNumber</key>
<true/>
<key>TopActivityIndicator</key>
Expand Down

0 comments on commit 6cceade

Please sign in to comment.