Code Review Stack Exchange is a question and answer site for peer programmer code reviews. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need to intercept a URL and go outside the WebView if the URL contains MAPS or DOCUMENTS constants. I must also go outside if url does not contain the value of DOMAIN and LOGIN_SALES_FORCE.

This is working nice, but seems ugly for me. Any ideas?

if ([request.URL.absoluteString rangeOfString:MAPS      ].location != NSNotFound ||
    [request.URL.absoluteString rangeOfString:DOCUMENTS ].location != NSNotFound)
{
    NSURL *url = [NSURL URLWithString:request.URL.absoluteString];
    return [[UIApplication sharedApplication] openURL:url];
} else if ([request.URL.absoluteString rangeOfString:DOMINIO           ].location == NSNotFound &&
           [request.URL.absoluteString rangeOfString:LOGIN_SALES_FORCE ].location == NSNotFound) {
    NSURL *url = [NSURL URLWithString:request.URL.absoluteString];
    return [[UIApplication sharedApplication] openURL:url];
}
share|improve this question
up vote 3 down vote accepted

Disclaimer: I don't know Objective-C. The proposed changes are quite basic, though, and should give the same result as your original code.

Intermediate variables

To shorten your expressions, you may want to use an intermediate variable:

NSString *absUrl = request.URL.absoluteString;

Then, you can group your conditions using boolean operators.

Boolean logic

if (   [absUrl rangeOfString:MAPS].location != NSNotFound 
    || [absUrl rangeOfString:DOCUMENTS].location != NSNotFound
    || (    [absUrl rangeOfString:DOMINIO].location == NSNotFound
         && [absUrl rangeOfString:LOGIN_SALES_FORCE].location == NSNotFound))
{
    NSURL *url = [NSURL URLWithString:request.URL.absoluteString];
    return [[UIApplication sharedApplication] openURL:url];
}

Remarks

Reading this documentation, I find it strange that you are converting request.URL to url using URLWithString. I may be wrong, but you could use absoluteUrl to achieve the same effect.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.