iOS - NSURLConnection finished with error - code -1002

 

網絡請求會異常,好比報錯:Error domain: @"NSURLErrorDomain" - code: 1002

在回調的地方打一個斷點,而後用命令行查看下什麼錯誤: po error
當錯誤是:**[Error Domain=NSURLErrorDomain Code=-1002 ] "unsupported URL" **
*Error Domain=NSURLErrorDomain Code=-1002 "unsupported URL" UserInfo={NSLocalizedDescription=unsupported URL, NSUnderlyingError=0x7fa9b1d06120 {Error Domain=kCFErrorDomainCFNetwork Code=-1002 "(null)"}}  *  的時候是url編碼問題,處理一下URL編碼就行了

我這邊是後臺返回的拼接的接口多了「/」,致使統一的域名格式不統一

http://gwtest.opg-iot.com:8000//api-sdeventApplication/images/handle/event_result/event_result_beimg_20190522093224_0.jpeg?Authenticator=eyJhbGciOiJIUzI1NiJ9.eyJyZWFsTmFtZSI6bnVsbCwibG9naW5OYW1lIjoiU0Q3MzkzIiwidXNlclR5cGUiOiIxIiwicGxhdGZvcm1JZCI6MTAsImV4cCI6MTU1OTA5MzkxOSwidXNlcklkIjo3MzkzfQ.KieGb8LUsgpvn9BU9jjSiNSNn12irclYKiPH_s0Uqgkapi

 

 

處理方法:


iOS 8 要用utf8轉碼:

urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];


iOS 8 之後要用這個方法:

urlString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:@"#%^{}\"[]|\\<> "].invertedSet];
相關文章
相關標籤/搜索