Use NSScanner or NSRange for url to find NSSet objects - ios

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?

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
}
}

Perhaps something like this without using NSScanner or NSRange:
BOOL result = NO;
for (NSString *extension in supportedFileExtensions) {
if ([externalFileExtension isEqualToString:extension]) {
result = YES;
break;
}
}

Related

How do I restrict my application to show up in the "Open in..." menu on iOS for a specific document type

I need to restrict my application to show up for ppt file types.How to do this?
You can use this. This will get the 'kind' of file.
- (NSString *)humanReadableFileType:(NSString *)path
{
NSString *kind = nil;
NSURL *url = [NSURL fileURLWithPath:[path stringByExpandingTildeInPath]];
LSCopyKindStringForURL((CFURLRef)url, (CFStringRef *)&kind);
return kind ? [kind autorelease] : #"";
}
After that check it like that
if([kind isEqualToString:#"ppt"])
{
}
Source: http://importantshock.wordpress.com/2007/01/07/cocoa-snippet-finding-a-files-kind/#post-19

safari link is not open in If www is missing iOS

I Referred a many links related to this issue,But I cannot able to find the answer.I am getting a dynamic url in my app...If the url contains http://www then it opens the link,If www is not present then this error occurs.Any Help on this.
I am using this code,
NSString *selectedurl=[self.SelectedItem objectForKey:#"url"];
selectedurl=[selectedurl stringByReplacingOccurrencesOfString:#" " withString:#"%20"];
NSURL *url=[NSURL URLWithString:selectedurl];
if (url.scheme.length == 0)
{
selectedurl = [#"http://" stringByAppendingString:selectedurl];
url = [[NSURL alloc] initWithString:selectedurl];
}
if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:selectedurl]])
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:selectedurl]];
}
only the URL will start in www your page does not open, you need to append thehttp:` in front of the string, so try this
NSString *sentence = #"www.google.com";
NSString *searchWord = #"http";
if ([sentence rangeOfString: searchWord].location != NSNotFound) {
NSLog(#"Yes , the search word is available");
}else
{
// add the http in front of www using stringWithFormat
}
or the alternate way
NSString *string = #"www.google.com";
if ([string containsString:#"http"]) {
NSLog(#"string contains http..!");
} else {
NSLog(#"string does not contain http...!");
}
choice-2
I think what you are searching for is Universal Links. Check this documentation for it here. It's pretty simple and straight forward. And here is a step by step explanation of how to support them. And afterwards, you can validate your own custom universal links here or hereenter link description here

Using rangeOfString for different links, URL's etc

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

UICollectionView: get profile pictures from Instagram instead of recent media pictures to display

I am a newbie iOS learner. Couldn't find answer to following question after searching for a while. Hence, here it is.
Building on a first app that displays recent pictures from a user's instagram feed, I am trying to display pictures from the follows of that users instead.
To call recent pictures from the Instagram feed, which worked well, I had created the following method "imageForPhoto"
+ (void)imageForPhoto:(NSDictionary *)photo size:(NSString *)size completion:(void(^)(UIImage *image))completion {
if (photo == nil || size == nil || completion == nil) {
return;
}
NSString *key = [[NSString alloc] initWithFormat:#"%#-%#", photo[#"id"], size];
NSURL *url = [[NSURL alloc] initWithString:photo[#"images"][size][#"url"]];
[self downloadURL:url key:key completion:completion];
}
Therefore, I first modified my code to get the data related to the "follows" in my PhotosViewController instead of recent media pictures ( v1/users/3/follows ):
NSURLSession *session = [NSURLSession sharedSession];
NSString *urlString = [[NSString alloc] initWithFormat:#"https://api.instagram.com/v1/users/3/follows?count=99&access_token=%#", self.accessToken];
Then, I created a new method that I called friendAvatarForPhoto to get the follows profile pictures from a photo NSDictionary that is passed in as the only method parameter. I placed this method in my PhotoController class:
+ (void)friendAvatarForPhoto:(NSDictionary *)photo completion:(void(^)(UIImage *image))completion {
if (photo == nil || completion == nil) {
return;
}
NSString *key = [[NSString alloc] initWithFormat:#"avatar-%#", photo[#"user"][#"id"]];
NSURL *url = [[NSURL alloc] initWithString:photo[#"profile_picture"]];
[self downloadURL:url key:key completion:completion];
}
It seems to work. I have manually checked that the pictures that are rendered on my UICollectionView are actually coming from the "profile_picture" key of the responseDictionary I get back from Instagram.
Here is the structure of this dictionary: https://www.dropbox.com/s/ldjqupadqg0j3nq/friends_response_dictionary.png
Specifically, as you can see, I modified both the key and the url:
NSString *key = [[NSString alloc] initWithFormat:#"avatar-%#", photo[#"user"][#"id"]];
NSURL *url = [[NSURL alloc] initWithString:photo[#"profile_picture"]];
but I am not really understanding what I need to initial the *key string with... I kept "avatar" for example but why should I? I am pretty sure it's wrong although the result seems to be fine from what is returned on my UICollectionView.
What should I initiate this string with instead?
What is the function of this key in the overall NSURLSession process?
I'd love to better understand the overall process and better connect the dots with Instagram's API so I can query the right key and be sure I am getting what I am looking for. In a reliable way, not an intuitive one as I have just done app-arently.
Any help from experienced developers in the community would be welcome, I've just started to explore iOS dev a few weeks ago based on rusty C skills from a long time back.
Thank you!
:) Arsene

Open in iBook a PDF with spaces in the name

I've implemented the already nth-times discussed open in iBook feature in my PDF viewer. And it works great when the PDF file does not contain spaces (example1.pdf, example2.pdf). When the PDF has some space (example 1.pdf) in the name clicking the open in iBook button does nothing.
NSString *fileURL = [(Documents *)(self.detailItem) url];
NSArray *subStrings = [fileURL componentsSeparatedByString:#"/"];
NSString *filePath = [[self documentsDirectory] stringByAppendingPathComponent:[subStrings lastObject]];
docIntController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:filePath]];
docIntController.delegate = self;
docIntController.UTI = #"com.adobe.pdf";
[docIntController presentOptionsMenuFromBarButtonItem:sender animated:YES];
Any suggestion is welcomed. Thanks :)
Since the filePath string is intended to be a URL, you likely need to run it through NSString -stringByAddingPercentEscapesUsingEncoding: before invoking fileURLWithPath:
Try enclosing the full path that you send to iBooks in quotes.
For example, use "example 1.pdf" instead of example1.pdf.

Resources