Inside my completion handler, I am trying to return the http response code in the same way as alertin JS. As soon as I get this sorting, I hope to improve my if statement to determine the end user connection problem. I watched this article.
http://www.ioscreator.com/tutorials/display-an-alert-view-in-ios8-with-swift
I also saw this answer A simple application delegation method to show the UIAlertController (in Swift) , but I get a similar method about what is windownot named.
My code silently looks like this:
class NewsViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
var tableView: UITableView!
var titleItems: NSMutableArray = []
var descritpionItems: NSMutableArray = []
class RemoteAPI {
let baseUrl = "http://api.dev/web/"
var newsTitle: NSMutableArray = []
var newsDescription: NSMutableArray = []
func getData(completionHandler: ((NSArray?, NSError?) -> Void)) -> Void {
let url = NSURL(string: baseUrl + "news")
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithURL(url!, completionHandler: { (data, response, error) -> Void in
if (error != nil) {
return completionHandler(nil, error)
}
if let httpResponse = response as? NSHTTPURLResponse {
if httpResponse.statusCode != 200 {
let alertController = UIAlertController(title: "Oh No!!", message: "Connection error",preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.Default,handler: nil))
UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(alertController, animated: true, completion: nil)
} else {
let json = JSON(data: data)
for var i = 0; i < json.count; ++i {
var title = json[i]["title"].string
var blurb = json[i]["description"].string
self.newsTitle.addObject(title!)
self.newsDescription.addObject(blurb!)
}
if (error != nil) {
return completionHandler(nil, error)
} else {
return completionHandler([self.newsTitle,self.newsDescription], nil)
}
}
}
})
task.resume()
}
}
var api = RemoteAPI()
override func viewDidLoad() {
super.viewDidLoad()
self.view.frame = CGRect(x: 5, y: 75, width: 365, height: 480)
self.tableView = UITableView(frame:self.view!.frame)
self.tableView!.delegate = self
self.tableView!.dataSource = self
self.tableView!.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
self.view?.addSubview(self.tableView)
api.getData({data, error -> Void in
if (data != nil) {
self.titleItems = self.api.newsTitle
self.descritpionItems = self.api.newsDescription
self.tableView!.reloadData()
} else {
println("API failed :-(")
println(error)
}
})
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return self.titleItems.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
var cell:UITableViewCell=UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "mycell")
if let newsTitle = self.titleItems[indexPath.row] as? NSString {
cell.textLabel?.text = newsTitle
}
if let newsDesc = self.descritpionItems[indexPath.row] as? NSString {
cell.detailTextLabel?.text = newsDesc
}
return cell
}
}
I get an error message NewsViewController.RemoteAPI does not have member named presentViewController. I understand why, just not sure what to call it in a class new to iOS