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];
}
Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked May 27, 2015 at 14:24
\$\endgroup\$
0

1 Answer 1

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.

answered May 27, 2015 at 14:46
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.