我已將可達性導入到我的應用程序中,並且我有幾個針對您的問題。首先讓我解釋我的應用程序和其他工具。可達性幫助 - WiFi檢測
此應用程序在同一時間通過兩個東西,一個ad-hoc網絡和通過3G互聯網進行通信。注意:ad-hoc網絡沒有連接到互聯網。這是完美的 - 它已經實現並測試出色。
說到這裏,我想實現Reachability來檢測兩件事情。
1)用戶是否連接到wifi ad-hoc網絡? (如果可能的話,更好的辦法是檢測它是否連接到WXYZ前綴的WIFI ad-hoc網絡,例如,如果列出了兩個網絡,一個稱爲Linksys,另一個稱爲WXYZ-Testing_Platform,它知道無論它是否連接到WXYZ)。
2)用戶可以通過3G(或2G等)連接到互聯網並訪問我們的服務器嗎?
在此先感謝
編輯以INCLUDE回答未來旁觀者:
爲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];
}
}
我現在來看看這個,謝謝! – Baub