Error Domain=NSURLErrorDomain Code=-1003 "A server with the specified hostname could not be found." UserInfo=0x168d9430 {NSErrorFailingURLStringKey=http://www.cndoa.com/server/service/get_shop_nearby.php, NSErrorFailingURLKey=http://www.cndoa.com/server/service/get_shop_nearby.php, NSLocalizedDescription=A server with the specified hostname could not be found., NSUnderlyingError=0x16840e10 "A server with the specified hostname could not be found.}
當iPhone將wifi的DNS設置為114.114.114.114時, 每過1~2小時,在請求服務器時就會出現以上錯誤, 由于這個錯誤, 蘋果連續拒絕了我們的App兩次,這個問題是DNS的域名解析錯誤, 安卓訪問都沒問題, 但是iPhone總會報這個錯誤,我用的AFNetworking3.1(這個問題和什么網絡請求是沒關系的), 和服務端也沒什么關系, 以我目前的了解,和前端的關系也不大, 只是前端可以做一些補救措施.目前, 我做了以下的處理,App本周已成功上線了. 當你遇到-1003錯誤時,改用ip再訪問一下服務器
AFHTTPSessionManager *session = [AFHTTPSessionManager manager];
session.requestSerializer = [AFJSONRequestSerializer serializer];
session.responseSerializer = [AFHTTPResponseSerializer serializer];
// 域名請求
NSString *urlStr = @"https://github.com/user/12345678";
[session GET:urlStr parameters:nil progress:^(NSProgress * _Nonnull uploadProgress) {
// nil
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
// 域名請求成功, 處理數據
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
// 域名請求失敗
if (error.code == -1003) {
// 找不到指定域名的主機, 通常為域名解析錯誤, 改為ip訪問
NSString *ipUrl = @"https://192.23.59.136/user/12345678";
AFHTTPSessionManager *ipSession = [AFHTTPSessionManager manager];
ipSession.requestSerializer = [AFJSONRequestSerializer serializer];
ipSession.responseSerializer = [AFHTTPResponseSerializer serializer];
[ipSession GET:ipUrl parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
//nil
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
// ip請求成功, 處理數據
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
// ip請求失敗
}];
}
}];
總結: 遇到這個DNS的域名解析錯誤的問題時, 可以使用該方式繞過DNS的域名解析, 而直接使用ip訪問,期待以后能有更加完善的解決方案, 能和大家多討論,分享.