Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

ios - how to check if rtmp or hls urls are exist or they'll give 404 error in swift

I need to parse some data from rss and open related links from parsed rss in swift 2, for example i want to check this link is valid or not:

rtmp://185.23.131.187:1935/live/jomhori1

or this one :

http://185.23.131.25/hls-live/livepkgr/_defint_/liveevent/livestream.m3u8

My code to check the validation of the url :

let urlPath: String = "http://185.23.131.25/hls-live/livepkgr/_defint_/liveevent/livestream.m3u8"
                            let url: NSURL = NSURL(string: urlPath)!
                            let request: NSURLRequest = NSURLRequest(URL: url)
                            let response: AutoreleasingUnsafeMutablePointer<NSURLResponse?>=nil

                            var valid : Bool!

                            do {
                                _ = try NSURLConnection.sendSynchronousRequest(request, returningResponse: response)
                            } catch {
                                print("404")
                                valid = false
                            }

I've searched the web but all the method I found wasn't helpful for my issue.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The answer by @sschale is nice, but NSURLConnection is deprecated, it's better to use NSURLSession now.

Here's my version of an URL testing class:

class URLTester {

    class func verifyURL(urlPath: String, completion: (isOK: Bool)->()) {
        if let url = NSURL(string: urlPath) {
            let request = NSMutableURLRequest(URL: url)
            request.HTTPMethod = "HEAD"
            let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { (_, response, error) in
                if let httpResponse = response as? NSHTTPURLResponse where error == nil {
                    completion(isOK: httpResponse.statusCode == 200)
                } else {
                    completion(isOK: false)
                }
            }
            task.resume()
        } else {
            completion(isOK: false)
        }
    }

}

And you use it by calling the class method with a trailing closure:

URLTester.verifyURL("http://google.com") { (isOK) in
    if isOK {
        print("This URL is ok")
    } else {
        print("This URL is NOT ok")
    }
}

Swift 3.0 with URLSession

class URLTester {

  class func verifyURL(urlPath: String, completion: @escaping (_ isOK: Bool)->()) {
    if let url = URL(string: urlPath) {
      var request = URLRequest(url: url)
      request.httpMethod = "HEAD"
      let task = URLSession.shared.dataTask(with: request, completionHandler: { (data, response, error) in
        if let httpResponse = response as? HTTPURLResponse, error == nil {
          completion(httpResponse.statusCode == 200)
        } else {
          completion(false)
        }
      })
      task.resume()
    } else {
      completion(false)
    }
  }
}

This is better than your answer because it only downloads the response headers instead of the whole page (also, it's better because asynchronous).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...