WEB开发网
开发学院手机开发iPhone 开发 iOS开发笔记:网络编程总结 阅读

iOS开发笔记:网络编程总结

 2010-10-13 02:04:00 来源:本站整理   
核心提示:}- (void)dealloc {// 删除通知对象[[NSNotificationCenter defaultCenter] removeObserver:self];[window release];[super dealloc];}Reachability 2.0版本// MyAppDelegate.h@cla

}

- (void)dealloc {

// 删除通知对象

[[NSNotificationCenter defaultCenter] removeObserver:self];

[window release];

[super dealloc];

}

Reachability 2.0版本

// MyAppDelegate.h

@class Reachability;

@interface MyAppDelegate : NSObject {

Reachability *hostReach;

}

@end

// MyAppDelegate.m

- (void)reachabilityChanged:(NSNotification *)note {

Reachability* curReach = [note object];

NSParameterAssert([curReach isKindOfClass: [Reachability class]]);

NetworkStatus status = [curReach currentReachabilityStatus];

if (status == NotReachable) {

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"AppName""

message:@"NotReachable"

delegate:nil

cancelButtonTitle:@"YES" otherButtonTitles:nil];

[alert show];

[alert release];

}

}

- (void)applicationDidFinishLaunching:(UIApplication *)application {

// ...

// 监测网络情况

[[NSNotificationCenter defaultCenter] addObserver:self

selector:@selector(reachabilityChanged:)

name: kReachabilityChangedNotification

object: nil];

hostReach = [[Reachability reachabilityWithHostName:@"www.google.com"] retain];

hostReach startNotifer];

// ...

}

二:使用NSConnection下载数据

1.创建NSConnection对象,设置委托对象

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[self urlString]]];

[NSURLConnection connectionWithRequest:request delegate:self];

2. NSURLConnection delegate委托方法

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError

上一页  1 2 3 4 5  下一页

Tags:iOS 开发 笔记

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接