Sunday 15 February 2015

iphone - check if an string matches any value in an array? -


Introduction: I am writing some code that I did not write and there is no knowledge of IOS.

The app contains a webview that handles most apps; Internal links are loaded in the webwave and links to other sites are opened in Safari which are expected. However, embedding a Google map may also be the reason for opening a safari, I found the relevant code of code, I think, and it looks like this:

  // Fixed a slightly above NSString * Const site URL = @ "http://my.example.com" if (! [Request.URL.absoluteString hasPrefix: siteUrl]) {if ([[UIApplication shared application] canOpenURL: request.URL]) { UIApplication shared application] openURL: request.URL]; } no return; }   

Now because 'maps.google.com' does not start with 'my.example.com' which spreads it in Safari - and it makes sense to me though , I tried to modify it:

  Fixed NSString * const googleMaps = @ "http://maps.google.com" if (! ([Request.URL.absoluteString hasPrefix: siteUrl ] | [[Request.URL.absoluteString hasPrefix: googleMaps]) {if ([[UIApplication shared application] canOpenURL: request.URL]) [[UIApplication shared application] openURL: request.URL]; } no return; }   

It also works but I have to add some more URL. Do I really like adding 'or' and string constants? Or is there any way to create an array and "if it matches any of these"? Sorry if this question is simple.

In PHP, I just do something like my status, just like in_array ('string', $ array) , I think I see how so in iOS

An NSSet of a solution object must be maintained, being all the strings that you can make your internal webview Want to handle

  NSSet * internalURLSet = [NSSet set with object: @ "http://my.example.com", @ "http://maps.google.com", Zero];   

If you want to add to the set over time, you can use an incompetent set, but you will know that whenever the URL starts with your app You want to handle internally and this change will not happen. If you're adding the set as an app run, add them to the instability set because they become known.

When you check a URL, use [internalURLSet includes the object: urlToTest] and matches the wires you added to the set.

It is very efficient to check against the set, it is less useful to add objects to the set, but when you check multiple times you will add the URL only once to the set.

(do indexOfObject: is against NSArray disabled ...)

No comments:

Post a Comment