Reputation: 11
anyone who knows about retrieving data from a database and using it to check if a user exits (sign in) please help. My head is wrecked and unfortunately my coding is not quite good enough to fix the problem!
do {
if let json = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as? NSDictionary
{
let parseJSON = json
let userID = parseJSON["userId"] as? String //dictionary object that uses a key
if(userID != nil)
{
//everything is fine and take user to main page
//instantiate the view controller
let profilePage = self.storyboard?.instantiateViewControllerWithIdentifier("ProfilePageViewController") as! ProfilePageViewController
//wrap it in nav controller
let profilePageNav = UINavigationController(rootViewController: profilePage)
//take user to this page
//window root view controller- existing is replaced and no back button
let appDelegate = UIApplication.sharedApplication().delegate
appDelegate?.window??.rootViewController = profilePageNav
}else{
//display the alert message from the php file
let errorMessage = parseJSON["message"] as? String
let myAlert = UIAlertController(
title: "Alert!",
message: errorMessage,
preferredStyle: UIAlertControllerStyle.Alert)
//create a button action -
let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil)
//adding ok button and presenting to a user and once ok button is pressed- dismiss the view
myAlert.addAction(okAction)
self.presentViewController(myAlert, animated: true, completion:nil)
}
}//end of if let json
}//end of do
catch let error as NSError {
print(error.localizedDescription)
}
print("success")
}//end of dispatch_async
}).resume()//end of request
}//end of action sign in
I think the issue is with the if let json = try line, the data is not in the correct format. My php script is working well and I can pass parameters in the browser and all seems ok. The key is correct (userId). I think another issue is with let userID = parseJSON["userId"] as? String
if I have made a request for the data as an NSDictionary - how can I convert this info to type String (I'm sure you can't anyways) Sorry - a lot of questions. I need String representations to use in another view controller. I have been trying this for ages and I can't get it to work so I would really appreciate some help- When i run the code- it bypasses the "do" block and goes straight to the .localizedDescription
Thank you for talking the time to read my v long question!
Upvotes: 1
Views: 4644
Reputation: 447
while sending a request "request.setValue("application/json", forHTTPHeaderField: "Accept")" resolved my error.
Upvotes: 0
Reputation: 4437
The issue is definitely in NSJSONSerialization.JSONObjectWithData which doesn't like your JSON for some reason.
Try cleaning your JSON before feeding it to NSJSONSerialization:
var dataString = String(data: data, encoding: NSUTF8StringEncoding)!
dataString = dataString.stringByReplacingOccurrencesOfString("\\'", withString: "'")
let cleanData: NSData = dataString.dataUsingEncoding(NSUTF8StringEncoding)!
Upvotes: 1