Sam*_*Sam 7 iphone networking ios
当iPhone连接到无线路由器,并且该路由器未连接到互联网时?我需要显示一条错误消息,说"检查你的互联网连接"我试过Reachability示例代码.但没有运气, http://developer.apple.com/library/ios/#samplecode/Reachability/Listings/ReadMe_txt.html
当我在手机中禁用WIFI它工作正常时,我尝试在Objective C中检查互联网连接示例代码"isDataSourceAvailable"即使它没有工作,任何人都可以帮我解决这个问题,真的很适合.
zou*_*oul 10
你可以这样做:
+ (BOOL) pingServer
{
NSURL *url = [NSURL URLWithString:@"http://some-url-that-has-to-work/"];
NSURLRequest *request = [NSMutableURLRequest requestWithURL:url];
NSHTTPURLResponse *response = nil;
[NSURLConnection sendSynchronousRequest:request
returningResponse:&response error:NULL];
return (response != nil);
}
Run Code Online (Sandbox Code Playgroud)
这是一个同步请求,因此它将阻止当前线程.您可以在后台线程中执行整个网络检查,以便阻止它无关紧要,或者您可以异步发送请求.此外,您可能希望将HTTP方法设置为HEAD,这样您就不会下载整个资源.
| 归档时间: |
|
| 查看次数: |
6426 次 |
| 最近记录: |