Ios 如何检查rtmp或hls URL是否存在或它们是否';我将在swift中给出404错误
我需要解析rss中的一些数据,并在swift 2中打开解析rss中的相关链接, 例如,我想检查此链接是否有效:Ios 如何检查rtmp或hls URL是否存在或它们是否';我将在swift中给出404错误,ios,validation,url,rss,swift2,Ios,Validation,Url,Rss,Swift2,我需要解析rss中的一些数据,并在swift 2中打开解析rss中的相关链接, 例如,我想检查此链接是否有效: rtmp://185.23.131.187:1935/live/jomhori1 或者这个: http://185.23.131.25/hls-live/livepkgr/_defint_/liveevent/livestream.m3u8 检查url验证的我的代码: let urlPath: String = "http://185.23.131.25/hls-live/live
rtmp://185.23.131.187:1935/live/jomhori1
或者这个:
http://185.23.131.25/hls-live/livepkgr/_defint_/liveevent/livestream.m3u8
检查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
}
让urlPath:String=”http://185.23.131.25/hls-live/livepkgr/_defint_/liveevent/livestream.m3u8"
让url:NSURL=NSURL(字符串:urlPath)!
let请求:NSURLRequest=NSURLRequest(URL:URL)
let响应:autoreleasingusafemutablepointer=nil
变量有效:Bool!
做{
_=尝试NSURLConnection.sendSynchronousRequest(请求,返回响应:响应)
}抓住{
打印(“404”)
有效=错误
}
我在网上搜索过,但我找到的所有方法对我的问题都没有帮助。我在Objective C中找到了一个解决方案,因此我将代码移植到Swift(尽管您需要对其进行测试):
为了便于使用,我写了下面的代码,它工作得很好
var video_Url = ""
if let url = NSURL(string: Response),
data = NSData(contentsOfURL: url)
{
video_Url = Response
}
else
{
video_Url = ""
}
@sschare给出的答案很好,但不推荐使用NSURLConnection,最好现在就使用NSURLSession 以下是我的URL测试类版本:
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)
}
}
}
您可以通过调用带有尾随闭包的类方法来使用它:
URLTester.verifyURL("http://google.com") { (isOK) in
if isOK {
print("This URL is ok")
} else {
print("This URL is NOT ok")
}
}
Swift 3.0和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)
}
}
}
这比只下载响应标题而不是整个页面要好(同样,它也比异步下载要好)。由@Moritz提供的答案的Obj-C变体: 注意:我更喜欢函数而不是类,但行为是一样的:
+(void)verifyURL:(NSString*)urlPath withCompletion:(void (^_Nonnull)(BOOL isOK))completionBlock
{
NSURL *url = [NSURL URLWithString:urlPath];
if (url) {
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.HTTPMethod = @"HEAD";
//optional: request.timeoutInterval = 3;
NSURLSessionDataTask *dataTask = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
{
BOOL isOK = NO;
if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
int code = (int)((NSHTTPURLResponse*)response).statusCode;
//note: you may want to allow other http codes as well
isOK = !error && (code == 200);
}
completionBlock(isOK);
}];
[dataTask resume];
} else {
completionBlock(NO);
}
}
下面是一个带有时间戳的调用:
NSDate *date1 = [NSDate date];
[AppDelegate verifyURL:@"http://bing.com" withCompletion:^(BOOL isOK) {
NSDate *date2 = [NSDate date];
if (isOK) {
NSLog(@"url is ok");
} else {
NSLog(@"url is currently not ok");
}
NSTimeInterval diff = [date2 timeIntervalSinceDate:date1];
NSLog(@"time to return: %.3f", diff);
}];
当你说valid时,你的意思是URL的格式是否正确,或者你是否得到了预期的响应?@sschare我的意思是,如果URL存在或不存在,我会处理404错误消息谢谢,我很感激。我会检查一下,然后告诉你答案。我会用它来看看它是否有效。
NSDate *date1 = [NSDate date];
[AppDelegate verifyURL:@"http://bing.com" withCompletion:^(BOOL isOK) {
NSDate *date2 = [NSDate date];
if (isOK) {
NSLog(@"url is ok");
} else {
NSLog(@"url is currently not ok");
}
NSTimeInterval diff = [date2 timeIntervalSinceDate:date1];
NSLog(@"time to return: %.3f", diff);
}];