Created
February 9, 2011 09:35
-
-
Save hanksudo/818206 to your computer and use it in GitHub Desktop.
Test URL connection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ConnectionTestAppDelegate.h | |
// ConnectionTest | |
// | |
// Created by Hank Wang on 2011/2/9. | |
// | |
#import <UIKit/UIKit.h> | |
@interface ConnectionTestAppDelegate : NSObject <UIApplicationDelegate, UIAlertViewDelegate> { | |
UIWindow *window; | |
NSURLConnection *urlConnection; | |
} | |
- (void)startConnection; | |
@property (nonatomic, retain) IBOutlet UIWindow *window; | |
@property (nonatomic, retain) NSURLConnection *urlConnection; | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ConnectionTestAppDelegate.m | |
// ConnectionTest | |
// | |
// Created by Hank Wang on 2011/2/9. | |
// | |
#import "ConnectionTestAppDelegate.h" | |
@implementation ConnectionTestAppDelegate | |
@synthesize window, urlConnection; | |
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { | |
[self.window makeKeyAndVisible]; | |
[self startConnection]; | |
return YES; | |
} | |
- (void)startConnection { | |
// Connection Request | |
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com"]]; | |
self.urlConnection = [[[NSURLConnection alloc] initWithRequest:request delegate:self] autorelease]; | |
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES; | |
} | |
// ------------- | |
// handleError:error | |
// ------------- | |
- (void)handleError:(NSError *)error | |
{ | |
NSString *errorMessage = [error localizedDescription]; | |
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Connect Test" | |
message:errorMessage | |
delegate:self | |
cancelButtonTitle:@"Cancel" | |
otherButtonTitles:@"Retry", nil]; | |
[alertView show]; | |
[alertView release]; | |
} | |
// ------------- | |
// connection:didReceiveResponse:response | |
// ------------- | |
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response | |
{ | |
NSLog(@"%@", @"didReceiveResponse"); | |
} | |
// ------------- | |
// connection:didReceiveData:data | |
// ------------- | |
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data | |
{ | |
NSLog(@"%@", @"didReceiveData"); | |
} | |
// ------------- | |
// connection:didFailWithError:error | |
// ------------- | |
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { | |
NSLog(@"%@", @"didFailWithError"); | |
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO; | |
if ([error code] == kCFURLErrorNotConnectedToInternet) { | |
// if we can identify the error, we can present a more precise message to the user. | |
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:@"Connection Failed" | |
forKey:NSLocalizedDescriptionKey]; | |
NSError *noConnectionError = [NSError errorWithDomain:NSCocoaErrorDomain | |
code:kCFURLErrorNotConnectedToInternet | |
userInfo:userInfo]; | |
[self handleError:noConnectionError]; | |
} else { | |
// otherwise handle the error generically | |
[self handleError:error]; | |
} | |
self.urlConnection = nil; // release connection | |
} | |
// ------------- | |
// connectionDidFinishLoading:connection | |
// ------------- | |
- (void)connectionDidFinishLoading:(NSURLConnection *)connection | |
{ | |
self.urlConnection = nil; // release connection | |
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO; | |
// show successful | |
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Connect Test" | |
message:@"Connection Successful" | |
delegate:nil | |
cancelButtonTitle:@"OK" | |
otherButtonTitles:nil]; | |
[alertView show]; | |
[alertView release]; | |
NSLog(@"%@", @"connectionDidFinishLoading"); | |
} | |
// ----- | |
// alertView:alertView clickedButtonAtIndex:buttonIndex | |
// ----- | |
- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { | |
switch (buttonIndex) { | |
case 0: | |
NSLog(@"Button Cancel Pressed"); | |
break; | |
case 1: | |
NSLog(@"Button Retry Pressed"); | |
[self startConnection]; | |
break; | |
default: | |
break; | |
} | |
} | |
- (void)dealloc { | |
[self.urlConnection release]; | |
[window release]; | |
[super dealloc]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment