我正在开发一个应用程序,它是一个客户端并与网络服务通信。我正在使用 Reachability 检查互联网连接类(class)。如果可用,我将 bool 值设置为 YES,默认为 NO。
在我的应用程序委托(delegate)的 didFinishLaunchingWithOptions
方法中,我获取了一个单例对象并将其添加用于观察网络状态变化,以便它可以快速将 bool 变为 YES。
当我的第一个 viewDidLoad
时,我尝试从 Web 服务 getToken
所以我检查 internetConnection
是否可用并且它总是返回 NO 因为我的对象在我尝试获取 token 后收到通知。我不认为使用延迟执行者是个好主意,所以我该如何处理这种情况。提前致谢..
最佳答案
在您的例子中,我使用了ASIHTTPRequest
库来检查请求是否到达服务器。 ASIHTTPRequest 提供了很多方法,包括这个:
-(void)requestFinished:(ASIHTTPRequest *)request
要测试连接,您可以这样做:
-(void)requestFinished:(ASIHTTPRequest *)request{
if(request.responseStatusCode==200)
{
//successfull connection, do what you need
}
else {
//failed request sent, display the correspondant error
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Your app name"
message:@"Unexpected error"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
}
}
关于Web 服务应用程序的 iOS 应用程序架构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6608595/