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];
Related
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;
}
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContactProperty:(CNContactProperty *)contactProperty;
{
[self dismissViewControllerAnimated:YES completion: nil];
CNLabeledValue *phoneNumberValue = contactProperty.value;
NSString *contactString = [phoneNumberValue valueForKey:#"_stringValue"];
contactString = [contactString stringByReplacingOccurrencesOfString:#"-" withString:#""];
contactString = [contactString stringByReplacingOccurrencesOfString:#" " withString:#""]; // This line of code is not working properly
contactString = [contactString stringByReplacingOccurrencesOfString:#"(" withString:#""];
contactString = [contactString stringByReplacingOccurrencesOfString:#")" withString:#""];
txtRecipient.text = contactString;
}
This is my code. I am using contactPicker to pick a contact from phonebook. Then I am storing it in to a string variable. After that I am removing dashes, brackets and spaces from string value by using stringByReplacingOccurrencesOfString. Every thing is working fine except for this line:
contactString = [contactString stringByReplacingOccurrencesOfString:#" " withString:#""];
After this line of code contactString remains the same i.e, spaces didn't get removed from the string. I also tried componentsSeparatedByString function but its returning only 1 character. i.e,
NSArray *components = [dateString componentsSeparatedByString:#" "];
components.length is returning 1.
Hope you understand my question. Is there any other way of removing spaces from a string? Any kind of help would be appreciable. Thanks.
Look like this is not a standard space.
Try this:
NSMutableCharacterSet* set = [NSMutableCharacterSet whitespaceCharacterSet];
[set addCharactersInString:#"()-"];
NSMutableString * contactString = [[phoneNumberValue valueForKey:#"_stringValue"] mutableCopy];
NSRange range;
while ((range = [contactString rangeOfCharacterFromSet:set]).location!=NSNotFound) {
[contactString deleteCharactersInRange:range];
}
Hey folkks I'm working with an api that search about movies now the problem is when i type single word in UISearchBar it works but when I type space for another word it dose'nt work.Near query=%# when i type single word it works but when I type another word with space it won't
NSString *movieName=searchBar.text;
NSString *movieString = [NSString stringWithFormat:#"https://api.themoviedb.org/3/search/movie?query=%#&api_key=c4bd81709e87b1209433c49",movieName];
NSURL *url=[NSURL URLWithString:movieString];
You don't need to replace Space with Dash you new to URL Encode your string
Below is an example of encoding your string to URL Encode
NSString *movieName=searchBar.text;
movieName = [movieName stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLUserAllowedCharacterSet]];
NSString *movieString = [NSString stringWithFormat:#"https://api.themoviedb.org/3/search/movie?query=%#&api_key=c4bd81709e87b1209433c49",movieName];
NSURL *url=[NSURL URLWithString:movieString];
Use
formattedString = [originalString stringByReplacingOccurrencesOfString:#" " withString:#"-"];
Please refer this code.
NSString *movieName=searchBar.text;
NSString* replacedMovieName = [movieName stringByReplacingOccurrencesOfString:#" " withString:#"_"];
Hope this helps.
No need replace space with dash, need to add Percent Escapes in string before creating NSURL.
Use NSString class method:
-(NSString *)stringByAddingPercentEscapesUsingEncoding:(NSStringEncoding)encoding
Like,
NSString *movieName=searchBar.text;
//This is what you need
movieName=[movieName stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *movieString = [NSString stringWithFormat:#"https://api.themoviedb.org/3/search/movie?query=%#&api_key=c4bd81709e87b1209433c49",movieName];
NSURL *url=[NSURL URLWithString:movieString];
OR
Alternately you can do following(Not best approach):
NSString *movieName=searchBar.text;
//This is what you need
movieName = [movieName stringByReplacingOccurrencesOfString:#" " withString:#"%20"];
NSString *movieString = [NSString stringWithFormat:#"https://api.themoviedb.org/3/search/movie?query=%#&api_key=c4bd81709e87b1209433c49",movieName];
NSURL *url=[NSURL URLWithString:movieString];
NSString *movieName=searchBar.text;
movieName = [movieName stringByReplacingOccurrencesOfString:#" " withString:#"-"];//use vice-versa
NSString *movieString = [NSString stringWithFormat:#"https://api.themoviedb.org/3/search/movie?query=%#&api_key=c4bd81709e87b1209433c49",movieName];
NSURL *url=[NSURL URLWithString:movieString];
This is my code and in my url string white spaces are not encoded by the code
NSString *str = item.fileArtPath;
NSCharacterSet *set = [NSCharacterSet URLQueryAllowedCharacterSet];
[str stringByAddingPercentEncodingWithAllowedCharacters:set];
[str stringByReplacingOccurrencesOfString:#" " withString:#"%20"];
NSURL *urlString = [NSURL URLWithString:str];
for below string:
http://xx.xx.xx.xxx:xx/xx/xx/xx/xxx/Audio Adrenaline.jpg
^^^^^^^^^^^^^^^^^^^^
The white space after Audio is not converted to %20 after I used string replacement. And in Debugging urlString is nil why so?
From the NSString Class Reference
Returns a new string made from the receiver by replacing all
characters not in the specified set with percent encoded characters.
Meaning it doesn't permute the instance it's called on. You need to say something like str = [str stringByAddingPercentEncodingWithAllowedCharacters:set]; Same with [str stringByReplacingOccurrencesOfString:#" " withString:#"%20"];
Recall that NSString is immutable. Calling methods stringBy... returns the modified string, rather than modifying the original one. Therefore your code should be rewritten as follows:
str = [str stringByAddingPercentEncodingWithAllowedCharacters:set];
str = [str stringByReplacingOccurrencesOfString:#" " withString:#"%20"];
NSString *search = [searchBar.text stringByReplacingOccurrencesOfString:#" " withString:#"%20"];
I have to make calls programmatically in my iPhone app.
I have set of numbers in different countries with different formatting - braces, dots, spaces, "+" sign.
Can I simply remove all of this and left only numbers?
for example:
+1-(609) 452-8401 => 16094528401 // usa
+49(0)89.439 => 49089439 // germany
+1-(949)586-1250 => 19495861250 // los angeles, usa
Will it be correct?
try this:-
NSMutableString *str1=[[NSMutableString alloc] initWithString:telephoneString];
[str1 setString:[str1 stringByReplacingOccurrencesOfString:#"(" withString:#""]];
[str1 setString:[str1 stringByReplacingOccurrencesOfString:#")" withString:#""]];
[str1 setString:[str1 stringByReplacingOccurrencesOfString:#"-" withString:#""]];
[str1 setString:[str1 stringByReplacingOccurrencesOfString:#" " withString:#""]];
telephoneString = [#"tel://" stringByAppendingString:str1];
[str1 release];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:telephoneString]];