Im trying to create an application which will open apple maps located in iOS device with given source and destination address.
NSString* addr = [NSString stringWithFormat: #"http://maps.apple.com/?daddr=%#&saddr=%#",[_fromTextfield.text stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]],[_toTextfield.text stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]]];
addr=[addr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]];
NSURL* url = [NSURL URLWithString:addr];
if ([[UIApplication sharedApplication]canOpenURL:url])
{
[[UIApplication sharedApplication] openURL:url];
}
But canOpenURL is not working ! It always returns NO.
I have added
"LSApplicationQueriesSchemes
urlscheme
urlscheme2
urlscheme3
urlscheme4
"
in Info.plist file.
Try below coding.it works perfectly.
NSString* addr = [NSString stringWithFormat: #"http://maps.apple.com/?daddr=%#&saddr=%#",#"Lacock" ,#"Avebury"];
NSURL* url = [NSURL URLWithString:addr];
if ([[UIApplication sharedApplication]canOpenURL:url])
{
[[UIApplication sharedApplication] openURL:url];
}
If error is
"This app is not allowed to query for scheme whatsapp"
1) Check Info plist.
add LSApplicationQueriesSchemes Array
add whatsapp String.
If error is invalid url.
2) Check, whether the string you are passing is not having special characters. Specially whitespace.
string = [string stringByReplacingOccurrencesOfString:#" " withString:#"%20"];
then use this string for url.
Both of these should solve. Cheers!
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;
}
}
I need the app recognize if it's a youtube video embed, then in-app webView
Here is the code I'm using now:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
{
NSString *urlString = request.URL.absoluteString;
NSString *youtube;
youtube = #"youtube";
if ([urlString rangeOfString:youtube options: NSCaseInsensitiveSearch].location != NSNotFound){
return YES;
}
[[UIApplication sharedApplication] openURL:request.URL];
return NO;
}
That works in most cases because most youtube links are directly transferred as embedded video already on the webpage. However, I check if i choose a profile page or others from youtube, this will still open in-app, becuz my app doesn't have back button (buttons in html page). So any link that's not to a video will cause can't return.
I tried use #"youtube.com/watch" #"/watch?" #"watch?" as rangeOfString, but only youtube works.
For example:
This a youtube video url: data-url="http://youtube.com/watch?feature=player_detailpage&v=Ke1Y3P9D0Bc" (in-app view good)
dara-url="youtube.com" (fail, still in-app view)
I wonder either i stored string by wrong format, symbols not support in rangeOfString?
Or there can be another way like urlString rangeOfString:youtube && #"watch"
Thank you for this, really appreciate.
NSURL *myURL = [NSURL URLWithString:#"http://www.youtube.com/watch?feature=player_detailpage&v=Ke1Y3P9D0Bc"];
NSString *host = myURL.host;
NSString *path = myURL.path;
NSLog(#"%#", host); // Output: www.youtube.com
NSLog(#"%#", path); // Output: /watch
NSLog(#"%#", myURL.query); // Output: feature=player_detailpage&v=Ke1Y3P9D0Bc
if (NSMaxRange([host rangeOfString:#"youtube.com" options:(NSCaseInsensitiveSearch|NSBackwardsSearch)]) == host.length &&
[path.lowercaseString isEqualToString:#"/watch"]) {
NSLog(#"This is a youtube video.");
}
You can certainly use,
if ([urlString rangeOfString:youtube options: NSCaseInsensitiveSearch].location != NSNotFound && [urlString rangeOfString:#"watch" options: NSCaseInsensitiveSearch].location != NSNotFound)
Whether that will get you what you want, I don't know, but it should work as a valid if statement.