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<UIApplicationDelegate>
{
????
}
@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<UIApplicationDelegate>
{
?????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?