我在iPhone应用程序中使用NSURLConnection,如下所示:
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest: request delegate: self];
请求已设置并正常工作,但我希望能够在没有可用连接的情况下向用户提供"连接不可用"消息.有没有快速的方法来确定iPhone上是否有可用的Internet连接?
像启动NSURLConnection后添加以下内容一样简单:
if (conn) { // do normal processing } else { // display connection not available message }
提前谢谢,本
这对我有用,取自苹果地震xml项目:
- (BOOL)isDataSourceAvailable { static BOOL checkNetwork = YES; if (checkNetwork) { // Since checking the reachability of a host can be expensive, cache the result and perform the reachability check once. checkNetwork = NO; Boolean success; const char *host_name = "twitter.com"; // your data source host name SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithName(NULL, host_name); SCNetworkReachabilityFlags flags; success = SCNetworkReachabilityGetFlags(reachability, &flags); _isDataSourceAvailable = success && (flags & kSCNetworkFlagsReachable) && !(flags & kSCNetworkFlagsConnectionRequired); CFRelease(reachability); } return _isDataSourceAvailable; }