6

I am trying to check the URL entered by the user, but I am fighting against some errors and warnings.

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

They say:

NSString may not respond to -rangeWithString
Unused variable urlAddress in the condition "if … else" (for both)
urlAddress undeclared : in the URLRequest

Does anyone have any idea what to do?

6 Answers 6

19

NSString responds to rangeOfString:, not rangeWithString:.

The variable urlAddress is declared both in the if statement, and in the else statement. That means it only lives in that scope. Once you leave the if/else statement, the variable is gone.

For a URL it's best if it begins with the scheme (like "http://"), and your code will gladly accept apple.http://.com as being valid.

You can use the hasPrefix: method instead, like this:

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];
Sign up to request clarification or add additional context in comments.

Comments

4
if ( [[url lowercaseString] hasPrefix:@"http://"] )
    return url;
else
    return [NSString stringWithFormat:@"http://%@", url];

Comments

1

I believe you want the rangeOfString method.

Comments

0

To get rid of your urlAddress warnings you should declare NSURL *urlAddress above if..else:

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

Comments

0

Use this

NSString *stringUrl = url.absoluteString;

if ([stringUrl localizedCaseInsensitiveContainsString:@"your string"]) {
    //do something
}

It first converts url to string and then checks whether this url contains your string

Comments

-1

If you have a NSURL convert it to NSString with

   NSString stringURL = url.absoluteString

Then check whether this NSString contains http:// with this code

 [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;
}

1 Comment

Much better to use [stringUrl localizedCaseInsensitiveContainsString:@"gif"]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.