我正在尝试验证 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];
}