2011-06-30 27 views
5

Estoy tratando de verificar la URL ingresada por el usuario, pero estoy luchando contra algunos errores y advertencias.Comprobar la cadena que contiene la URL para "http: //"

-(BOOL) textFieldShouldReturn:(UITextField *)textField { 
    //check "http://" 
    NSString *check = textField.text; 
    NSString *searchString = @"http://"; 
    NSRange resultRange = [check rangeWithString:searchString]; 
    BOOL result = resultRange.location != NSNotFound; 
    if (result) { 
     NSURL *urlAddress = [NSURL URLWithString: textField.text]; 
    } else { 
     NSString *good = [NSString stringWithFormat:@"http://%@", [textField text]]; 
     NSURL *urlAddress = [NSURL URLWithString: good]; 
    } 
    // open url 
    NSURLRequest *requestObject = [NSURLRequest requestWithURL:urlAddress]; 
} 

Ellos dicen:

NSString puede no responder a -rangeWithString
sin usar variables urlAddress en la condición "if ... else" (para ambos)
urlAddress no declarados: en el URLRequest

¿Alguien tiene alguna idea de qué hacer?

Respuesta

19

NSString responde a rangeOfString:, no rangeWithString:.

La variable urlAddress se declara tanto en la instrucción if como en la declaración else. Eso significa que solo vive en ese ámbito. Una vez que abandona la instrucción if/else, la variable desaparece.

Para una URL, es mejor si comienza con el esquema (como "http: //"), y su código aceptará Apple.http: //.com como válido.

Usted puede utilizar el método hasPrefix: lugar, como este:

BOOL result = [[check lowercaseString] hasPrefix:@"http://"]; 
NSURL *urlAddress = nil; 

if (result) { 
    urlAddress = [NSURL URLWithString: textField.text]; 
} 
else { 
    NSString *good = [NSString stringWithFormat:@"http://%@", [textField text]]; 
    urlAddress = [NSURL URLWithString: good]; 
} 

NSURLRequest *requestObject = [NSURLRequest requestWithURL:urlAddress]; 
0

para deshacerse de sus urlAddress advertencias que debe declarar NSURL *urlAddress por encima de if..else:

NSURL *urlAddress = nil; 
if (result) { 
    urlAddress = [NSURL URLWithString: textField.text];  
} else { 
    NSString *good = [NSString stringWithFormat:@"http://%@", [textField text]]; 
    urlAddress = [NSURL URLWithString: good];  
} 
4
if ([[url lowercaseString] hasPrefix:@"http://"]) 
    return url; 
else 
    return [NSString stringWithFormat:@"http://%@", url]; 
0

Si usted tiene un NSURL convertirlo en NSString con

NSString stringURL = url.absoluteString 

A continuación, compruebe si este NSString contiene http: // con este código

[stringURL containsString: @"http://"] 


    - (BOOL)containsString:(NSString *)string { 
    return [self containsString:string caseSensitive:NO]; 
} 

- (BOOL)containsString:(NSString*)string caseSensitive:(BOOL)caseSensitive { 
    BOOL contains = NO; 
    if (![NSString isNilOrEmpty:self] && ![NSString isNilOrEmpty:string]) { 
     NSRange range; 
     if (!caseSensitive) { 
      range = [self rangeOfString:string options:NSCaseInsensitiveSearch]; 
     } else { 
      range = [self rangeOfString:string]; 
     } 
     contains = (range.location != NSNotFound); 
    } 

    return contains; 
} 
Cuestiones relacionadas