Apple maps not opening on link - ios

i am very new to iOS-Development. We are developing a WebApp with Cordova at the moment and we need a plugin to open the native maps application when we click on a button. Our current implementation looks like this:
#import "Maps.h"
#implementation Maps
-(void)startNavigation:(CDVInvokedUrlCommand *)command{
NSString* street = [command.arguments objectAtIndex:0];
NSString* town = [command.arguments objectAtIndex:1];
street = [street stringByReplacingOccurrencesOfString:#" " withString:#"+"];
town = [town stringByReplacingOccurrencesOfString:#" " withString:#"+"];
NSString* address = [NSString stringWithFormat: #"http://maps.apple.com/?q=%#, %#", street, town];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: address]];
}
#end
But it has no effect, although i can see that the adress-String is correctly. If I pass the town, everything is working fine... Any ideas?
Thanks
Wal
edit:
Thanks I checked the String and it was not a valid URL. No i am escaping the String through
address = [address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
And everything works fine.

the line:
NSString *address = [NSString stringWithFormat: #"http://maps.apple.com/?q=%#, %#", street, town];
would product 'http://maps.apple.com/?q=blah, blah" -- is that a valid URL (the space)?
you should confirm the application can open using:
[[UIApplication sharedApplication] canOpenURL:someURL];

Related

iOS: App Share URL using objective C

I have created the code to share my app on whatsapp. The problem is when the share link is sent from ios to an android phone, the url link and text all are displayed in plain text format in android phone's whatsapp chat box, without highlighted link and url.
Can this be fixed? How?
NSString *urlString = [NSString stringWithFormat:#"%#", APP_SHARE_URL];
NSString *initialText1 = [NSString stringWithFormat:#"Hey I am using App: %#\n%#",urlString, profileModel.name];
NSCharacterSet *set = [NSCharacterSet URLHostAllowedCharacterSet];
NSString *whatsappString = [NSString stringWithFormat:#"%#", [[NSString stringWithFormat:#"%#", initialText1] stringByAddingPercentEncodingWithAllowedCharacters:set]];
NSURL *whatsappURL = [NSURL URLWithString:[NSString stringWithFormat:#"whatsapp://send?text=%#", whatsappString]];
if ([[UIApplication sharedApplication] canOpenURL:whatsappURL]) {
[[UIApplication sharedApplication] openURL:whatsappURL];
}
else {
[self showMessage:#"Unable to open WhatsApp"];
}
I am using this code for sharing on whatsapp and it works absolutely fine. Plain text issue occurs in Android OS 5.0 and earlier.
NSString *textToShare = [self getEncodedString:text];
NSString *urlStr = #"whatsapp://send?text=";
urlStr = [NSString stringWithFormat:#"%#%#",urlStr,textToShare];
NSURL *url = [NSURL URLWithString:urlStr];
[[UIApplication sharedApplication] openURL:url]
- (NSString*)getEncodedString:(NSString*)string {
NSString * encodedString = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(
NULL,
(CFStringRef)string,
NULL,
(CFStringRef)#"!*'\"();:#&=+$,/?%#[]% ",
kCFStringEncodingUTF8 ));
return encodedString;
}

ios, how to create #define for URL that will have dynamic variable in it

I want to create a global variable which is part of a URL, which will be created on run time using a string from the user.
For example:
#define base_URL #"http://api.openweathermap.org/data/2.5/find?q=%#&unit=metrics"
also please suggest how should I use it, for example:
NSString *url = [NSString stringWithFormat:#"%#%#", base_URL, string.text];
You could simply do the following having NSString *input:
NSString *url = [NSString stringWithFormat:base_URL, input];
This will replace %# with the string from input
From my point of view you've got 2 options
Store the variable on a Plist and read it every time you need it
Create a singleton which contains the variable
I'd go with 1, but that's a personal choice
Did you try with this one?
#define BASE_URL(__ARGS__) [NSString stringWithFormat: #"YOUR_URL%#", __ARGS__]
You can use it as:
NSURL *url = [NSURL urlWithString: BASE_URL(string.text)];
You can use the method
- (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target
withString:(NSString *)replacement
Example
#define URL #"This is %# Url"
NSString *newStr = [URL stringByReplacingOccurrencesOfString:#"%#" withString:#"Hello"];

Calling Through App with openUrl

I've been trying to use the openUrl function is iOS to dial phone numbers from my app, but it's not going through, the numbers from the response have white spaces, and I've tried to remove it but when I NSLog it's not removing it
(void)phone:(id)sender{
NSString *phone = [[information valueForKeyPath:#"place_detail"] objectForKey:#"phone"];
[phone stringByReplacingOccurrencesOfString:#" " withString:#""];
NSString *dial = [NSString stringWithFormat:#"tel://%#", phone];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:dial]];
}
Try replacing this:
[phone stringByReplacingOccurrencesOfString:#" " withString:#""];
With this:
phone = [phone stringByReplacingOccurrencesOfString:#" " withString:#""];
Use this method to remove all forms of white space:
NSArray* words = [yourString componentsSeparatedByCharactersInSet :[NSCharacterSet whitespaceCharacterSet]];
NSString* nospacestring = [words componentsJoinedByString:#""];
This is advantageous because it removes not only the space character.
Next just call with the following method:
NSString *value=#"your number";
NSURL *url = [[ NSURL alloc ] initWithString:[NSString stringWithFormat:#"tel://%#",value]];
[[UIApplication sharedApplication] openURL:url];

Creating NSURL with custom query parameter returns nil

I have a UISearchBar from which I'm extracting the text that represents an address and building from it a JSON URL for Google Geocoder.
NSString* address = searchbar.text;
NSString* url = [NSString stringWithFormat:#"http://maps.google.com/maps/api/geocode/json?address=%#&sensor=false", address];
If I copy & paste the url from debug window to the browser it works like a charm but when I try to convert it to NSURL i get nil
NSURL* theUrl = [NSURL URLWithString:url];
Any ideas?
I added the encoding to the address prior to concatenating the url and that fixed the problem so now the code looks like this:
NSString* address = searchbar.text;
NSString *encodedString = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL, CFBridgingRetain(address), NULL, (CFStringRef)#"!*'();:#&=+$,/?%#[]", kCFStringEncodingUTF8));
NSString* url = [NSString stringWithFormat:#"http://maps.google.com/maps/api/geocode/json?address=%#&sensor=false", encodedString];

iOS: phone number dialing not working

I'm building an app that lets you call different service centers from Netherlands. The problem is that some of them have a different commercial format (like 0800-xxxx) and the device can't make the call.
The code looks like this:
if ([[UIApplication sharedApplication] canOpenURL:phoneURL]) {
[[UIApplication sharedApplication] openURL:phoneURL];
}
Do you have any idea how to format the number or to make the phone call, no matter it's format?
EDIT: This is how the phoneNumber is created:
NSString *phoneNumberString = phoneNumber; // dynamically assigned
NSString *phoneURLString = [NSString stringWithFormat:#"telprompt:%#", phoneNumberString];
NSURL *phoneURL = [NSURL URLWithString:phoneURLString];
I used this code and it worked:
NSString *cleanedString = [[phoneNumber 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];
}
NSString *strm = (NSString *) CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)mob, NULL, CFSTR(":/?#[]#!$&’()*+,;="), kCFStringEncodingUTF8);
NSString *strMob = [[NSString alloc] initWithFormat:#"tel://%#",strm];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:strMob]];
try this code . This works perfectly for any kind of format.It will convert into perfect calling URL .

Resources