use*_*098 13 iphone connection objective-c ipad ios
哪个是使用iOS SDK检查Internet连接的最佳方法?
iDe*_*Dev 36
最好的方法是使用Reachability代码.点击此处查看苹果示例代码.这有很多方便的方法来检查互联网可用性,Wifi/WAN连接检查等.
例如: -
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(networkChanged:) name:kReachabilityChangedNotification object:nil];
reachability = [Reachability reachabilityForInternetConnection];
[reachability startNotifier];
- (void)networkChanged:(NSNotification *)notification
{
NetworkStatus remoteHostStatus = [reachability currentReachabilityStatus];
if(remoteHostStatus == NotReachable) { NSLog(@"not reachable");}
else if (remoteHostStatus == ReachableViaWiFiNetwork) { NSLog(@"wifi"); }
else if (remoteHostStatus == ReachableViaCarrierDataNetwork) { NSLog(@"carrier"); }
}
Run Code Online (Sandbox Code Playgroud)
Art*_*are 11
可达性比它需要的要多得多,而且尚未针对ARC更新.
这是我在纯C中的解决方案.大部分代码都是直接从Reachability获取的,但只提炼到了必要的内容.我只想要它返回是否有互联网连接,但你可以从评论中读取它是否基于找到Wifi或蜂窝网络返回YES.
在继续共享代码之前的最后一点:您需要进入构建目标,选择构建阶段选项卡,并将"SystemConfiguration.framework"添加到"Link Binary With Libraries"列表中.
#import <CoreFoundation/CoreFoundation.h>
#import <SystemConfiguration/SystemConfiguration.h>
#import <netdb.h>
BOOL networkReachable()
{
struct sockaddr_in zeroAddress;
bzero(&zeroAddress, sizeof(zeroAddress));
zeroAddress.sin_len = sizeof(zeroAddress);
zeroAddress.sin_family = AF_INET;
SCNetworkReachabilityRef reachabilityRef = SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault, (const struct sockaddr *) &zeroAddress);
SCNetworkReachabilityFlags flags;
if (SCNetworkReachabilityGetFlags(reachabilityRef, &flags)) {
if ((flags & kSCNetworkReachabilityFlagsReachable) == 0) {
// if target host is not reachable
return NO;
}
if ((flags & kSCNetworkReachabilityFlagsConnectionRequired) == 0) {
// if target host is reachable and no connection is required
// then we'll assume (for now) that your on Wi-Fi
return YES; // This is a wifi connection.
}
if ((((flags & kSCNetworkReachabilityFlagsConnectionOnDemand ) != 0)
||(flags & kSCNetworkReachabilityFlagsConnectionOnTraffic) != 0)) {
// ... and the connection is on-demand (or on-traffic) if the
// calling application is using the CFSocketStream or higher APIs
if ((flags & kSCNetworkReachabilityFlagsInterventionRequired) == 0) {
// ... and no [user] intervention is needed
return YES; // This is a wifi connection.
}
}
if ((flags & kSCNetworkReachabilityFlagsIsWWAN) == kSCNetworkReachabilityFlagsIsWWAN) {
// ... but WWAN connections are OK if the calling application
// is using the CFNetwork (CFSocketStream?) APIs.
return YES; // This is a cellular connection.
}
}
return NO;
}
Run Code Online (Sandbox Code Playgroud)
Dar*_*ngs 10
试试这段代码:
- (BOOL)connectedToInternet
{
NSURL *url=[NSURL URLWithString:@"http://www.google.com"];
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"HEAD"];
NSHTTPURLResponse *response;
[NSURLConnection sendSynchronousRequest:request returningResponse:&response error: NULL];
return ([response statusCode]==200)?YES:NO;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
30685 次 |
最近记录: |