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!
Related
I am receiving a url from a server request, I made a button , when pressed, safari must open and go to the link,
My code below:
- (IBAction)openFeedbackWebViewPresser:(id)sender {
NSString *feedbackUrl = self.getConfig.feedbackURL;
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: feedbackUrl]];
}
If I print the feedback url , its as below:
https://xxx.xxxxx.com/CRM/feedback#/1715171559ae979371687#/10306
I tried to use another way:
NSURL *url = [NSURL URLWithString:feedbackUrl];
[[UIApplication sharedApplication] openURL: url];
the url is returning nil, knowing that the feedbackUrl contains a url.
Any idea whats wrong?
Thanks
Might be your string url is containing any spaces or any other special characters that must be encoded. like below
NSString *feedbackUrl = [self.getConfig.feedbackURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: feedbackUrl]];
hope it helps.!!!
This might be happening because of the special characters that must be encoded. The following encoding works for me-
NSString *feedbackUrl = [self.getConfig.feedbackURL stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: feedbackUrl]];
This is the code to open the link in browser, Hope this help
- (IBAction)emailButton:(id)sender {
[[UIApplication sharedApplication]
openURL:[NSURL URLWithString:self.emailLabel.text]];
}
I'm running iOS 9b5.
In my app, if a device can make a phone call, I want to color the text blue so it looks tappable. If not, I leave it black.
In order to determine the device capabilities, I use:
[[UIApplcation sharedApplication] canOpenURL:#"telprompt://5555555555"]
As we all know, iOS 9 requires we whitelist any URL schemes we'll be using in our app as a privacy measure.
I have this in my Info.plist:
<key>LSApplicationQueriesSchemes</key>
<array>
<string>telprompt</string>
</array>
No matter what I do, I still get canOpenURL: failed for URL: "telprompt://" - error: "(null)". I've tried tel:// and sms:// and I can't seem to avoid that syslog warning.
Does anybody know of a way to detect whether or not a device can make a phone call wtihout triggering these warnings?
What I discovered so far is, that if the console logs -canOpenURL: failed for URL: "xxx://" - error: "(null)", it actually works. As soon as there is any other error than null, it may not work. If the error is "This app is not allowed to query for scheme xxx", then you have to add this scheme to your app's .plist:
<key>LSApplicationQueriesSchemes</key>
<array>
<string>xxx</string>
</array>
Strange behavior that the console output looks like an error although there is none, indeed.
I think you might need to try this on an actual device, or just try it again. I just got this working on my iPhone 5, it looks like you don't even need to add it to the LSApplicationQueriesSchemes. If the app is built with Xcode 7 Beta 6 and you use canOpenURL or openURL like below it seems to work just fine on device.
[[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:#"tel:555-555-5555"]]
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"tel:555-555-5555"]]
On the iOS sim I still get the error:
LaunchServices: ERROR: There is no registered handler for URL scheme tel
-canOpenURL: failed for URL: "tel:555-555-5555" - error: "This app is not allowed to query for scheme tel"
I got the same error in IOS9 devices. So I have used below code snippet to avoid this error.
NSString *cleanedString = [[[PHONE NUMBER] componentsSeparatedByCharactersInSet:[[NSCharacterSet characterSetWithCharactersInString:#"0123456789-+()"] invertedSet]] componentsJoinedByString:#""];
NSString *escapedPhoneNumber = [cleanedString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *phoneURLString = [NSString stringWithFormat:#"telprompt:%#", escapedPhoneNumber];
NSURL *phoneURL = [NSURL URLWithString:phoneURLString];
if ([[UIApplication sharedApplication] canOpenURL:phoneURL]) {
[[UIApplication sharedApplication] openURL:phoneURL];
}
As iOS9 deprecates stringByAddingPercentEscapesUsingEncoding, the following can be used to clean the telprompt: URL.
NSString *cleanedString = [[[PHONE NUMBER] componentsSeparatedByCharactersInSet:[[NSCharacterSet characterSetWithCharactersInString:#"0123456789-+()"] invertedSet]] componentsJoinedByString:#""];
//NSString *escapedPhoneNumber = [cleanedString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *escapedPhoneNumber = [cleanedString stringByAddingPercentEncodingWithAllowedCharacters: [NSCharacterSet URLQueryAllowedCharacterSet]];
NSString *phoneURLString = [NSString stringWithFormat:#"telprompt:%#", escapedPhoneNumber];
NSURL *phoneURL = [NSURL URLWithString:phoneURLString];
if ([[UIApplication sharedApplication] canOpenURL:phoneURL]) {
[[UIApplication sharedApplication] openURL:phoneURL];
}
In iOS9 I'm using this code and it works:
NSString *assistanceNumber = [[NSUserDefaults standardUserDefaults] objectForKey:#"AssistanceCallMISDN"];
assistanceNumber= [[assistanceNumber componentsSeparatedByCharactersInSet:[[NSCharacterSet characterSetWithCharactersInString:#"0123456789-+()"] invertedSet]] componentsJoinedByString:#""];
assistanceNumber = [assistanceNumber stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *phoneUrl = [NSURL URLWithString:[#"telprompt://" stringByAppendingString:assistanceNumber]];
NSURL *phoneFallbackUrl = [NSURL URLWithString:[#"tel://" stringByAppendingString:assistanceNumber]];
if ([UIApplication.sharedApplication canOpenURL:phoneUrl]) {
[UIApplication.sharedApplication openURL:phoneUrl];
} else if ([UIApplication.sharedApplication canOpenURL:phoneFallbackUrl]) {
[UIApplication.sharedApplication openURL:phoneFallbackUrl];
} else
{
[[[UIAlertView alloc] initWithTitle:#"" message:[NSString stringWithFormat:#"No se ha podido realizar la llamada a través de la aplicación. Puede llamar usted al %#", assistanceNumber] delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil] show];
[_viewEmergency setHidden:YES];
}
My Info.plist
<key>LSApplicationQueriesSchemes</key>
<array>
<string>telprompt</string>
<string>tel</string>
</array>
Try running this on a real device instead of simulator. No need to add LSApplicationQueriesSchemes for the tel scheme.
try this one:
NSString *phone_number = [[yourPhoneNumber componentsSeparatedByCharactersInSet:[[NSCharacterSet characterSetWithCharactersInString:#"0123456789-+()"] invertedSet]] componentsJoinedByString:#""];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:#"telprompt://%#", phone_number]]];
SCENARIO
I have an app that is a UIWebView, I make some url overriding for requirements.
PROBLEM
To make a call opening url with tel: works weird in iOS7 and iOS8, it makes the phone call direct in the background, but it also ask for the confirmation, so user experience is horrible:
[[UIApplication sharedApplication] openURL:request.URL];
SOLUTION
To solve this issue, I used telprompt. It works nice in all iOS versions:
NSURL *url = [NSURL URLWithString:#"telprompt://637****"];
return [[UIApplication sharedApplication] openURL:url];
But shows this confirmation dialog:
QUESTION
Now, I have a new requirement, to make the phone call without confirmation or prompt. So... There is some way to make a phone call in iOS omitting the confirmation prompt?
I want something like
NSURL *url = [NSURL URLWithString:#"telnoprompt://637******"];
return [[UIApplication sharedApplication] openURL:url];
NSMutableCharacterSet *characterSet =[NSMutableCharacterSet characterSetWithCharactersInString:#" "];
NSArray *arrayOfComponents = [phone_number componentsSeparatedByCharactersInSet:characterSet];
phone_number = [arrayOfComponents componentsJoinedByString:#""];
NSString *phoneURLString = [NSString stringWithFormat:#"tel:%#", phone_number];
NSString *escapedUrlString = [phoneURLString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *phoneURL = [NSURL URLWithString:escapedUrlString];
I would like to share one Url link and some text message into WhatsApp from my application. How can i share content?
I got this code for only text
NSString * msg = #"Trueman India Magazine";
NSString * urlWhats = [NSString stringWithFormat:#"whatsapp://send?text=%#",msg];
NSURL * whatsappURL = [NSURL URLWithString:[urlWhats stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
if ([[UIApplication sharedApplication] canOpenURL: whatsappURL])
{
[[UIApplication sharedApplication] openURL: whatsappURL];
}
but how i share my url link in WhatsApp?
I had a problem with this whatsapp api with url strings, especially when they contained a query string with several fields, e.g. http://example.com/foo?bar=foo&foo=bar.
When opening the app I found the message text would be empty.
The solution was to properly percent escape the string using the CFString functions.
See the apple documentation here:
https://developer.apple.com/library/mac/documentation/CoreFoundation/Reference/CFURLRef/index.html#//apple_ref/c/func/CFURLCreateStringByAddingPercentEscapes
But for anyone else with this issue here is my solution in full:
CFStringRef originalURLString = (__bridge CFStringRef)[NSString stringWithFormat:#"%#", #"http://example.com/foo?bar=foo&foo=bar"];
CFStringRef preprocessedURLString = CFURLCreateStringByReplacingPercentEscapesUsingEncoding(kCFAllocatorDefault, originalURLString, CFSTR(""), kCFStringEncodingUTF8);
NSString *urlString = (__bridge NSString*)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, preprocessedURLString, NULL, CFSTR("!*'();:#&=+$,/?%#[]"), kCFStringEncodingUTF8);
NSString *whatsAppURLString = [NSString stringWithFormat:#"whatsapp://send?text=%#", urlString];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:whatsAppURLString]];
Note the use of the characters to be escaped in the CFURLCreateStringByAddingPercentEscapes function.
Include the plain link inside the text, e.g.:
NSString * msg = #"Trueman India Magazine http://www.truemanindiamagazine.com";
The link will be generated/tappable after sending it to someone
We can achieve this by using simple jquery. here is the article link http://www.stepblogging.com/how-to-share-web-article-on-whatsapp-using-jquery/
and you can check demo on your smart phone Demo Link
I create a UITextView and set text=#"中国,浙江省杭州市滨江区",
set dataDetectorTypes=UIDataDetectorTypeAddress,
then,long pressed, choose open map, it can found the address in GoogleMap.
But, the same address, i used openUrl can't find the address.
NSString *urlText = [NSString stringWithFormat:#"http://maps.google.com/maps?q=%#", [address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlText]];
anybody who can tell me why? or iOS not use this url(http://maps.google.com/maps?q=%#)
Would you try with a different enconding? eg., NSUnicodeStringEncoding
NSString *urlText = [NSString stringWithFormat:#"http://maps.google.com/maps?q=%#", [address stringByAddingPercentEscapesUsingEncoding:NSUnicodeStringEncoding]];
And what is the result of
[address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
?
Google Maps http:// calls don't use % seperators but rather +'s.
NSString *fixedAddress = [fullAddress stringByReplacingOccurencesOfString:#" " withString:#"+"];
NSString *googleCall = #"http://maps.google.com/maps?q=";
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[googleCall stringByAppendingString:fixedAddress]]];
I, myself encountered this issue and fixed it with the preceeding code last night.