socket.io-client-swift icon indicating copy to clipboard operation
socket.io-client-swift copied to clipboard

Self Signed Cert

Open dani5447 opened this issue 10 years ago • 14 comments

It's not working in my particular case for some reason, and I'm wondering if I'm not quite using the option correctly. Do I need to use .SessionDelegate as well? If so, do you have any examples?

Here is a code snippet, fairly basic:

    let socket = SocketIOClient(socketURL: NSURL(string: url)!, options: [.Log(true), .ForcePolling(true), .SelfSigned(true)])

    override func viewDidLoad() {
        super.viewDidLoad()

        //add socket event handlers
        self.addHandlers()

        //connect
        self.socket.connect()
    }

And the error code I still get despite using the .SelfSigned opts is below:

ERROR SocketEnginePolling: The certificate for this server is invalid. You might be connecting to a server that is pretending to be “url here” which could put your confidential information at risk. 2016-03-17 15:14:05.983 dev[11046:403951] LOG SocketEnginePolling: Created POST string: 1:1 2016-03-17 15:14:05.984 dev[11046:403951] LOG SocketEnginePolling: Doing polling request 2016-03-17 15:14:05.999 dev[11046:403967] NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9813)

dani5447 avatar Mar 17 '16 19:03 dani5447

Yeah, you'll probably need to use SessionDelegate as well. I don't have any examples of that, but you should be able find some online.

nuclearace avatar Mar 17 '16 20:03 nuclearace

Ok, thank you

dani5447 avatar Mar 17 '16 21:03 dani5447

@dani5447 hello ,How you solve?

hughdev avatar Mar 30 '16 05:03 hughdev

@j364960953 I had to

  • extend NSURLSessionDelegate on the class
  • use this instantiation: (the selfsigned and sessiondelegate options)
let socketClient = SocketIOClient(socketURL: NSURL(string: baseURL)!, options: [SocketIOClientOption.Log(true), SocketIOClientOption.ForcePolling(true), SocketIOClientOption.SelfSigned(true), SocketIOClientOption.SessionDelegate(self), SocketIOClientOption.ConnectParams(["__sails_io_sdk_version":"0.11.0"]),
            SocketIOClientOption.Cookies([currCookie])])
  • Add a session delegate method at the bottom of the class:
func URLSession(session: NSURLSession,
        task: NSURLSessionTask,
        didReceiveChallenge challenge: NSURLAuthenticationChallenge,
        completionHandler: (NSURLSessionAuthChallengeDisposition, NSURLCredential?)
        -> Void) {
            completionHandler(NSURLSessionAuthChallengeDisposition.UseCredential, NSURLCredential(forTrust: challenge.protectionSpace.serverTrust!))
    }

dani5447 avatar Apr 01 '16 00:04 dani5447

thanks!

hughdev avatar May 06 '16 02:05 hughdev

thanks!

Heiyuan avatar Oct 09 '16 03:10 Heiyuan

@dani5447 Thanks, this helped me .. 👍 :)

mohangslab083 avatar Dec 02 '16 12:12 mohangslab083

I am using this swift library in obj-c project. With respect to this issue, I have written this code as follows but my delegate never gets called. Any idea what could be missing?

socket = [[SocketIOClient alloc] initWithSocketURL:url config:@{@"log": @YES, @"forcePolling": @YES, @"selfSigned": @YES, @"sessionDelegate":self }];

Add here is my delegate.

  • (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler{ completionHandler(NSURLSessionAuthChallengeUseCredential, [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]); }

githubforIT avatar Mar 02 '17 08:03 githubforIT

I got it working.

githubforIT avatar Mar 02 '17 09:03 githubforIT

@githubforIT how to fix it?

hoangbossemo avatar Mar 06 '17 08:03 hoangbossemo

Actually, when I implemented the didReceiveChallenge delegate method in the AppDelegate and set the sessionDelegate to its instance, it started working.

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate]; socket = [[SocketIOClient alloc] initWithSocketURL:url config:@{@"log": @YES, @"forcePolling": @YES, @"selfSigned": @YES, @"sessionDelegate":appDelegate }];

githubforIT avatar Mar 06 '17 09:03 githubforIT

I want to try local notifications using SOCKETIO., when the app is running on simulator everything is fine. But when It's running on iPhone, when the app enters into background task suspends/ get paused. Please help me regarding this issue

ghost avatar Mar 06 '17 11:03 ghost

@githubforIT I tried to use the same method as you, however, I often get an AppDelegate retain message sent to deallocated instance error.

May I ask, Did you encounter an error on the same path as mine?

miketw2014 avatar Jul 17 '17 07:07 miketw2014

I am using this swift library in obj-c project. With respect to this issue, I have written this code as follows but my delegate never gets called. Any idea what could be missing?

socket = [[SocketIOClient alloc] initWithSocketURL:url config:@{@"log": @yES, @"forcePolling": @yES, @"selfSigned": @yES, @"sessionDelegate":self }];

Add here is my delegate.

  • (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler{ completionHandler(NSURLSessionAuthChallengeUseCredential, [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]); }

@githubforIT Thank you so much for pointing out to the sessionDelegate property, I got this working in my ViewController. I didn't have to create a reference to the AppDelegate as you did in https://github.com/socketio/socket.io-client-swift/issues/326#issuecomment-284339626.

The 3 changes I made to your solution are:

  1. Add the NSURLSessionDelegate to the ViewControllers interface:
@interface MainViewController () <NSURLSessionDelegate>
  1. Put a dash in front of the delegate method:
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler {
    // Trust self-signed HTTPS certificate:
    completionHandler(NSURLSessionAuthChallengeUseCredential, [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]);
}
  1. And to be complete for current readers, because we're almost 3 years ahead in time; use SocketManager instead of SocketIOClient to initiate the socket (this is due to the updated library, we use latest version 15.2.0):
@property (nonatomic) SocketManager *socketManager;

NSURL* url = [[NSURL alloc] initWithString:@"https://192.168.1.1:123"]; // The HTTPS location, can even be on a local network.
_socketManager = [[SocketManager alloc] initWithSocketURL:url config:@{@"log": @YES,
                                                                       @"forcePolling": @YES,
                                                                       @"selfSigned": @YES,
                                                                       @"sessionDelegate": self}];

funnel20 avatar Feb 13 '20 08:02 funnel20