PhoneGap and iPhone Development

Have you considered using Appcelerator for your project? Appcelerator provides a NATIVE app versus running your app in a web client. Contact us for more information on Appcelerator and why it is the platform we use for all solutions.


See the reasons why I don’t recommend PhoneGap, Appcelerator is the better solution


 have been doing mobile development for about six to eight months now, I have played around with Android writing sample apps and doing the same with IPhone. I initially was very successful with Android because it was java and I am a java developer, but eventually I bit the bullet and started doing iPhone development.

I have 2 iMacs, two mac book pros, an iPhone 3GS and am IPhone 4 and finally a iPad…. so I figured I better focus on iPhone development. I took a three day developer boot camp which was helpful but the best learning actually came from reading books and writing code myself.

After I got my first application/prototype functional, I stumbled upon PhoneGap. Having some experience with jQuery, Prototype and CSS I figured I could dive in… I have to admit I am impressed. It still has a way to go, but I think it gets the job done pretty well.

I will post more stuff later, but my first post is about dealing with HTTPS request with certificate issues, unsigned, personally signed or just plain ol bad!! It appears that when making ajax calls from phoneGap, there is no way to handle the problem, to basically accept the certificate and keep it moving.

What I ended up doing was writing a simple PhoneGap Plugin to address the problem.

add this code to your javascript file

    PhoneGap.exec("PixAuth. loginWithBadCert","https://www.badcert.com");

Place the code below in the Plugins directory, and then call the method after the device is ready, meaning PhoneGap is all set!

//
//  PixAuth.h
//  phoneGap1
//
//  Created by Aaron saunders on 8/25/10.
//  Copyright 2010 clearlyINNOVATIVE. All rights reserved.
//

#import
#import "PhoneGapCommand.h"

@interface PixAuth : PhoneGapCommand {
    NSURLConnection *aConnection;
    NSURLResponse *connectionResponse;
    NSMutableData *connectionData;

}

- (void)loginWithBadCert:(NSMutableArray*)params withDict:(NSMutableDictionary*)options;

@end

the code for make the request is where the magic is. Making the request, we handle the bad certificate by saying we accept all credentials of any sort. This code is bear minimum and has been slightly modified to hide the client that I am working with and the inner workings of the application.

There may be some typos in the editing of the content, feel free to post a comment if there are questions.

//
//  PixAuth.m
//  phoneGap1
//
//  Created by Aaron saunders on 8/25/10.
//  Copyright 2010 clearlyINNOVATIVE. All rights reserved.
//

#import "PixAuth.h"


@implementation PixAuth

-(void) loginWithBadCert:(NSMutableArray*)params withDict:(NSMutableDictionary*)options {
    NSURL *serverURL = [NSURL URLWithString:[NSString stringWithFormat:@"@%",
                                [params objectAtIndex:0]]];
    NSURLRequest *connectionRequest = [NSURLRequest requestWithURL:serverURL
                                cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60.0];
    NSURLConnection * aConnection = [[NSURLConnection alloc] initWithRequest:connectionRequest delegate:self];
    connectionData = [[NSMutableData alloc] init];
}

/* NSURLConnection Delegate Methods */

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
     NSLog(@"in didReceiveResponse ");
    [connectionResponse release];
    connectionResponse = [response retain];
    [connectionData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
	NSLog(@"in didReceiveData ");
    [connectionData appendData:data];
}

//
// this method, and the one below get the magic rolling and allow my ajax request
// to function as expected, even with the bogus certificate
//
- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {
	return YES;
}

//
// my application requires credentials, so they are stored in the application settings
//
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
	NSLog(@"in didReceiveAuthenticationChallange ");

	NSURLCredential *newCredential;
	newCredential = [NSURLCredential credentialWithUser:[[NSUserDefaults standardUserDefaults] stringForKey:@"emailAddress"]
							password:[[NSUserDefaults standardUserDefaults] stringForKey:@"password"]
							persistence:NSURLCredentialPersistenceNone];
	[[challenge sender] useCredential:newCredential
		   forAuthenticationChallenge:challenge];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
	NSLog(@"in connectionDidFinishLoading ");
	NSString *string = [[[NSString alloc] initWithData:connectionData encoding:NSUTF8StringEncoding] autorelease];
	NSLog(@"%@", string);
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
	NSLog(@"in didFailWithError ");
	NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
}

- (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse {
    return nil;     // Never cache
}

@end


One Comment on “PhoneGap and iPhone Development

Leave a Reply

Your email address will not be published. Required fields are marked *


× three = 9

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>