Use NSScanner or NSRange for url to find NSSet objects

155 views Asked by At

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?

2

There are 2 answers

4
rmaddy On BEST ANSWER

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
    }
}
4
kylejs On

Perhaps something like this without using NSScanner or NSRange:

BOOL result = NO;
for (NSString *extension in supportedFileExtensions) {
    if ([externalFileExtension isEqualToString:extension]) {
        result = YES;
        break;
    }
}