gpt4 book ai didi

iOS 可达性 : Problem with host Method

转载 作者:行者123 更新时间:2023-11-29 11:20:22 24 4
gpt4 key购买 nike

我已经创建了一个方法来检查主机是否可达。我从苹果开发者网站下载了 Reachability 类(.h 和 .m)并导入到我的项目中。我已将 NSString 名称作为 URL(主机名)传递。主机名是 http://www.google.com .但是,无论我将什么主机名传递给此方法,它始终返回 NO (connectToHost)。代码如下:

- (BOOL) checkHostAvailability:(NSString *)Name{
BOOL connectToHost;
hostReach = [[Reachability reachabilityWithHostName:Name] retain];
[hostReach startNotifier];

NetworkStatus hostStatus = [hostReach currentReachabilityStatus];
NSLog(@"Name: %@", Name);
NSLog(@"hostStatus is %@", hostStatus);

if(hostStatus == NotReachable){
NSLog(@"Here is the checkHostAvailability Method and host NOT reachable");
connectToHost = NO;
}else{
NSLog(@"Here is the checkHostAvailability Method and host is reachable");
connectToHost = YES;
}
return connectToHost;


}

经过几个小时的调查,我发现 NetworkStatus hostStatus 始终等于 null。我认为这就是此方法不起作用的原因。我花了 8 个小时找出这段代码的问题并搜索了这个网站,但我仍然找不到问题和解决方案。

请帮忙,非常感谢。

最佳答案

从主机名中删除“http://”。

关于iOS 可达性 : Problem with host Method,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7501641/

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