why use openURL open the google map can't find the address - ios

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.

Related

Open apple maps on ios 8 and above not working

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!

dialing iPhone on iOS9

There are many answers for this question, but on iOS 9 there are some problems. When I use this code:
NSString *phoneNumber = [#"telprompt://" stringByAppendingString:self.lblPhone.text];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];
I got this error:
LaunchServices: ERROR: There is no registered handler for URL scheme (null)
Then I change the Info.plist with this:
<key>LSApplicationQueriesSchemes</key>
<array>
<string>telprompt</string>
<string>tel</string>
</array>
But nothing changed!
So I tried second code and this works:
NSString *numberString = #"004986632461";
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:#"telprompt://%#",numberString]]];
But this one not:
NSString *numberString = [NSString stringWithFormat:#"%f", [[_lblPhone text] floatValue]];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:#"telprompt://%#",numberString]]];
It's dialing, but the number is wrong. Any idea?
Your second bit of code doesn't work because you are attempting to format the text as a floating point value.
This line:
NSString *numberString = [NSString stringWithFormat:#"%f", [[_lblPhone text] floatValue]];
Will result in a value of 4986632704.000000 for the string #"004986632461".
Skip the float part. Just do:
NSString *numberString = _lblPhone.text;
Don't ever try to treat a phone number as a numeric value. It's text and should only be treated (and stored) as text.
Try:
NSString *phoneNumber = [#"tel://" stringByAppendingString:self.lblPhone.text];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];
Previously telprompt was used by most developers since it will return to app after the call while tel will return to home screen. But from iOS 8 onwards, tel will return to the app after call. So you can use that.
I solved this problem myself. It's very easy. I must clean invisible spaces!
NSString *numberString = [_lblPhone.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

Make a phone call from UIWebView without prompt

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

Making a phone call in an iOS application

I have some code which attempts to make a call within an application, but it doesn't seem to be working:
UIApplication *myApp = [UIApplication sharedApplication];
NSString *theCall = [NSString stringWithFormat:#"tel://%#",phone];
NSLog(#"making call with %#",theCall);
[myApp openURL:[NSURL URLWithString:theCall]];
Sometimes, the variable phone is something such as #"(102) 222-2222". How can I make a call with a phone number like this? Do I need to manually extract the numbers out of it and get rid of all the extra punctuation?
Yup. You need to take those out yourself. Or you can use the snippet below...
NSString *cleanedString = [[phoneNumber componentsSeparatedByCharactersInSet:[[NSCharacterSet characterSetWithCharactersInString:#"0123456789-+()"] invertedSet]] componentsJoinedByString:#""];
NSURL *telURL = [NSURL URLWithString:[NSString stringWithFormat:#"tel:%#", cleanedString]];
Note: you may be tempted to use -stringByTrimmingCharactersInSet:, but that one only removes characters at the start and the end of the string, not if they appear in the middle.
To go back to original app you can use telprompt:// instead of tel:// - The tell prompt will prompt the user first, but when the call is finished it will go back to your app:
NSString *phoneNumber = [#"telprompt://" stringByAppendingString:mymobileNO.titleLabel.text];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];
just an update on above answer.
Here's a simple method that can be used to make a call and return to the app after the call is finished.
Add the following to your .m file
- (void) dialNumber:(NSString*) number{
number = [#"telprompt://" stringByAppendingString:number];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:number]];
}
Then add the following code wherever you want to make the call from:
[self dialNumber:#"5031234567"];

How can I make phone call in iOS?

How can I make a phone call in Objective-C?
You can initiate a call
https://developer.apple.com/library/archive/featuredarticles/iPhoneURLScheme_Reference/PhoneLinks/PhoneLinks.html
So this would probably work:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"tel:12125551212"] options:#{} completionHandler:nil];
This is clipped from a project I did to do just that:
NSString *phoneStr = [NSString stringWithFormat:#"tel:%#",phone_number];
NSURL *phoneURL = [NSURL URLWithString:phoneStr];
[[UIApplication sharedApplication] openURL:phoneURL];
It may also be helpful to know how to prompt the user to call a number:
NSURL *phoneNumber = [NSURL URLWithString:#"telprompt://13232222222"];
[[UIApplication sharedApplication] openURL:phoneNumber];
telprompt gives the user a choice to place the call or cancel making the call before the phone dials. The two forward slashes after the colon are optional.
well if you are talking about using objective-c to make a phone call on the iphone, then you can do something like this:
NSURL *phoneNumber = [[NSURL alloc] initWithString: #"tel:867-5309"];
[[UIApplication sharedApplication] openURL: phoneNumber];
If you are talking about doing this on a mac ,well, then like others have mentioned that is specific based on number of things like, if you are using voip, a modem, connecting through something like an Asterisks box, etc..
openURL is deprecated.
Now use this:
UIApplication *application = [UIApplication sharedApplication];
[application openURL:[NSURL URLWithString: #"tel:12125551212"] options:#{} completionHandler:nil];
REMOVE EMPTY SPACES IN PHONE NUMBER
NSString *phoneNumberString = #"123 456";
phoneNumberString = [phoneNumberString stringByReplacingOccurrencesOfString:#" " withString:#""];
phoneNumberString = [NSString stringWithFormat#"tel:%#", phoneNumberString];
NSURL *phoneNumberURL = [NSURL URLWithString:phoneNumberString]];
[[UIApplication sharedApplication] openURL:phoneNumberURL];
NSString *phoneNumber = #"Phone number here";
UIWebView *webView = [[UIWebView alloc] init];
NSURL *url = [NSURL URLWithString:numberString];
NSURLRequest *requestURL = [NSURLRequest requestWithURL:url];
webView.dataDetectorTypes = UIDataDetectorTypeNone;
[webView loadRequest:requestURL];
This will either be very platform-specific, or you'll have to use a wrapper library to account for the differences among platforms, so you better state what platform this is intended for. In general, there are various telephony APIs available on most platforms.
On Windows systems there's for example the "TAPI", also things may somewhat differ if you are targeting a digital telephone system such as ISDN, because there are other APIs available.

Resources