Using rangeOfString for different links, URL's etc

95 views Asked by At

Thanks to rmaddy for getting me in the right direction by answering THIS question, this leads me into some other issues using that acepted answer. For my original question on that thread, it did solve what I was trying to do. But now 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?

Here is the code that works for most sites.

- (BOOL)webView:(UIWebView *)awebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    if (navigationType == UIWebViewNavigationTypeLinkClicked){
    //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
        }
    }
}

Example Links: https://www.dropbox.com/s/57jcgnbnfhcpw9y/Test.zip?dl=0 http://www.mediafire.com/download/wt77jvm3szwjehm/Test.zip https://copy.com/QFvw3fw4FF2k4foX https://app.box.com/s/fixnvrym13eylcr73njv

Direct download link: http://download.thinkbroadband.com/5MB.zip

1

There are 1 answers

0
Shams Ahmed On

Use regular expressions to search for a match like this:

    NSString *urlString = @"http://www.youtube.co.uk/someVideo.mp2";

    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];


    // Expression to match any your mime types
    NSString *pattern = [NSString stringWithFormat:@"(\\W|^)(%@)(\\W|$)", [supportedFileExtensions.allObjects componentsJoinedByString:@"|"]];

    NSRegularExpression *regx = [NSRegularExpression regularExpressionWithPattern:pattern
                                                                          options:NSRegularExpressionCaseInsensitive
                                                                            error:nil];

    // returns first match in the string
    NSTextCheckingResult *match = [regx firstMatchInString:urlString options:NSMatchingReportProgress range:NSMakeRange(0, urlString.length)];
    NSLog(@"matched type: %@", [urlString substringWithRange:match.range]); 

You then need to implement a custom HTTP URL protocol that inspects all HTTP responses. copy.com link sends a Content-Disposition: attachment; for it files and shouldStartLoadWithRequest won't no about it until it execute the request.

once you have created your protocol use this method to register it for all your web communications:

[MyURLProtocol registerClass:[MyURLProtocol class]];

see this tutorial for creating custom protocol