AFNetWorking出現的崩潰報錯:
[Bugly] Trapped uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: URLString'
原因:是AFNetWorking內部有一個這個方法:
NSMutableURLRequest *request = [self.requestSerializer requestWithMethod:method URLString:[[NSURL URLWithString:URLString relativeToURL:self.baseURL] absoluteString] parameters:parameters error:&serializationError];
如果URLString含有非法字符時,比如前后有空格。這個方法[NSURL URLWithString:URLString relativeToURL:self.baseURL]會返回nil,從而在方法內部的NSParameterAssert(URLString);處拋出異常,導致崩潰:
(lldb) po [[NSURL URLWithString:URLString relativeToURL:self.baseURL] absoluteString]
nil
(lldb) po [NSURL URLWithString:URLString relativeToURL:self.baseURL]
nil
(lldb) po [NSURL URLWithString:URLString]
nil
(lldb) po URLString
http://10.161.0.169:8092/Spring/PayV647/do
解決方案:傳入URLString之前,先將其編碼:
URLString = [URLString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];