gpt4 book ai didi

iphone - 如何检查 iPhone 的网络连接

转载 作者:行者123 更新时间:2023-12-03 20:45:50 26 4
gpt4 key购买 nike

此代码用于检查互联网连接是否可用。如果互联网连接可用,则应从服务器数据库验证用户名和密码,即应调用发送请求方法,如果没有,则应从本地验证用户名和密码数据库,即检查方法应该被调用。但是这里的问题是当互联网关闭时,它也会进入发送请求方法而不是进入检查网络。可能是什么问题。请帮助我解决这个问题。我添加了可达性文件并导入了 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];
}





}

最佳答案

Rocky,你的代码是正确的。只需检查 URL 是否正确。还有其他方法在按钮上检查主机,如下所示

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(reachabilityChanged:)
name:kReachabilityChangedNotification
object:nil];

hostReach = [[Reachability reachabilityWithHostName:@"www.google.com"]retain];
[hostReach startNotifier];

并在 (reachabilityChanged:) 方法调用中像这样

-(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];
}
}

希望这对你有帮助

关于iphone - 如何检查 iPhone 的网络连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7035369/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com