美文网首页ios developers
修改pod的第三方库的代码

修改pod的第三方库的代码

作者: Mage | 来源:发表于2022-11-14 16:20 被阅读0次

    原理:在pod install完成后执行脚本修改代码。

    例子:在podfile文件的最后面添加以下代码

    post_install do |installer|
      
      find_and_replace("Pods/SDWebImage/SDWebImage/Core/SDWebImageDownloaderOperation.m",
          "[self callCompletionBlocksWithError:[NSError errorWithDomain:SDWebImageErrorDomain code:SDWebImageErrorBadImageData userInfo:@{NSLocalizedDescriptionKey : description}]];", "[self callCompletionBlocksWithError:[NSError errorWithDomain:SDWebImageErrorDomain code:SDWebImageErrorBadImageData userInfo:@{NSLocalizedDescriptionKey : description,SDWebImageErrorResponse:imageData?:[NSData data]}]];")
    
          find_and_replace("Pods/SDWebImage/SDWebImage/Core/SDWebImageError.h","FOUNDATION_EXPORT NSErrorDomain const _Nonnull SDWebImageErrorDomain;\n\n", "FOUNDATION_EXPORT NSErrorDomain const _Nonnull SDWebImageErrorDomain;\nFOUNDATION_EXPORT NSErrorDomain const _Nonnull SDWebImageErrorResponse;\n\n\n")
          
          find_and_replace("Pods/SDWebImage/SDWebImage/Core/SDWebImageError.m","NSErrorDomain const _Nonnull SDWebImageErrorDomain = @\"SDWebImageErrorDomain\";\n\n", "NSErrorDomain const _Nonnull SDWebImageErrorDomain = @\"SDWebImageErrorDomain\";\nNSErrorDomain const _Nonnull SDWebImageErrorResponse = @\"SDWebImageErrorResponse\";\n\n\n")
    end
    
    
    def find_and_replace(dir, findstr, replacestr)
      Dir[dir].each do |name|
        FileUtils.chmod("+w",name) #add
          text = File.read(name)
          replace = text.gsub(findstr,replacestr)
          if text != replace
             puts "Fix: " + name
             File.open(name, "w") { |file| file.puts replace }
             STDOUT.flush
          end
      end
      Dir[dir + '*/'].each(&method(:find_and_replace))
    end
    
    

    相关文章

      网友评论

        本文标题:修改pod的第三方库的代码

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