Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Self Signed Cert #326

Closed
dani5447 opened this issue Mar 17, 2016 · 14 comments
Closed

Self Signed Cert #326

dani5447 opened this issue Mar 17, 2016 · 14 comments

Comments

@dani5447
Copy link

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)

@nuclearace
Copy link
Member

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.

@dani5447
Copy link
Author

Ok, thank you

@hughdev
Copy link

hughdev commented Mar 30, 2016

@dani5447 hello ,How you solve?

@dani5447
Copy link
Author

dani5447 commented Apr 1, 2016

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

@hughdev
Copy link

hughdev commented May 6, 2016

thanks!

1 similar comment
@Heiyuan
Copy link

Heiyuan commented Oct 9, 2016

thanks!

@mohangslab083
Copy link

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

@githubforIT
Copy link

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
Copy link

I got it working.

@hoangbossemo
Copy link

@githubforIT how to fix it?

@githubforIT
Copy link

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

@ghost
Copy link

ghost commented Mar 6, 2017

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

@miketw2014
Copy link

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

@funnel20
Copy link

funnel20 commented Feb 13, 2020

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 #326 (comment).

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants