2016-08-04 134 views
0

我新手用於開發ios應用程序,我試圖連接https web服務器,我遇到了問題我無法連接自簽名證書,所以我搜索了網絡因爲我已經在下面給出的代碼其工作正常。但其工作異步mode.so我需要直到完成任務等待並返回data.Please放棄任何想法?/如何使用NSOperationQueue進行同步請求?

func requestAsynChronousData(request: NSURLRequest)->NSData? { 
    let data: NSData? = nil 
    let configuration = NSURLSessionConfiguration.defaultSessionConfiguration() 
    let session = NSURLSession(configuration: configuration, delegate: self, delegateQueue:NSOperationQueue.mainQueue()) 
    let task = session.dataTaskWithRequest(request){ (data: NSData?, response: NSURLResponse?, error: NSError?) -> Void in 
     if error == nil { 
     print("RESULTTTT\(data)") 
     } 
    } 
    task.resume() 
    return data; 
} 

func URLSession(session: NSURLSession, didReceiveChallenge challenge: NSURLAuthenticationChallenge, completionHandler: (NSURLSessionAuthChallengeDisposition, NSURLCredential?) -> Void) { 
    print("WAITING!!!!!!!!!!!!!COMPLETIONHANDLER") 
    completionHandler(NSURLSessionAuthChallengeDisposition.UseCredential, NSURLCredential(forTrust: challenge.protectionSpace.serverTrust!)) 
} 
+0

除非您在輔助線程上這樣做,否則您不應該*執行同步URL請求。如果你這樣做,你基本上可以保證從應用商店被拒絕,因爲iOS會自動殺死阻塞主線程的應用超過幾秒的應用,並且HTTP超時需要長達90秒。任何可以同步完成的事情也可以異步完成。你只需要圍繞它進行設計。例如,不是編寫一個返回值的函數,而是編寫一個函數,該函數接收一個塊並在請求完成時將該值傳遞給該塊。 – dgatwood

回答

0

您需要添加dispatch_semaphore_create到你的代碼像下面

func requestAsynChronousData(request: NSURLRequest)->NSData? { 
    let data: NSData? = nil 
    let configuration = NSURLSessionConfiguration.defaultSessionConfiguration() 

    let semaphore = dispatch_semaphore_create(0) 

    let session = NSURLSession(configuration: configuration, delegate: self, delegateQueue:NSOperationQueue.mainQueue()) 
    let task = session.dataTaskWithRequest(request){ (data: NSData?, response: NSURLResponse?, error: NSError?) -> Void in 

     if error == nil { 
     print("RESULTTTT\(data)") 
     } 

     dispatch_semaphore_signal(semaphore) 
    } 
    task.resume() 

    dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER); 
    return data; 
} 

但這方式將阻止您的主線程,而您的完成處理程序將完成

+0

嗨它沒有工作。仍在等待..它不會返回數據 – vara

+0

做完你的完成塊呼叫? – iSashok

+0

是的..它已經等待...不退出.. – vara

0

我找到了解決方案。

func requestAsynChronousData(request: NSURLRequest)->NSData? { 
let data: NSData? = nil 
let configuration = NSURLSessionConfiguration.defaultSessionConfiguration() 
let session = NSURLSession(configuration: configuration, delegate: self, delegateQueue:nil) 
let task = session.dataTaskWithRequest(request){ (data: NSData?, response: NSURLResponse?, error: NSError?) -> Void in 
    if error == nil { 
    data=taskData 
    print("RESULTTTT\(data)") 
    } 
    dispatch_semaphore_signal(semaphore) 
} 
task.resume() 
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER); 
return data; 
} 

func URLSession(session: NSURLSession, didReceiveChallenge challenge: NSURLAuthenticationChallenge, completionHandler: (NSURLSessionAuthChallengeDisposition, NSURLCredential?) -> Void) { 
print("WAITING!!!!!!!!!!!!!COMPLETIONHANDLER") 
completionHandler(NSURLSessionAuthChallengeDisposition.UseCredential, NSURLCredential(forTrust: challenge.protectionSpace.serverTrust!)) 
} 
0

或者,你可以在你的隊列操作的最大數量設置爲1。

let operationQueue = NSOperationQueue() 
operationQueue.maxConcurrentOperationCount = 1 

let session = NSURLSession(configuration: configuration, delegate: self, delegateQueue:operationQueue) 
let task = session.dataTaskWithRequest(request){ (data: NSData?, response: NSURLResponse?, error: NSError?) -> Void in 

    if error == nil { 
    print("RESULTTTT\(data)") 
    } 
} 

operationQueue.addOperation(task) 

這將迫使運營此隊列同步採取行動,而無需使用信號燈,這將使NSOperationQueue清理up本身