AFNetWorking Crash: Invalid para
2019-08-15 本文已影响0人
OnePi
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]];