gpt4 book ai didi

iphone - 如何接收连接类型已更改的通知(3G、Edge、Wifi、GPRS)

转载 作者:行者123 更新时间:2023-12-01 18:32:11 24 4
gpt4 key购买 nike

有没有办法发现当前连接是 3G、Edge 还是 WiFi,并收到通知?或者只是一种发现连接是否是WiFi的方法?

最佳答案

你应该使用 Apple 的 Reachability 类

http://developer.apple.com/library/ios/#samplecode/Reachability/Listings/Classes_Reachability_h.html

实现后,您可以在委托(delegate)中使用它:

- (void) configureTextField:  (Reachability*) curReach
{
NetworkStatus netStatus = [curReach currentReachabilityStatus];
BOOL connectionRequired= [curReach connectionRequired];
NSString* statusString= @"non";
switch (netStatus)
{
case NotReachable:
{
statusString = @"Access Not Available";

//Minor interface detail- connectionRequired may return yes, even when the host is unreachable. We cover that up here...
connectionRequired= NO;
break;
}

case ReachableViaWWAN:
{
statusString = @"Reachable WWAN";


UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Cellular Data Detected" message:@"You are using Cellular data such as 3G or Edge. Downloading large amount of data may effect your cellular internet package costs. To avoid such extra cost kindly use Wifi."
delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];


break;
}
case ReachableViaWiFi:
{
statusString= @"Reachable WiFi";

break;
}
}
if(connectionRequired)
{
statusString= [NSString stringWithFormat: @"%@, Connection Required", statusString];
}
NSLog(@"Network= %@",statusString);
if ([statusString isEqualToString:@"Access Not Available"]){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No Internet Connection" message:@"It seems you are not connected to the internet, the app will try to load from the last cached data - assuming this data exist."
delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
//textField.text= statusString;
}


- (void) updateInterfaceWithReachability: (Reachability*) curReach
{
if(curReach == hostReach)
{
//[self configureTextField: remoteHostStatusField imageView: remoteHostIcon reachability: curReach];
// NetworkStatus netStatus = [curReach currentReachabilityStatus];
// BOOL connectionRequired= [curReach connectionRequired];

[self configureTextField:curReach];

}
}

关于iphone - 如何接收连接类型已更改的通知(3G、Edge、Wifi、GPRS),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7685152/

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