ios网络开发 网络状态检查
http://www.cnblogs.com/hanjun/archive/2012/12/01/2797622.html
網(wǎng)絡(luò)連接中用到的類:
一.Reachability?
? ? 1.添加?Reachability 的.h和.m文件,再添加SystemConfiguration.framework。
? ? 2.Reachability中定義了三種網(wǎng)絡(luò)狀態(tài):
? typedef Num{
NotReachable = 0, ?//無連接
ReachableViaWiFi, ?//使用3G/GPRS網(wǎng)絡(luò)
ReachableViaWWAN? ?//使用WiFi網(wǎng)絡(luò)
? ? ? ?}NetworkStatus;
? ? ?3.示例:
??Reachability *reachability = [Reachablity ?reachabilityWithHostName:@"www.baidu.com"];
? switch([reachabilityStatus]){
case??NotReachable:
//TODO?
break;?
case ?ReachableViaWiFi:
//TODO ?
break;?
case ?ReachableViaWWAN:
//TODO ?
break;??
?}?
? ? ? 4.檢查當(dāng)前網(wǎng)絡(luò)環(huán)境
程序啟動時,如果想檢測可用的網(wǎng)絡(luò)環(huán)境,可以像這樣來使用
? //是否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.鏈接狀態(tài)的實時通知
實時檢查,持續(xù)狀態(tài)發(fā)生變化時,需要及時地通知用戶:
?
Reachability?1.5版本//MyAppDelegate.h
#import?"Reachability"
@interface?MyAppDelegate:NSObject<UIApplicationDelegate>
{
????
}
@property?NetworkStatus?remoteHostStatus;
@end?
?
?//MyAppDelegate.m
#import?"MyAppDelegate.h"
@implementation?MyAppDelegate
@synthesize?remoteHostStatus;
//更新網(wǎng)絡(luò)狀態(tài)
-?(void)updateStatus
{
????self.remoteHostStatus?=?[[Reachability?sharedReachability]?remoteHostStatus];
}
//通知網(wǎng)絡(luò)狀態(tài)
-?(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];
????}
}
//程序啟動器,啟動網(wǎng)絡(luò)監(jiān)視
-?(void)applicationDidFinishLaunching:(UIApplication?*)application
{
???//設(shè)置網(wǎng)絡(luò)監(jiān)測的站點(diǎn)
???[[Reachability?sharedReachability]?setHostName:@"www.baidu.com"];
???[[Reachability?sharedReachability]?setNetworkStatusNotificationsEnabled:YES];
???//設(shè)置網(wǎng)絡(luò)狀態(tài)變化時的通知函數(shù)
???[[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
//通知網(wǎng)絡(luò)狀態(tài)
-?(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];
????}
}
//程序啟動器,啟動網(wǎng)絡(luò)監(jiān)視
-?(void)applicationDidFinishLaunching:(UIApplication?*)application
{
???//....
???//監(jiān)測網(wǎng)絡(luò)情況
???[[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的子類,可以設(shè)置一些請求參數(shù)
?4.NSURLResponse?
?5.NSError?
轉(zhuǎn)載于:https://www.cnblogs.com/xuejinhui/p/4469624.html
總結(jié)
以上是生活随笔為你收集整理的ios网络开发 网络状态检查的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android 5.1 API 22 所
- 下一篇: iOS UISegmentedContr