感谢rmaddy通过回答THIS问题让我朝着正确的方向前进,这引出了我使用这个被接受的答案的其他一些问题。对于我在该主题上的原始问题,它确实解决了我想要做的事情。但现在我在其他一些网站上遇到了问题。
我有一个webview,其中我有一些指向不同网站的链接,如Media Fire,Copy,Box等。甚至是直接下载链接。例如,媒体消防链接开始下载甚至没有访问该网站,几乎就像它只是下载文本。直接下载甚至不会解雇我的下载器。
使用接受的答案,区分这些的最简洁方法是什么?
以下是适用于大多数网站的代码。
- (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
}
}
}
示例链接: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
答案 0 :(得分:0)
使用正则表达式搜索如下匹配项:
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]);
然后,您需要实现检查所有HTTP响应的custom HTTP URL protoco l。 copy.com链接发送Content-Disposition:attachment;对于它的文件和shouldStartLoadWithRequest在执行请求之前不会有任何关于它的文件。
创建协议后,请使用此方法为所有网络通信注册:
[MyURLProtocol registerClass:[MyURLProtocol class]];
请参阅此tutorial以创建自定义协议