Youtube Video ID From URL - Objective C

后端 未结 16 905
失恋的感觉
失恋的感觉 2020-12-22 23:23

I basically have a Youtube url as an NSString, but I need to extract the video id that is displayed in the url. I found many tutorials on how to do this in php or and other

16条回答
  •  粉色の甜心
    2020-12-22 23:37

    I used the highest voted answer to write a better, more restrictive regex.

    NSString *regex = @"(?:youtube.com.+v[=/]|youtu.be/)([-a-zA-Z0-9_]+)";
    

    and then you can get the ID

    NSTextCheckingResult *match = [regex firstMatchInString:url
                                                    options:0
                                                      range:NSMakeRange(0, [url length])];
    NSRange videoIDRange = [match rangeAtIndex:1];
    NSString *youTubeID = [url substringWithRange:videoIDRange];
    

提交回复
热议问题