【问题标题】:how to validate youtube URL in objective-C?如何在 Objective-C 中验证 youtube URL?
【发布时间】:2013-05-16 10:27:39
【问题描述】:

我正在尝试验证 youtube 网址 http://www.youtube.com/watch?v=UdQfR4nsXvI 。使用下面的代码

(BOOL) validateUrl: (NSString *) candidate
{

NSString *urlRegEx = @"(http|https)://((\\w)*|([0-9]*)|([-|_])*)+([\\.|/]((\\w)*|([0-9]*)|([-|_])*))+";

NSPredicate *urlTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", urlRegEx];

return [urlTest evaluateWithObject:candidate];

} 

我还想验证任何 youtube URL 格式 ....

编辑代码:


- (IBAction)uplaod_video_Action:(id)sender
{
    NSURL *candidateURL = [NSURL URLWithString:youtube_Url.text];

    // WARNING > "test" is an URL according to RFCs, being just a path
    // so you still should check scheme and all other NSURL attributes you need

    if (candidateURL && candidateURL.scheme && candidateURL.host)
    {
        // candidate is a well-formed url with:
        //  - a scheme (like http://)
        //  - a host (like stackoverflow.com)
        NSLog(@"hallo");
    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Message" message:@"Please enter the Email" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
        [alert show];

    }

【问题讨论】:

标签: iphone ios objective-c


【解决方案1】:

使用regexpal.com 检查您的验证机制

在 Raywenderlich.com 上有一个非常 comprehensive regex tutorial

【讨论】:

    【解决方案2】:

    试试这个

    NSString *urlRegEx = @"(^http:\/\/(?:www\.)?youtube.com\/watch\?(?=[^?]*v=\w+)(?:[^\s?]+)?$);
    

    【讨论】:

      【解决方案3】:

      试试这个

      -(BOOL) validateUrl: (NSString *) candidate
      {
      
      NSString *urlRegEx = @"(?<=v(=|/))([-a-zA-Z0-9_]+)|(?<=youtu.be/)([-a-zA-Z0-9_]+)";
      
      NSPredicate *urlTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", urlRegEx];
      
      return [urlTest evaluateWithObject:candidate];
      
      } 
      

      【讨论】:

      • 当我在 url 中给出“http:www”时,它也在使用它
      • @user2389500 我觉得你很困惑所以请使用正则表达式
      猜你喜欢
      • 2012-01-03
      • 2017-05-09
      • 1970-01-01
      • 2016-10-07
      • 2012-08-26
      • 2015-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多