2
\$\begingroup\$

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];
}
\$\endgroup\$
0
3
\$\begingroup\$

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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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