gpt4 book ai didi

ios - 要检查wifi是否开启但没有互联网连接

转载 作者:行者123 更新时间:2023-12-01 17:01:14 25 4
gpt4 key购买 nike

我正在使用可达性类来检查代码中的wifi连接。但是有时会出现wifi开启但没有互联网连接或互联网连接不足的问题,这里我的代码循环运行,等待来自所谓的Web服务的任何响应,并且挂断并有时崩溃。
当我在AlertView上单击OK(从Web服务中提取一些数据)时,将执行以下代码
这是我的代码:

        Reachability *ReachObj = [Reachability reachabilityForInternetConnection];
[ReachObj startNotifier];
NetworkStatus remoteHostStatus = [ReachObj currentReachabilityStatus];

if (remoteHostStatus==ReachableViaWiFi)
{
SecondView *ObjSecView=[[SecondView alloc]init];
[self presentModalViewController:ObjSecView animated:YES];
}

else
if (remoteHostStatus==NotReachable)
{
FirstView *objFrstView=[[FeedBackPopOverViewController alloc]init];
[self presentModalViewController:objFrstView animated:YES];
}

我是Objective C的新手。
请先帮助我,谢谢。对于我的语法错误,我们深表歉意。

最佳答案

尝试这个..

SCNetworkReachabilityFlags flags;

SCNetworkReachabilityRef reachability=SCNetworkReachabilityCreateWithName(NULL, [@"your web sevice url" UTF8String]);

SCNetworkReachabilityGetFlags(reachability, &flags);
BOOL reachable=!(flags & kSCNetworkReachabilityFlagsConnectionRequired);

CFRelease(reachability);
NSURLRequest *request=[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];


if([NSURLConnection canHandleRequest:request] && reachable)
{
conn=[NSURLConnection connectionWithRequest:request delegate:self];

if(conn)
{
////

}
else
{
[_delegate performSelector:@selector(httpDataDidFailLoadingWithReason:)
withObject:@"No Internet Connection" afterDelay:0.1];
}

}


-(void) httpDataDidFailLoadingWithReason:(NSString*)reason
{

UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"abc"
message:reason
delegate:self cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
[alertView release];

}

关于ios - 要检查wifi是否开启但没有互联网连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6705654/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com