Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone - SSL connection

What is the best starting point to learn connecting to ssl web services by iphone?

Until now i did some basic connections over http via SOAP etc. but i have no experience on https. Any good sources, tutorials, starting references, "use nsurl...class"s are appreciated

like image 557
ahmet emrah Avatar asked Nov 11 '09 17:11

ahmet emrah


2 Answers

NSURLConnection works by default with SSL and can access https sites. Issues may appear regarding letting user trust SSL certificates, here's a discussion on this that I've found to be interesting.

like image 186
luvieere Avatar answered Oct 03 '22 14:10

luvieere


i'm posting a sample https client. it ignores if server certificate is not valid. the server has a webget method with uritemplate=username({usercode})/password({passcode})

you can use CharlesProxy to check your outgoing message

#import "Hello_SOAPViewController.h"
@interface NSURLRequest (withHttpsCertificates)
+ (BOOL)allowsAnyHTTPSCertificateForHost:(NSString*)host;
+ (void)setAllowsAnyHTTPSCertificate:(BOOL)allow forHost:(NSString*)host;
@end

@implementation Hello_SOAPViewController


NSMutableData *webData;

- (void)viewDidLoad {

//////////////////////////////////////////////////

//Web Service Call

//////////////////////////////////////////////////

    NSURL *url = [NSURL URLWithString:@"https://192.168.1.105/HelloService/Service.svc/username(user)/password(xxx)"];                           

    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60.0];
    [NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];

    [theRequest addValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];       

    [theRequest setHTTPMethod:@"GET"];     
    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

    if(theConnection) {
        webData = [[NSMutableData data] retain];
    }
    else {
        NSLog(@"theConnection is NULL");
    }

}



-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [webData setLength: 0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [webData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{

    NSLog(@"ERROR with theConnection:%@",[error description]);
    if ([error code] == -1001 ){//isEqualToString:@"timed out"]) {
        UIAlertView *alertView = [[[UIAlertView alloc] initWithTitle:@"Connection Error" message:@"Server Unresponsive"  delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease];
        [alertView show];

    }else{
        UIAlertView *alertView = [[[UIAlertView alloc] initWithTitle:@"Connection Error" message:@"Check your internet connection "  delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease];
        [alertView show];
    }


    [connection release];
    [webData release];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"DONE. Received Bytes: %d", [webData length]);

    ///////////////////////
    //Process Your Data here:






    ///////////////////////

    [connection release];
    [webData release];

}


- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {

    [super dealloc];
}
like image 20
ahmet emrah Avatar answered Oct 03 '22 14:10

ahmet emrah