網路連接中用到的類:
一.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.連結狀態的即時通知
即時檢查,持續狀態發生變化時,需要及時地通知使用者:
Reachability 1.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];
}
Reachability 2.0版本
//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