gpt4 book ai didi

ios - 在以下代码中加入3G检测

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

我有一种检查 Wi-Fi 的方法,这是我从一个旧项目中获得的,现在我需要实际检查 3G 或 < strong>Wifi,如果没有,请给出错误消息。

原始样本工作:

- (BOOL)checkForWIFIConnection {

Reachability* wifiReach = [Reachability reachabilityForLocalWiFi];
NetworkStatus netStatus = [wifiReach currentReachabilityStatus];
if (netStatus!=ReachableViaWiFi)
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Sem conexão à internet!", @"AlertView")
message:NSLocalizedString(@"Não está conectado à internet. Tente novamente após se connectar.", @"AlertView")
delegate:self
cancelButtonTitle:NSLocalizedString(@"OK", @"AlertView")
otherButtonTitles: nil];
[alertView show];
return NO;
}
else {
return YES;
}
}

如何让它检查 ReachableViaWWAN ?我可以在这里添加它吗 (<-) ??

- (BOOL)checkForWIFIConnection {

Reachability* wifiReach = [Reachability reachabilityForLocalWiFi];
NetworkStatus netStatus = [wifiReach currentReachabilityStatus];
if (netStatus!=ReachableViaWiFi && ReachableViaWWAN) <- (i get an error saying use of logical && with constant operand)
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Sem conexão à internet!", @"AlertView")
message:NSLocalizedString(@"Não está conectado à internet. Tente novamente após se connectar.", @"AlertView")
delegate:self
cancelButtonTitle:NSLocalizedString(@"OK", @"AlertView")
otherButtonTitles: nil];
[alertView show];
return NO;
}
else {
return YES;
}
}

感谢您的帮助。

最佳答案

只需更改:

Reachability* wifiReach = [Reachability reachabilityForLocalWiFi];

到:

Reachability* wifiReach = [Reachability reachabilityForInternetConnection];

if (netStatus!=ReachableViaWiFi)

到:

if (netStatus == NotReachable)

换句话说:

Reachability* wifiReach = [Reachability reachabilityForInternetConnection];
NetworkStatus netStatus = [wifiReach currentReachabilityStatus];
if (netStatus == NotReachable)
{

顺便说一句 - 请查阅 Objective-C 教程以了解如何编写复合表达式。你的if声明需要是这样的:

if (netStatus!=ReachableViaWiFi && netStatus!=ReachableViaWWAN)

虽然这将解决编译器问题,但它不适用于您的代码,因为 WWAN使用 reachabilityForLocalWiFi 时永远不会给出值.

关于ios - 在以下代码中加入3G检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22306158/

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