http://www.cnblogs.com/hanjun/archive/2012/12/01/2797622.html
网络连接中用到的类:
一.Reachability
1.添加 Reachability 的.h和.m文件,再添加SystemConfiguration.framework。
2.Reachability中定义了三种网络状态:
typedef Num{
NotReachable = 0, //无连接
ReachableViaWiFi, //使用3G/GPRS网络
ReachableViaWWAN //使用WiFi网络
}NetworkStatus;
3.示例:
Reachability *reachability = [Reachablity reachabilityWithHostName:@"www.baidu.com"];
switch([reachabilityStatus]){
case NotReachable:
//TODO
break;
case ReachableViaWiFi:
//TODO
break;
case ReachableViaWWAN:
//TODO
break;
}
4.检查当前网络环境
程序启动时,如果想检测可用的网络环境,可以像这样来使用
//是否wifi
+ (BOOL)isEnableWIFI
{
return ([[Reachability reachabiliyForLocalWIFI] currentReachabilityStatus] != NotReachable);
}
//是否3G
+ (BOOL)isEnable3G
{
return ([[Reachability reachabiliyForInternetConnetion] currentReachabilityStatus] != NotReachable);
}
示例:
- (void)viewWillAppear:(BOOL)animated
{
if (([Reachability reachabiliyForInternetConnetion].currentReachabilityStatus == NotReachable) && [Reachability reachabiliyForLocalWIFI].currentReachabilityStatus == NotReachable))
{
self.navigationItem.hidesBackButton = YES;
[self.navigationItem setLeftBarButtonItem:nil animated:NO];
}
}
5.链接状态的实时通知
实时检查,持续状态发生变化时,需要及时地通知用户:
//MyAppDelegate.h
#import "Reachability"
@interface MyAppDelegate:NSObject
{
}
@property NetworkStatus remoteHostStatus;
@end
//MyAppDelegate.m
#import "MyAppDelegate.h"
@implementation MyAppDelegate
@synthesize remoteHostStatus;
//更新网络状态
- (void)updateStatus
{
self.remoteHostStatus = [[Reachability sharedReachability] remoteHostStatus];
}
//通知网络状态
- (void)reachabilityChanged:(NSNotification *)note
{
[self updateStatus];
if (self.remoteHostStatus == NotReachable)
{
UIAlert *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"AppName",nil)
message: NSLocalizedString (@"NotReachable",nil);
delegate:nil cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
}
}
//程序启动器,启动网络监视
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
//设置网络监测的站点
[[Reachability sharedReachability] setHostName:@"www.baidu.com"];
[[Reachability sharedReachability] setNetworkStatusNotificationsEnabled:YES];
//设置网络状态变化时的通知函数
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:)
name:@"kNetworkReachabilityChangedNotification" object:nil];
[self updateStatus];
}
- (void)dealloc
{
//删除通知对象
[[NSNotificationCenter defaultCenter] removeObserver:self];
[window release];
[super dealloc];
}
//MyAppDelegate.h
#import "Reachability"
@class Reachability;
@interface MyAppDelegate:NSObject
{
Reachability *hostReach;
}
@end
//MyAppDelegate.m
#import "MyAppDelegate.h"
@implementation MyAppDelegate
//通知网络状态
- (void)reachabilityChanged:(NSNotification *)note
{
Reachability *currentReach = [note object];
NSParameterAssert([currentReach isKindOfClass:[Reachability class]]);
NetworkStatus status = [currentReach currentReachabilityStatus];
if (status == NotReachable)
{
UIAlert *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"AppName",nil)
message: NSLocalizedString (@"NotReachable",nil);
delegate:nil cancelButtonTitle:@"YES"
otherButtonTitles:nil];
[alert show];
[alert release];
}
}
//程序启动器,启动网络监视
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
//....
//监测网络情况
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:)
name:@"kNetworkReachabilityChangedNotification" object:nil];
hostReach = [[Reachability reachabilityWithHostName:@"www.baidu.com"] retain];
// hostReach startNotifer];
//...
}
二、其他常用的类。
1.NSURL
2.NSURLRequest
3.NSMutableURLRequest 是NSURLRequest的子类,可以设置一些请求参数
4.NSURLResponse
5.NSError