React-native fetch() from https server with self-signed certificate

前端 未结 7 1287
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-30 01:39

I\'m trying to communicate with https server having self-signed certificate.

I can do this from .NET application (using ServicePointManager.ServerCertificateValidati

7条回答
  •  时光说笑
    2020-11-30 02:17

    Piggybacking of @Santiago Jimenez Wilson's answer here.

    Obviously patching react-native itself is pretty dirty so we took the suggested override and extracted it into a category.

    Just create a new file called RCTHTTPRequestHandler+yourPatchName.m somewhere in your project:

    //
    //  RCTHTTPRequestHandler+yourPatchName
    //
    
    #import "RCTBridgeModule.h"
    #import "RCTHTTPRequestHandler.h"
    
    @implementation RCTHTTPRequestHandler(yourPatchName)
    
    - (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler
    {
      completionHandler(NSURLSessionAuthChallengeUseCredential, [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]);
    }
    @end
    

    Next step would be to then differ between dev and prod and only overload the method for dev.

提交回复
热议问题