Bau*_*aub 7 iphone cocoa-touch objective-c reachability
我已将Reachability导入到我的应用程序中,并且我还有几个方法问题.让我先解释一下我的应用程序和其他工具.
该应用程序通过3G与同一时间,ad-hoc网络和互联网进行通信.注意:ad-hoc网络未连接到Internet.这非常有效 - 它已经实现并且测试得非常好.
话虽如此,我想实现Reachability来检测两件事.
1)用户是否连接到wifi ad-hoc网络?(如果可能的话,更好的方法是检测它是否连接到前缀为WXYZ的wifi ad-hoc网络.例如,如果列出了两个网络,一个名为Linksys,另一个名为WXYZ-Testing_Platform,它知道是否与WXYZ连接).
2)用户可以通过3G(或2G等)连接到互联网并访问我们的服务器吗?
提前致谢
编辑包括对未来寻求者的回答:
对于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;
}
}
Run Code Online (Sandbox Code Playgroud)
对于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];
}
}
Run Code Online (Sandbox Code Playgroud)
只有可达性让你知道,如果该设备可以发送数据包了成功.所以1)你应该参考iPhone获取没有私人图书馆的SSID.对于2)您将仅使用可达性来检查互联网连接,然后您需要使用NSURLConnection
或其他网络库以确保您可以访问您的服务器.
归档时间: |
|
查看次数: |
3191 次 |
最近记录: |