日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

ios网络开发 网络状态检查

發(fā)布時間:2023/12/13 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ios网络开发 网络状态检查 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。