iOS: phone number dialing not working - ios

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 .

Related

iOS Objective C Check if Object Exists at Index

So I have some code to grab an item from an index:
NSString *fullUrl = [NSString stringWithFormat:#"%#",[webView.URL absoluteURL]];
NSArray *listItems = [fullUrl componentsSeparatedByString:#"?url="];
NSString *urlString = [listItems objectAtIndex:1];
if (urlString != (id)[NSNull null]) {
NSURL *url = [NSURL URLWithString:urlString];
[[UIApplication sharedApplication] openURL:url];
} else {
//fallback just in case
[self loadBaseURL];
}
It ended up erroring on this line:
NSString *urlString = [listItems objectAtIndex:1];
Which I thought I was checking for in the following line for null. It seems like I need some check beforehand to ensure that the objectAtIndex:1 exists before grabbing it. What's the best format to write this in?
It is an error to say
[listItems objectAtIndex:1]
if there is no such index. So your first step should be to examine listItems.count. If your proposed index is greater than or equal to that number, don’t use it.

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

How do I add a number to an NSURL? Too many arguments error

I've got a small problem that seems a little bit odd to me. I often used NSString or NSLog while adding NSNumbers into several places:
NSNumber *categoryId = [[NSNumber alloc]initWithInt:0];
NSURL *url = [NSURL URLWithString:#"http://shop.rs/api/json.php?action=getCategoryByCategory&category=%i",[categoryId integerValue]];
Now xcode tells me that I'm too many arguments. What am I doing wrong? Setting up an NSNumber into NSStrings or NSLogs works as I did it above.
Best Regards
What is wrong is on
NSURL *url = [NSURL URLWithString:#"http://shop.rs/api/json.php?action=getCategoryByCategory&category=%i",[categoryId integerValue]];
you are calling URLWithString: and then pass in a string that is not being formatted correctly. If you want to do it all on one line then you need to be using stringWithFormat: like
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"http://shop.rs/api/json.php?action=getCategoryByCategory&category=%i",[categoryId integerValue]]];
Because it is adding a parameter you can't just create a string like you normally would with #"some text" you need to format it using the stringWithFormat: which will return an NSString * with the text held within #"" and the paramters you pass in. So [NSString stringWithFormat:#"My String will come with %#", #"Apples"]; this would provide an NSString with "My String will come with Apples". For more information check out the Apple Documentation for NSString and stringWithFormat:
Try this :
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"http://shop.rs/api/json.phpaction=getCategoryByCategory&category=%i", [categoryId integerValue]]];
Initially code was wrong because of : "categoryId integerValue]" (I forgot a '[').
You can use NSString to form your NSURL. You can then pass it to your URLWithString like below:
NSNumber *categoryId = [NSNumber numberWithInteger:0];
NSString *urlString = [NSString stringWithFormat:#"http://shop.rs/api/json.php?action=getCategoryByCategory&category=%i",[categoryId integerValue]];
NSURL *url = [NSURL URLWithString:urlString];

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

Apple maps not opening on link

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

Resources