Socket.io-client-swift: Self Signed Cert

Created on 17 Mar 2016  ·  14Comments  ·  Source: socketio/socket.io-client-swift

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)

Most helpful comment

@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!))
    }

All 14 comments

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.

Ok, thank you

@dani5447 hello ,How you solve?

@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!))
    }

thanks!

thanks!

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

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

I got it working.

@githubforIT how to fix it?

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

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

@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?

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}];
Was this page helpful?
0 / 5 - 0 ratings

Related issues

Seonift picture Seonift  ·  4Comments

MD04-TanTan picture MD04-TanTan  ·  5Comments

jetricy picture jetricy  ·  5Comments

janiduw picture janiduw  ·  5Comments

alionthego picture alionthego  ·  4Comments