iPhone SDK: Check validity of URLs with NSURL not working?

前端 未结 5 2169
心在旅途
心在旅途 2020-12-12 03:52

I\'m trying to check if a given URL is valid and i\'m doing it like this:

- (BOOL)urlIsValid:(NSString *)address {
    NSURL *testURL = [NSURL URLWithString:         


        
5条回答
  •  无人及你
    2020-12-12 04:16

    check this method works fine for me

    - (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];
    

    }

    if (![self validateUrl:strRSSurl]) {
            UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Invalid url" message:[NSString stringWithFormat:@"\"%@\"",strRSSurl] delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
            [alert show ];
            [alert setTag:7];
        }
        else
        {
          valid url
        }
    

提交回复
热议问题