AFNetWorking Crash: Invalid parameter not satisfying: URLString

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]];
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。