0

Basically I'm trying to find NSSet objects contained in a url for my webview.

How would I use NSScanner or NSRange to do this?

Below is what I'm working with.

    //External file links
    NSURL* externalURL = [request URL];
    NSString *externalFileExtension = [[request URL] pathExtension];


    //External file extensions
    NSLog(@"fileExtension is: %@", externalFileExtension);
    NSSet *supportedFileExtensions = [NSSet setWithObjects:@"mpeg", @"mpg", @"m1s", @"mpa", @"mp2", @"m2a", @"mp2v", @"mv2", @"m2s", @"avi", @"mov", @"qt", @"asf", @"asx", @"wmv", @"wma", @"wmx", @"rm", @"ra", @"ram", @"rmvb", @"mp4", @"3gp", @"3gpp", @"ogm", @"mkv", @"flv", @"mv4", @"srt", @"swf", @"vob", @"aif", @"iff", @"m3u", @"m4a", @"mid", @"mp3", @"mpa", @"wav", @"aac", @"7z", @"cbr", @"deb", @"gz", @"pkg", @"rar", @"rpm", @"sitx", @"tar.gz", @"zip", @"zipx", @"ipsw", @"bin", @"cue", @"dmg", @"iso", @"mdf", @"toast", @"vcd", @"torrent", @"nes", @"rom", @"doc", @"docs", @"msg", @"odt", @"rtf", @"txt", @"wpd", @"wps", nil];

    if ([supportedFileExtensions containsObject:externalFileExtension]) {
         //my actions
}

Because of the type of links, some sites don't exactly use file extensions, some use the extension type in the link i.e. "zip=blahblah" or "blahblahzipblah'

I need to search the link clicked to find the supportedFileExtensions portion.

Thank you in advanced.

UPDATE: Thanks to rmaddy for getting me in the right direction. For my original question, it did solve it. But I am having issues with the use on some other sites.

I have a webview in which I have a few links to different sites like Media Fire, Copy, Box, etc. Even a direct download link. The media fire link for example starts the download without even going to the site, almost like its just downloading the text. The direct download wont even fire my downloader at all.

Using the accepted answer, what would be the cleanest way to distinguish these?

ChrisOSX
  • 724
  • 2
  • 11
  • 28

2 Answers2

1

The following should be what you want:

//External file links
NSURL* externalURL = [request URL];
NSString *urlString = [externalURL absoluteString];

NSSet *supportedFileExtensions = [NSSet setWithObjects:@"mpeg", @"mpg", @"m1s", @"mpa", @"mp2", @"m2a", @"mp2v", @"mv2", @"m2s", @"avi", @"mov", @"qt", @"asf", @"asx", @"wmv", @"wma", @"wmx", @"rm", @"ra", @"ram", @"rmvb", @"mp4", @"3gp", @"3gpp", @"ogm", @"mkv", @"flv", @"mv4", @"srt", @"swf", @"vob", @"aif", @"iff", @"m3u", @"m4a", @"mid", @"mp3", @"mpa", @"wav", @"aac", @"7z", @"cbr", @"deb", @"gz", @"pkg", @"rar", @"rpm", @"sitx", @"tar.gz", @"zip", @"zipx", @"ipsw", @"bin", @"cue", @"dmg", @"iso", @"mdf", @"toast", @"vcd", @"torrent", @"nes", @"rom", @"doc", @"docs", @"msg", @"odt", @"rtf", @"txt", @"wpd", @"wps", nil];

for (NSString *extension in supportedFileExtensions) {
    if ([urlString rangeOfString:extension].location != NSNotFound) {
        // Found extension somewhere in the URL - process it as needed
        break; // stop looking for more
    }
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • Working for the most part, only a couple issues. I have a few links to different sites like Media Fire, Copy, Box, etc. Even a direct download link. The media fire link for example starts the download without even going to the site, almost like its just downloading the text. The direct download wont even fire my downloader at all. – ChrisOSX Nov 29 '14 at 18:10
  • That doesn't seem to have anything to do with your original question. If this answer solves your original question, you should accept and/or update the answer. If you have a further issue, post a new question with relevant details. – rmaddy Nov 29 '14 at 18:20
  • Instead of updating this completed and answered question, post a new question. – rmaddy Nov 29 '14 at 18:37
  • New question here: https://stackoverflow.com/questions/27205980/using-rangeofstring-for-different-links-urls-etc – ChrisOSX Nov 29 '14 at 19:12
0

Perhaps something like this without using NSScanner or NSRange:

BOOL result = NO;
for (NSString *extension in supportedFileExtensions) {
    if ([externalFileExtension isEqualToString:extension]) {
        result = YES;
        break;
    }
}
kylejs
  • 1,128
  • 11
  • 25
  • But the problem with that is that: NSString *externalFileExtension = [[request URL] pathExtension]; is still searching for an extension, when the "extension" could be in the middle of the link tapped. – ChrisOSX Nov 29 '14 at 15:12
  • The code in this answer is the long way of simply calling `[supportedFileExtensions containsObject:externalFileExtension]` which the OP is already doing. – rmaddy Nov 29 '14 at 15:56
  • Ah. I didn't realise that containsObject would call isEqual on each item. I believed that it just compared pointers – kylejs Dec 01 '14 at 10:11
  • Actually, it compares the values returned by the hash method? I'll be quiet now... – kylejs Dec 02 '14 at 19:31