iOS开发笔记:网络编程总结
2010-10-13 02:04:00 来源:本站整理}
- (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
- ››开发学院教你用SQL 语句最快速清空MySQL 数据表的...
- ››iOS Safari支持浏览器内方向感应
- ››ios实现条形码扫描功能
- ››iOS开发知识:Core data操作含有初始数据的sqlite...
- ››iOS iPhone官方参考资料明细
- ››iOS开发基础:Modal View Controller的不同呈现方...
- ››iOS如何处理设备方向变化
- ››iOS开发基础之@property关键字
- ››iOS4下实现UIView动画结束后调用事件的新方法
- ››iOS开发代码:从文本文件中读取内容到字符串里
- ››iOS开发基础-UITableView
- ››iOS 开发者应该知道的 ARM 结构
赞助商链接