2011年2月9日 星期三

[iOS Dev] 簡單的網路連結測試 Simple Internet Connection test

以下範例為使用 NSURLConnection 測試網路連結

若網路連結失敗會回傳錯誤訊息 kCFURLErrorNotConnectedToInternet , 則使用UIAlertView來回報錯誤 (也可以做成Cancel and Retry讓使用者選擇)




若用 iPhone Simulator 是用Mac的網路連結, 所以測試前, 要先把Mac的網路斷線

若連結成功, 會在Console裡看到:
2011-02-09 15:28:38.148 ConnectionTest[11804:207] didReceiveResponse
2011-02-09 15:28:38.151 ConnectionTest[11804:207] didReceiveData
...
...
...
2011-02-09 15:28:38.965 ConnectionTest[11804:207] connectionDidFinishLoading


開始實作:先建立一個Template為Window-based Application 的 new project 叫 connectionTest



//
// 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

//
// 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



Reference

Apple Developer Sample - LazyTableImages
Related Posts Plugin for WordPress, Blogger...