2016-07-16 42 views
0

我在這個新的傢伙,但用於搜索特定的東西在這裏,因爲我找不到具體的答案,也許有人在這裏可以幫助。警報控制消息和NSURLSession

我基本上學習和建立一個應用程序。

目前我已經建立登錄,註冊視圖控制器等。 使用php和mysql服務器。

由於使用和在堆棧溢出採取的點點滴滴件IM

我所遇到的問題,以警示控制消息的問題,NSURLConnection的

現在我知道NSURLConnectionNSURLSession 但我取代不能實現它,它的相當難以解決這個問題。

我的代碼如下:

let request:NSMutableURLRequest = NSMutableURLRequest(URL: url) 
request.HTTPMethod = "POST" 
request.HTTPBody = postData 
request.setValue(postLength as String, forHTTPHeaderField: "Content-Length") 
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type") 
request.setValue("application/json", forHTTPHeaderField: "Accept") 

var reponseError: NSError? 
var response: NSURLResponse? 

var urlData: NSData? 
do { 
    urlData = try NSURLConnection.sendSynchronousRequest(request, returningResponse:&response) 
} catch let error as NSError { 
    reponseError = error 
    urlData = nil 
} 

這給了我這個問題

sendSynchronousRequest(_:returningResponse :)」在IOS 9.0已被否決:使用[NSURLSession dataTaskWithRequest:completionHandler:(見NSURLSession.h

我怎麼能實現這個與NSURLSession

第二個問題是與對外開放的警告消息

alertView.delegate = self是問題,我已經嘗試了幾種不同的東西像更換alertView,但它不工作,我嘗試alertController等沒有什麼作品:

 if let error = reponseError { 
      alertController.message = (error.localizedDescription) 
     } 

     alertView.delegate = self 
     alertController.addAction(OKAction) 
     self.presentViewController(alertController, animated: true) { } 
    } 
} catch { 
    let alertController:UIAlertController = UIAlertController(title: "Sign up Failed", message: "Server Error", preferredStyle: .Alert) 
    let OKAction = UIAlertAction(title: "OK", style: .Default) { (action) in } 
    alertController.addAction(OKAction) 
    self.presentViewController(alertController, animated: true) { } 
} 

的問題是:

使用未解決的標識符 'alertView'

這是一個名爲signup.VC的Swift文件。

+0

@HiDeo感謝您修改帖子,看起來我也必須學習堆棧溢出,以及如何使用它lol –

+0

請每個問題只發佈一個問題。 – rmaddy

回答

0

不是使用同步請求,而是創建一個異步請求,並在完成處理程序塊中放置當前在上面的catch塊結束後出現的所有代碼,以便在請求完成後執行。

我不知道你的第二個問題,因爲你沒有包括你聲明alertView變量的代碼。