我已将 Reachability 导入到我的应用程序中,我有几个操作问题要问大家。让我先解释一下我的应用程序和其他工具。
此应用程序同时与两个事物通信,一个是临时网络,另一个是通过 3G 的互联网。注意:临时网络未连接到互联网。这非常有效 - 它已经实现并测试得很好。
话虽如此,我想实现可达性来检测两件事。
1) 用户是否连接到 wifi ad-hoc 网络? (如果可能的话,更好的是检测它是否连接到前缀为 WXYZ 的 wifi ad-hoc 网络。例如,如果列出两个网络,一个称为 Linksys,另一个称为 WXYZ-Testing_Platform,它知道它是否连接到 WXYZ)。
2) 用户是否可以通过3G(或2G等)连接到互联网并访问我们的服务器?
提前致谢
编辑以包括 future 观察者的答案:
对于 1),我的代码如下所示:
.h
#import <SystemConfiguration/CaptiveNetwork.h> //for checking wifi network prefix
.m
- (BOOL) connectedToWifi
{
CFArrayRef myArray = CNCopySupportedInterfaces();
// Get the dictionary containing the captive network infomation
CFDictionaryRef captiveNtwrkDict = CNCopyCurrentNetworkInfo(CFArrayGetValueAtIndex(myArray, 0));
NSLog(@"Information of the network we're connected to: %@", captiveNtwrkDict);
NSDictionary *dict = (__bridge NSDictionary*) captiveNtwrkDict;
NSString* ssid = [dict objectForKey:@"SSID"];
if ([ssid rangeOfString:@"WXYZ"].location == NSNotFound || ssid == NULL)
{
return false;
}
else
{
return true;
}
}
对于 2),我导入了 Reachability 并在我连接到服务器时使用此方法...注意:替换 http://www.google.com与服务器信息
-(void) checkIfCanReachServer
{
UIAlertView *errorView;
Reachability *r = [Reachability reachabilityWithHostName:@"http://www.google.com"];
NetworkStatus internetStatus = [r currentReachabilityStatus];
if(internetStatus == NotReachable) {
errorView = [[UIAlertView alloc]
initWithTitle: @"Network Error"
message: @"Cannot connect to the server."
delegate: self
cancelButtonTitle: @"OK" otherButtonTitles: nil];
[errorView show];
}
}
最佳答案
Reachability 仅让您知道设备是否可以成功出 发送数据包。所以
对于 1) 你应该引用 iPhone get SSID without private library .对于 2),您将仅使用 Reachability 来检查互联网连接,然后您需要使用 NSURLConnection
或其他网络库来确保您可以访问您的服务器。
关于iphone - 可达性帮助 - WiFi 检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7122722/