今天有一個線上bug,是分配給提供H5的團隊的,可是後臺查不出來緣由。因而讓前端iOS幫忙查一查緣由。前端
今天,交給我來幫忙查緣由,可是問題在網絡好的狀態下並沒必要現,很難去定位問題的根本緣由。最後只能到測試旁邊鏈接測試專用的慢網環境,而後才能必現。nginx
剛進入界面時是加載一個H5頁面,當點擊H5頁面上的某個按鈕的時候,會經過webview攔截到scheme,而後去走接口請求數據,獲得數據後再加載H5頁面。此時,走了webview加載失敗的回調。web
加斷點,打印出來的error信息是:網絡
Error Domain=NSURLErrorDomain Code=-999 「The operationcouldn’t becompleted.
而後,筆者進入NSURLError.h中查看-999表明什麼對應的key是什麼:dom
NS_ENUM(NSInteger) { NSURLErrorUnknown = -1, NSURLErrorCancelled = -999, NSURLErrorBadURL = -1000, NSURLErrorTimedOut = -1001, NSURLErrorUnsupportedURL = -1002, NSURLErrorCannotFindHost = -1003, NSURLErrorCannotConnectToHost = -1004, NSURLErrorNetworkConnectionLost = -1005, NSURLErrorDNSLookupFailed = -1006, NSURLErrorHTTPTooManyRedirects = -1007, NSURLErrorResourceUnavailable = -1008, NSURLErrorNotConnectedToInternet = -1009, NSURLErrorRedirectToNonExistentLocation = -1010, NSURLErrorBadServerResponse = -1011, NSURLErrorUserCancelledAuthentication = -1012, NSURLErrorUserAuthenticationRequired = -1013, NSURLErrorZeroByteResource = -1014, NSURLErrorCannotDecodeRawData = -1015, NSURLErrorCannotDecodeContentData = -1016, NSURLErrorCannotParseResponse = -1017, NSURLErrorAppTransportSecurityRequiresSecureConnection NS_ENUM_AVAILABLE(10_11, 9_0) = -1022, NSURLErrorFileDoesNotExist = -1100, NSURLErrorFileIsDirectory = -1101, NSURLErrorNoPermissionsToReadFile = -1102, NSURLErrorDataLengthExceedsMaximum NS_ENUM_AVAILABLE(10_5, 2_0) = -1103, // SSL errors NSURLErrorSecureConnectionFailed = -1200, NSURLErrorServerCertificateHasBadDate = -1201, NSURLErrorServerCertificateUntrusted = -1202, NSURLErrorServerCertificateHasUnknownRoot = -1203, NSURLErrorServerCertificateNotYetValid = -1204, NSURLErrorClientCertificateRejected = -1205, NSURLErrorClientCertificateRequired = -1206, NSURLErrorCannotLoadFromNetwork = -2000, // Download and file I/O errors NSURLErrorCannotCreateFile = -3000, NSURLErrorCannotOpenFile = -3001, NSURLErrorCannotCloseFile = -3002, NSURLErrorCannotWriteToFile = -3003, NSURLErrorCannotRemoveFile = -3004, NSURLErrorCannotMoveFile = -3005, NSURLErrorDownloadDecodingFailedMidStream = -3006, NSURLErrorDownloadDecodingFailedToComplete =-3007, NSURLErrorInternationalRoamingOff NS_ENUM_AVAILABLE(10_7, 3_0) = -1018, NSURLErrorCallIsActive NS_ENUM_AVAILABLE(10_7, 3_0) = -1019, NSURLErrorDataNotAllowed NS_ENUM_AVAILABLE(10_7, 3_0) = -1020, NSURLErrorRequestBodyStreamExhausted NS_ENUM_AVAILABLE(10_7, 3_0) = -1021, NSURLErrorBackgroundSessionRequiresSharedContainer NS_ENUM_AVAILABLE(10_10, 8_0) = -995, NSURLErrorBackgroundSessionInUseByAnotherProcess NS_ENUM_AVAILABLE(10_10, 8_0) = -996, NSURLErrorBackgroundSessionWasDisconnected NS_ENUM_AVAILABLE(10_10, 8_0)= -997, };
找到了吧!!!NSURLErrorCancelled就是-999,它表明請求被取消的意思。測試
出現NSURLErrorDomain Code=-999的根本緣由是什麼呢?其實就是由於webview在以前的請求尚未加載完成,下一個請求發起了,此時webview會取消掉以前的請求,所以會回調到失敗這裏。ui
所以,在處理Webview的加載失敗的回調時,要注意攔截掉被取消的請求。url
在webview加載失敗時,添加以下代碼來判斷:spa
- (void)webView:(UIWebView *)webViewdidFailLoadWithError:(NSError *)error { [self stopAnimating]; // 若是是被取消,什麼也不幹 if([errorcode] == NSURLErrorCancelled) { return; } // 後續失敗處理 }
若是你們遇到一樣的問題,請不要着急,這個bug不必定是後臺的,也不必定是前端的,所以彼此應該要互相配合,共同找到問題的根本緣由。code
因爲安卓也有一樣的問題,可是復現率沒有iOS的高,在筆者找出根本緣由後,H5人員與安卓端描述,但願安卓端也能統一改,可是描述不清楚,致使安卓這邊有意見。因而叫筆者過來幫忙解釋緣由,講了半天,安卓的leader說不可能~不會的~不該該~不影響~
發現跟安卓溝通要是不懂一點安卓,真心容易被人忽悠。固然,最後仍是要改的,事實都擺在面前了,還能有什麼藉口能夠逃避!
但願你們在遇到一樣的問題時,淡定!溝通協做共同解決問題。