Answer the question
In order to leave comments, you need to log in
How to fix error when checking value in NSURLSession in SWIFT?
Please tell me, when processing json in NSURLSession in a certain state, you need to display an error.
I do it through AlertController, but it constantly pours in.
Here is Alert itself
func dispayMyAlertMassage (userMessage : String)
{
var myAlert = UIAlertController(title: "Ошибка", message: userMessage, preferredStyle: UIAlertControllerStyle.Alert)
let okAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil)
myAlert.addAction(okAction)
self.presentViewController(myAlert,animated: true, completion : nil)
}
@IBAction func LoginButton(sender: AnyObject){
// сохраняем имя пользователя
let userName = nameText.text
// сохраняем пароль пользователя
let userPassword = PasswordText.text
// ссылка на класс
let myUrl = NSURL ( string: "http://localhost:8080/test1d/directURL?name="+userName+"&pass="+userPassword);
let request = NSMutableURLRequest(URL: myUrl);
request.HTTPMethod = "GET";
if userName.isEmpty & userPassword.isEmpty
{
self.dispayMyAlertMassage ("Введите данные пользователя!");
}
else
{
let task = NSURLSession.sharedSession().dataTaskWithRequest(request)
{
data,response,error in
if error != nil {
println("error=\(error)")
}
var err: NSError?
var json = NSJSONSerialization.JSONObjectWithData (data, options: .MutableContainers, error: &err) as NSDictionary
let parseJSON = json["ID"] as? NSInteger
println("parse id:\(parseJSON )")
if parseJSON != -1
{
//auth successesfull
NSUserDefaults.standardUserDefaults().setBool(true, forKey:"isUserLoggedIn");
NSUserDefaults.standardUserDefaults().synchronize();
self.dismissViewControllerAnimated(true, completion: nil)
}
else
{
self.dispayMyAlertMassage ("Введите данные пользователя!"); -- тут система ломается
}
}
task.resume()
}
2015-08-03 17:11:01.440 VinciMobile[5879:423254] *** Assertion failure in -[UIKeyboardTaskQueue waitUntilAllTasksAreFinished], /SourceCache/UIKit_Sim/UIKit-3318/Keyboard/UIKeyboardTaskQueue.m:374
2015-08-03 17:11:01.444 VinciMobile[5879:423254] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UIKeyboardTaskQueue waitUntilAllTasksAreFinished] may only be called from the main thread.'
self.presentViewController(myAlert,animated: true, completion : nil)
Answer the question
In order to leave comments, you need to log in
My own director.
That's right.
dispatch_async(dispatch_get_main_queue(), {
self.dispayMyAlertMassage ("Неверные регистрационные данные");
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question