在我的 iphone 应用程序中,用户可以设置是否要通过 wifi 或 3G/运营商数据从互联网下载数据。
我们如何以编程方式做到这一点?
换句话说,如何强制 iphone 从运营商数据而不是 wifi 获取数据?
有什么建议吗?
在我的 iphone 应用程序中,用户可以设置是否要通过 wifi 或 3G/运营商数据从互联网下载数据。
我们如何以编程方式做到这一点?
换句话说,如何强制 iphone 从运营商数据而不是 wifi 获取数据?
有什么建议吗?
你不能,如果 iPhone 连接到 WiFi,你不能以编程方式强制它使用蜂窝网络下载。
如果手机连接到 WiFi,则不能强制 iPhone 使用运营商数据(3G / Edge)而不是 WiFi。您可以使用SCNetworkReachabilityGetFlags函数来确定您是使用 WiFi 还是有运营商数据连接。
您可以做的是,如果用户连接到 WiFi,弹出一条消息说您的应用程序仅适用于运营商数据,并要求用户关闭 WiFi 并重新启动应用程序。我不建议这样做,因为它只会激怒您的用户,尽管这并没有阻止 Vodafone Portugal 为他们的许多应用程序这样做,以愚蠢地试图强迫您使用更多(昂贵的)运营商数据.
为此,您需要检测手机的状态,并且您可以轻松识别手机使用 wifi 时未传输的天气数据。
-(void) viewWillAppear:(BOOL)animated
{
// check for internet connection
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetworkStatus:) name:kReachabilityChangedNotification object:nil];
internetReachable = [[Reachability reachabilityForInternetConnection] retain];
[internetReachable startNotifier];
// check if a pathway to a random host exists
hostReachable = [[Reachability reachabilityWithHostName: @"www.apple.com"] retain];
[hostReachable startNotifier];
// now patiently wait for the notification
}
- (void) checkNetworkStatus:(NSNotification *)notice {
// called after network status changes
NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
switch (internetStatus){
case NotReachable:
{
NSLog(@"The internet is down.");
self.internetActive = NO;
break;
}
case ReachableViaWiFi:
{
NSLog(@"The internet is working via WIFI.");
self.internetActive = YES;
break;
}
case ReachableViaWWAN:
{
NSLog(@"The internet is working via WWAN.");
self.internetActive = YES;
break;
}
}
NetworkStatus hostStatus = [hostReachable currentReachabilityStatus];
switch (hostStatus)
{
case NotReachable:
{
NSLog(@"A gateway to the host server is down.");
self.hostActive = NO;
break;
}
case ReachableViaWiFi:
{
NSLog(@"A gateway to the host server is working via WIFI.");
self.hostActive = YES;
break;
}
case ReachableViaWWAN:
{
NSLog(@"A gateway to the host server is working via WWAN.");
self.hostActive = YES;
break;
}
}
}
我不确定这是否对您有帮助:
http://developer.apple.com/library/ios/#samplecode/Reachability/Introduction/Intro.html