美文网首页iOS技术类
AFNetWorking Crash: Invalid para

AFNetWorking Crash: Invalid para

作者: OnePi | 来源:发表于2019-08-15 09:59 被阅读0次

    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]];
    

    相关文章

      网友评论

        本文标题:AFNetWorking Crash: Invalid para

        本文链接:https://www.haomeiwen.com/subject/sbeajctx.html