0

Hi in my console OS X application i try to connect a web service which is hosting https. I can't connect that web service with code below. The strange thing , if i use same class in normal cocoa application. it works fine. Do you have any idea what is the problem?

import Foundation
class Service : NSObject , NSURLSessionDelegate {
func httpGet(request: NSMutableURLRequest!) {
    let configuration =
    NSURLSessionConfiguration.defaultSessionConfiguration()

    let session = NSURLSession(configuration: configuration, delegate: self, delegateQueue:NSOperationQueue.mainQueue())

    let task = session.dataTaskWithRequest(request){
        (data, response, error) -> Void in
        print("ok data taken")
    }
    task.resume()
}
func URLSession(session: NSURLSession, didReceiveChallenge challenge: NSURLAuthenticationChallenge, completionHandler: (NSURLSessionAuthChallengeDisposition, NSURLCredential?) -> Void) {
    completionHandler(NSURLSessionAuthChallengeDisposition.UseCredential, NSURLCredential(forTrust: challenge.protectionSpace.serverTrust!))
}}
let service = Service()
 service.httpGet(NSMutableURLRequest(URL: NSURL(string: "http://www.google.com")!))
sleep(10)
  • 3
    `I can't connect` What does it mean: nothing happens? No error message? No symptom at all? Please give details. Thanks. – Eric Aya Feb 03 '16 at 16:24
  • Yes nothing happens, no any errors. Just i m suspecious about threat. Since this is a console application , maybe main threat already executed before answer. – SerhatTopkaya Feb 03 '16 at 17:01
  • Indeed, it could be the cause. Related: http://stackoverflow.com/questions/25126471/cfrunloop-in-swift-command-line-program – Eric Aya Feb 03 '16 at 17:24
  • Do you *want* us to help? Should we just guess..?? How far does it get, what's in the debugger console, dump the contents of the created objects if any, add more debugging statements, etc.... – cacau Feb 04 '16 at 07:42
  • @cacau i changed sample code. Thanks. – SerhatTopkaya Feb 04 '16 at 09:38
  • Why don't you try the solutions I've linked?? Martin R and Joseph have good answers over there. If it works for you then we can close this question as duplicate, and if it doesn't then we can help further. // Also, your `sleep` here is inefficient, it blocks the main thread so it doesn't do what you hope at all here. – Eric Aya Feb 04 '16 at 12:23
  • First of all thanks @EricD. I solve my problem with `delegateQueue:NSOperationQueue.mainQueue()` to `delegateQueue:nil` and i change `sleep` to `semaphore` . Now it works well. – SerhatTopkaya Feb 05 '16 at 09:46

1 Answers1

1

I solved that problem with below code :)

class Service : NSObject , NSURLSessionDelegate {

func httpGet(request: NSMutableURLRequest!) {

    let s  = dispatch_semaphore_create(0)

    let configuration =
    NSURLSessionConfiguration.defaultSessionConfiguration()

    let session = NSURLSession(configuration: configuration, delegate: self, delegateQueue:nil)

    let task = session.dataTaskWithRequest(request){
        (data, response, error) -> Void in
        dispatch_semaphore_signal(s)
        print("ok data taken")
    }
    task.resume()
    dispatch_semaphore_wait(s, dispatch_time(DISPATCH_TIME_NOW, AppConstants.Values.SemephoreTimeOut))
}
func URLSession(session: NSURLSession, didReceiveChallenge challenge: NSURLAuthenticationChallenge, completionHandler: (NSURLSessionAuthChallengeDisposition, NSURLCredential?) -> Void) {
    completionHandler(NSURLSessionAuthChallengeDisposition.UseCredential, NSURLCredential(forTrust: challenge.protectionSpace.serverTrust!))
}}

Have a nice day !