凌雪
2018-10-23
来源 :网络
阅读 1737
评论 0
摘要:本文将带你了解IOS开发入门iOS开发中如何检测网络是否真正可用(连通),希望本文对大家学IOS有所帮助。
本文将带你了解IOS开发入门iOS开发中如何检测网络是否真正可用(连通),希望本文对大家学IOS有所帮助。
iOS开发中如何检测网络是否真正可用(连通),在iOS 开发过程中,无论是什么应用,基本上都会涉及到移动设备的网络连接状态判断,以便于在手机等移动端没有网络或者是切换网络后,在应用内给用户以必要的提示,进而提高用户体验。
而iOS 开发中经常用到的Reachability,只能判断手机连接的网络类型(如2g,3g,4g, WiFi或者是无网络连接的情况),并没有实现对所连接网络的可用性(是否可以访问Internet)进行判断。如果手机连接的WiFi并没有接入互联网,那么这台手机虽然连上了WiFi,但是根本就不能上网(例如将路由器的Wan 口网线拔掉)。
下边直接上代码,自己封装的判断网络可用性的方法,根据返回的BOOL 值来判断网络是否可用(可以写到专门的工具类里边,方便在工程中的任何地方调用)。
<code>#define kAppleUrlToCheckNetStatus @"https://captive.apple.com/"
- (BOOL)checkNetCanUse {
__block BOOL canUse = NO;
NSString *urlString = kAppleUrlToCheckNetStatus;
**// 使用信号量实现NSURLSession同步请求**
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
[[[NSURLSession sharedSession] dataTaskWithURL:[NSURL URLWithString:urlString] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSString* result = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
//解析html页面
NSString *htmlString = [self filterHTML:result];
//除掉换行符
NSString *resultString = [htmlString stringByReplacingOccurrencesOfString:@"\n" withString:@""];
if ([resultString isEqualToString:@"SuccessSuccess"]) {
canUse = YES;
NSLog(@"手机所连接的网络是可以访问互联网的: %d",canUse);
}else {
canUse = NO;
NSLog(@"手机无法访问互联网: %d",canUse);
}
dispatch_semaphore_signal(semaphore);
}] resume];
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
return canUse;
}
</code>
//过滤后台返回字符串中的标签
<code><code>- (NSString *)filterHTML:(NSString *)html {
NSScanner *theScanner;
NSString *text = nil;
theScanner = [NSScanner scannerWithString:html];
while ([theScanner isAtEnd] == NO) {
// find start of tag
[theScanner scanUpToString:@"<" intoString:NULL] ;
// find end of tag
[theScanner scanUpToString:@">" intoString:&text] ;
// replace the found tag with a space
//(you can filter multi-spaces out later if you wish)
html = [html stringByReplacingOccurrencesOfString:
[NSString stringWithFormat:@"%@>", text]
withString:@""];
}
return html;
}
</code></code>
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标移动开发之IOS频道!
喜欢 | 0
不喜欢 | 0
您输入的评论内容中包含违禁敏感词
我知道了

请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式AI+学习就业服务平台 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号