此代码用于检查互联网连接是否可用。如果互联网连接可用,则应从服务器数据库验证用户名和密码,即应调用发送请求方法,如果没有,则应从本地验证用户名和密码数据库,即检查方法应该被调用。但这里的问题是当互联网关闭然后它进入发送请求方法而不是检查nethod.What可能是prob.Please帮我解决这个问题。我添加了可访问性文件并导入了CFNetwork.framework。
- (void) showNetWorkAlert {
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
UIAlertView *networkAlert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Network connection unavailable."
delegate:self cancelButtonTitle:@"OK" otherButtonTitles:@"Retry", nil];
[networkAlert show];
[networkAlert release];
}
#pragma mark To Check Network Connection.
- (BOOL) currentNetworkStatus {
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
BOOL connected;
const char *host = "www.apple.com";
SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithName(NULL, host);
SCNetworkReachabilityFlags flags;
connected = SCNetworkReachabilityGetFlags(reachability, &flags);
BOOL isConnected = YES;
isConnected = connected && (flags & kSCNetworkFlagsReachable) && !(flags & kSCNetworkFlagsConnectionRequired);
CFRelease(reachability);
if(!isConnected) {
// sleep(1);
[self showNetWorkAlert];
//[self check];
}
else
return isConnected;
//[self sendRequest];
return isConnected;
}
-(IBAction)buttonPressed:(id)sender
{
//[self sendRequest];
//[[Reachability sharedReachability] setHostName:kHostName];
//Set Reachability class to notifiy app when the network status changes.
//[[Reachability sharedReachability] setNetworkStatusNotificationsEnabled:YES];
//Set a method to be called when a notification is sent.
//[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:@"kNetworkReachabilityChangedNotification" object:nil];
//[self updateStatus];
//[self sendRequest];
//NSLog(<#NSString *format#>)
//this is to select username and password from database.
//[self check];
if ([self currentNetworkStatus]) {
[self sendRequest];
}
else {
[self check];
}
}
答案 0 :(得分:3)
Rocky你的代码是正确的。请检查URL是否正确。 还有其他方式 按钮按钮检查主机
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(reachabilityChanged:)
name:kReachabilityChangedNotification
object:nil];
hostReach = [[Reachability reachabilityWithHostName:@"www.google.com"]retain];
[hostReach startNotifier];
并在(reachabilityChanged :)方法调用make like this
-(void)reachabilityChanged:(NSNotification*)note
{
static BOOL showNotConnnected =NO;
Reachability *curReach =[note object];
NSParameterAssert([curReach isKindOfClass:[Reachability class]]);
NetworkStatus netStatus = [curReach currentReachabilityStatus];
if(netStatus != NotReachable)
{
UIAlertView *notconnect1 = [[UIAlertView alloc]initWithTitle:@"Server is connected" message:@"Server is connected" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:@"Cancel",nil];
[notconnect1 show];
[notconnect1 release];
}
else if(showNotConnnected == NO)
{
showNotConnnected =YES;
UIAlertView *notconnect = [[UIAlertView alloc]initWithTitle:@"Server is Not connected" message:@"Server may be slow or not connected" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:@"Cancel",nil];
[notconnect show];
[notconnect release];
}
}
我希望这会对你有所帮助
答案 1 :(得分:1)
我认为有一个名为Reachability的示例代码可以帮助您。请参阅下面的网址
http://developer.apple.com/library/ios/samplecode/Reachability/Introduction/Intro.html
答案 2 :(得分:0)
您应该查看Apple的可访问性代码以检查与Internet的连接。您可以在iOS Developer Portal中获取一个示例项目。
答案 3 :(得分:0)
在发出网络请求之前,不应使用可达性来检测网络是否可用,因为发出网络请求的行为可以在需要时启动网络。可达性仅应用于检测网络在不可用后何时可用。
尝试发出网络请求 - 如果网络不可用,您将收到错误消息。通常响应会立即返回,但如果网络不稳定,可能需要一段时间,因此您不应该在主线程上进行同步网络调用。如果可能的话,使用NSURLConnection,当发生某些事情时你会得到回调。