-1

Relying on the subject seen in this topic: Check if NSURL returns 404

How do I check if a page returns 404 in Swift?

I'm a new iOS developer and I don't have enough knowledgement to translate from Objective-C to Swift (I've started on Swift..)

Is there anyone that can help me figuring out how to do this operation in Swift?

Thank you very much

Community
  • 1
  • 1
ernestocattaneo
  • 1,197
  • 5
  • 18
  • 30
  • Do you implemented any code ? If yes, please add that – Midhun MP Jan 08 '15 at 10:37
  • @MidhunMP Just think about a simple connection to a website.. A webview wants to load a page, but before loading... it must check the availability... Anyway the answer of Rashad is a perfect example.. – ernestocattaneo Jan 08 '15 at 10:59

1 Answers1

0
let urlPath: String = "www.aaaa.com/test"
var url: NSURL = NSURL(string: urlPath)!
var request1: NSURLRequest = NSURLRequest(URL: url)
var response: AutoreleasingUnsafeMutablePointer<NSURLResponse?>=nil
var error: NSErrorPointer = nil
var dataVal: NSData =  NSURLConnection.sendSynchronousRequest(request1, returningResponse: response, error:nil)!
var err: NSError
println(response)
//response.statusCode is what you want
var jsonResult: NSDictionary = NSJSONSerialization.JSONObjectWithData(dataVal, options: NSJSONReadingOptions.MutableContainers, error: nil) as NSDictionary
println("Synchronous\(jsonResult)")

You can check :

if response.statuCode == 200

Or

if response.statuCode == 404

Hope this helps.. :)

Rashad
  • 11,057
  • 4
  • 45
  • 73
  • 1
    Thank you very much for your help. But.. I've paisted this in a new project and changed the URL. The problem is that "response.statusCode" looks like it does not exists... infact I can't run the project because that line gives me an error: AutoreleasingUnsafeMutablePointer does not have a member named 'statusCode' ...what you say?? thanks again – ernestocattaneo Jan 08 '15 at 10:57