I have the following code:
phoneNumber = (NSString*)CFBridgingRelease(ABMultiValueCopyValueAtIndex(phones, i));
NSString *phoneNumberURLString = [[NSString alloc] initWithFormat:#"tel://%#", phoneNumber];
NSURL *phoneURL = [[NSURL alloc] initWithString:phoneNumberURLString];
[[UIApplication sharedApplication] openURL:phoneURL];
However, the issue with this is that, sometimes a certain phone number doesn't get called: for example if phoneNumber is (520) 123-1232, then this doesn't call the phone. Any idea on how to standarize the phone number format such that this method works for all cases?
It is the spaces in the number. A space is not a valid URL character. It must be encoded propery as %20.
Related
I have this code in a button click for ratting this app
-(IBAction)_clickbtnratethisApp:(id)sender
{
NSString *str = #"itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa";
str = [NSString stringWithFormat:#"%#/wa/viewContentsUserReviews?", str];
str = [NSString stringWithFormat:#"%#type=Purple+Software&id=", str];
// Here is the app id from itunesconnect
str = [NSString stringWithFormat:#"%#289382458", str];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
}
but nothing happens when I click the button,my need is I want to set a alert view to show like the ordinary alert like ratethisapp,nothanks,later.If I tap the ratethisapp I will redirected to the ring page of appstore.How to active this?Thanks in advance.
The itms-apps: URL syntax doesn't work on the simulator but works fine on the device, so that's probably the only thing wrong with your code and if you try it on a device it will work fine.
Unrelated tip: You can concatenate strings using the stringByAppendingString: and stringByAppendingFormat: methods, that will save some code versus the way you are building your string currently.
I'd like to make a button call a phone number entered by the user inside the text field. I have a code but it doesn't work.
NSString * phoneNumber = [NSString stringWithFormat:#"%#%#", #"tel://", phoneNumber.text];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];
Anyone has a similar approach to this? Thanks.
I think it's tel: instead of tel://. See this Apple document. Try giving this a shot:
NSString *pn = [#"tel:" stringByAppendingString:phoneNumber.text];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:pn]];
See my answer to another question for some sample code to handle cases with invalid input.
Basically you do this:
NSString *cleanedString = [[phoneNumber componentsSeparatedByCharactersInSet:[[NSCharacterSet characterSetWithCharactersInString:#"0123456789-+()"] invertedSet]] componentsJoinedByString:#""];
NSString *escapedPhoneNumber = [cleanedString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *telURL = [NSURL URLWithString:[NSString stringWithFormat:#"tel://%#", escapedPhoneNumber]];
Update: I noticed that the string you created shares the some name ("phoneNumber") as the text field from which you try to get the text. You may want to rename either of those two.
I have problem with NSURL. I am trying to create NSURL with string
code
NSString *prefix = (#"tel://1234567890 ext. 101");
NSString *dialThis = [NSString stringWithFormat:#"%#", prefix];
NSURL *url = [[NSURL alloc] initWithString:dialThis];
NSLog(#"%#",url);
also tried
NSURL *url = [NSURL URLWithString:dialThis];
but it gives null . what is wrong ?
Thanks..
Your problem is the unescaped spaces in the URL. This, for instance, works:
NSURL *url = [NSURL URLWithString:#"tel://1234567890x101"];
Edit: As does this..
NSURL *url2 = [NSURL URLWithString:[#"tel://1234567890 ext. 101"
stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
Before passing any string as URL you don't control, you have to encode the whitespace:
NSString *dialThis = [prefix stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
// tel://1234567890%20ext.%20101
As a side note, iOS is not going to dial any extension. The user will have to do that manually.
From Apple URL Scheme Reference: Phone Links:
To prevent users from maliciously redirecting phone calls or changing the behavior of a phone or account, the Phone application supports most, but not all, of the special characters in the tel scheme. Specifically, if a URL contains the * or # characters, the Phone application does not attempt to dial the corresponding phone number.
Im not sure the "ext." in phone number can be replce by what value? but you can try like this,
NSString *prefix = [NSString stringWithString: #"tel://1234567890 ext. 101"];
NSString *dialThis = [NSString stringWithFormat:#"%#", prefix];
NSURL *url = [NSURL URLWithString:[dialThis stringByReplacingOccurrencesOfString:#" ext. " withString:#"#"]];
// it might also represent by the pause symbol ','.
you can go to find the ext. is equivalent to what symbol in the phone, then replace it.
but dunno it can be work in actual situation or not....
As with iOS 9.0,
stringByAddingPercentEscapesUsingEncoding:
has been deprecated.
Use the following method for converting String to NSURL.
let URL = "URL GOES HERE"
let urlString = URL.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLFragmentAllowedCharacterSet())
If you've got something you think should be a URL string but know nothing about how URL strings are supposed to be constructed, you can use NSURL's URLWithDataRepresentation:relativeToURL: method. It parses the URL string (as bytes in an NSData) and percent-encodes characters as needed. Use the NSUTF8StringEncoding for best results when converting your NSString to NSData.
NSURL *url = [NSURL URLWithDataRepresentation:[#"tel:1234567890 ext. 101" dataUsingEncoding:NSUTF8StringEncoding] relativeToURL:nil];
NSLog(#"%#",url);
creates a URL with the string 1234567890%20ext.%20101
It attempts to do the right thing. However, for best results you should find the specification for the URL scheme you using and follow it's syntax to create your URL string. For the tel scheme, that is https://www.rfc-editor.org/rfc/rfc3966.
P.S. You had "tel://" instead of "tel:" which is incorrect for a tel URL.
Try this one, It works for me....
NSString *prefix = (#"tel://1234567890 ext. 101");
NSString *dialThis = [NSString stringWithFormat:#"%#", prefix];
NSURL *url = [NSURL URLWithString:[queryString stringByReplacingOccurrencesOfString:#" " withString:#"%20"]];
NSLog(#"%#",url);
Make an extension for use in any part of the project as well:
extension String {
var asNSURL: NSURL! {
return NSURL(string: self)
}
}
From now you can use
let myString = "http://www.example.com".asNSURL
or
myString.asNSURL
I have an iPhone app that will let users email some pre-determined text as HTML.
I'm having a problem in that if the text contains special characters within the text (e.g., ampersand &, >, <), the NSString variable that I use for sending the body of the email gets truncated at the special character.
I'm not sure how to fix this (I tried using the method stringByAddingPercentEscapesUsingEncoding…but this hasn't fixed the problems).
Thoughts on what I'm doing wrong / how to fix it?
Here is sample code showing what I'm trying to do
Thanks!!!
- (void)send_an_email:(id)sender {
NSString *subject_string = [NSString stringWithFormat:#"Summary of %#", commercial_name];
NSString *body_string = [NSString stringWithFormat:#"%#<br /><br />", [self.dl email_message]]; // email_message returns the body of text that should be shipped as html. If email_message contains special characters, the text truncates at the special character
NSString *full_string = [NSString stringWithFormat:#"mailto:?to=&subject=%#&body=%#", [subject_string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding], [body_string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[[UIApplication sharedApplication] openURL:[[NSURL alloc] initWithString:full_string]];
}
This works for me to send email from within the app. The way you have it, it quits the app and opens Mail. Try something like this:
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
... do your email setup code
[picker setMessageBody:emailBody isHTML:YES];
Here is a good tutorial
I'm trying to send the contents of UITextView or UITextField as parameters to a php file
NSString *urlstr = [[NSString alloc] initWithFormat:#"http://server.com/file.php?name=%#&tags=%#&entry=%#",nameField.text, tagsField.text, dreamEntry.text];
When i log urlstr, the url format is ok just as long as the UITextView or UITextField don't contain spaces. How would i go about converting the spaces to %20 ?
edit
here is the code at present, which not only crashes but isn't encoding the url properly.
name=John Doe&tags=recurring nightmare&entry=Testing testing testing
is converted to
name=John -1844684964oe&tags=recurringightmare&entry=Testing 4.214929e-307sting -1.992836e+00sting
- (IBAction)sendButtonPressed:(id)sender
{
NSString *urlString = [[NSString alloc] initWithFormat:#"http://server.com/file.php?name=%#&tags=%#&entry=%#", nameField.text, tagsField.text, dreamEntry.text];
NSString *encodedString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [[NSURL alloc] initWithString:encodedString];
NSLog(encodedString);
NSLog(urlString);
[urlString release];
[url release];
[encodedString release];
}
Actually, all of the previous answers contain at least some inaccuracies, which for many common values of user provided text in the TextFields would not correctly communicate with the server
stringByAddingPercentEscapesUsingEncoding: percent escapes all characters which are not valid URL characters. This method should applied once to the entire URL.
A previous answer claims that stringByAddingPercentEscapesUsingEncoding: works like the URL building classes in many scripting languages, where you should not apply it to the entire URL string, but it doesn't. Anyone can easily verify this by checking its output for unescaped &s and ?s. So it is fine to apply to the entire string, but it is not enough to apply to your 'dynamic' url content.
The previous answer is right in that you have to do some more work to the names and values that go into your CGI query string. Since CGI is specified by RFC3875, this is often referred to as RFC3875 percent escaping. It makes sure that your names and values don't contain characters that are valid URL characters but which are significant in other parts of the URL (;, ?, :, #, &, =, $, +, {, }, <, >, and ,)
However, it is very important to also finish by doing plain URL percent escapes on the full string to make sure that all characters in the string are valid URL characters. While you don't in your example, in general there could be characters in a 'static' part of the string which are not valid URL characters, so you do need to escape those as well.
Unfortunately, NSString doesn't give us the power to escape the RFC3875 significant characters so we have to dip down into CFString to do so. Obviously using CFString is a pain so I generally add a Category onto NSString like so:
#interface NSString (RFC3875)
- (NSString *)stringByAddingRFC3875PercentEscapesUsingEncoding:(NSStringEncoding)encoding;
#end
#implementation NSString (RFC3875)
- (NSString *)stringByAddingRFC3875PercentEscapesUsingEncoding:(NSStringEncoding)encoding {
CFStringEncoding cfEncoding = CFStringConvertNSStringEncodingToEncoding(encoding);
NSString *rfcEscaped = (NSString *)CFURLCreateStringByAddingPercentEscapes(
NULL,
(CFStringRef)self,
NULL,
(CFStringRef)#";/?:#&=$+{}<>,",
cfEncoding);
return [rfcEscaped autorelease];
}
#end
With this Category in place, the original problem could be correctly solved with the following:
NSString *urlEscapedBase = [#"http://server.com/file.php" stringByAddingPercentEscapesUsingEncoding:
NSUTF8StringEncoding];
NSString *rfcEscapedName = [nameField.text stringByAddingRFC3875PercentEscapesUsingEncoding:
NSUTF8StringEncoding];
NSString *rfcEscapedTags = [tagsField.text stringByAddingRFC3875PercentEscapesUsingEncoding:
NSUTF8StringEncoding];
NSString *rfcEscapedEntry = [dreamEntry.text stringByAddingRFC3875PercentEscapesUsingEncoding:
NSUTF8StringEncoding];
NSString *urlStr = [NSString stringWithFormat:#"%#?name=%#&tags=%#&entry=%#",
urlEscapedBase,
rfcEscapedName,
rfcEscapedTags,
rfcEscapedEntry];
NSURL *url = [NSURL URLWithString:urlStr];
This is a little variable heavy just be more clear. Also note that the variable list provided to stringWithFormat: should not be nil terminated. The format string describes the precise number of variables that should follow it. Also, technically the strings for query string names (name, tags, entry,..) should be run through stringByAddingPercentEscapesUsingEncoding: as a matter of course but in this small example we can easily see that they contain no invalid URL characters.
To see why the previous solutions are incorrect, imagine that the user input text in dreamEntry.text contains an &, which is not unlikely. With the previous solutions, all text following that character would be lost by the time the server got that text, since the unescaped ampersand would be interpreted by the server as ending the value portion of that query string pair.
You're not supposed to URL-escape the entire string, you're supposed to URL-escape the dynamic components. Try
NSString *urlStr = [NSString stringWithFormat:#"http://server.com/file.php?name=%#&tags=%#&entry=%#",
[nameField.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
[tagsField.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
[dreamEntry.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
nil];
NSURL *url = [NSURL URLWithString:urlStr];
The second issue with your code (and undoubtedly the reason for the odd printing) is you're passing the string directly to NSLog, so it's being treated as a format string. You need to use
NSLog(#"%#", encodedString);
instead. That will make it print as expected.
Edit: A third issue with your code is you're mixing autoreleased and owned objects, then releasing them all at the end. Go look at the 3 objects you create, and which you subsequently release later. One of them shouldn't be released later because it was produced by a method that did not start with the words alloc, copy, or new. Identifying the object in question is an exercise left to the reader.
You can take your URL and use:
NSString *urlStr = [[NSString alloc] initWithFormat:#"http://server.com/file.php?name=%#&tags=%#&entry=%#",nameField.text, tagsField.text, dreamEntry.text];
NSString *encStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];