encoding an url string if it contans the following string - iphone

I am using the code like
searchQuery = [searchQuery stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
NSString* urlString = [NSString stringWithFormat:#"http://google.com/#auto|en|%#", searchQuery];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
The code is run when I pressed a button app not open the browser, if I remove the last phrase "#auto|en|" it is working fine. How I can encode the string to give an url.

check the searchQuery it is not accepting your any parameters like this.. or give more description so other people can give proper response on it.

Related

calling phone number within the app issue

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.

How to call a phone from a number entered in a UITextField?

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.

NSURL with string

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

iphone - open URL with japanese characters

In my application, the requirement is to use UIApplication's openURL method to start browser with following URL:
http://192.168.100.80/1003/images/test/いうydさdfghjk-320x160.png
Above string is stored in NSString.
When I am passing above URL as parameter to openURL, its saying that the page is not found and I noticed that the URL in the address bar is not in japanese characters.
How can I show above URL in safari?
You have to escape japanese characters using UTF8 encoding and then replace them:
NSString *query = #"ファイル";
NSString *encodedQuery = [query stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
NSString *urlString = [NSString stringWithFormat:#"http://ja.wikipedia.org/wiki/%#:East_Asian_Cultural_Sphere.png", encodedQuery];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: urlString]];

iphone email link not selected correctly

I'm creating a link to open my App and pass some data in the URL.
When I add the query parameter ? my link get broken.
NSData *fileData = [NSData dataWithContentsOfFile:dataFilePath];
NSString *encodedString = [GTMBase64 stringByWebSafeEncodingData:fileData padded:YES];
NSString *urlString = [NSString stringWithFormat:#"myApp://localhost/backup?%#",
encodedString];
the link is quite long, but also a shorter one doesn't work:
myApp://localhost/backup?PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz48bG9jPjxpbmdyZWRpZW50VHlwZS
and when the e-mail appears in the iPhone, only this is underlined and act as a link:
myApp://localhost/
Adding the query as NeilInglis suggest it doesn't work also, the link is broken at same place.
NSString *urlString = [NSString stringWithFormat:#"myApp://localhost/backup?query=%#",
encodedString];
The Html is ON or OFF, it doesn't affect.
If I enocode the URL it also doesn't work ...
Don't know what I can try next ...
any ideas ?
thanks ...
regards,
r.
Try mapping the query parameter to a name.
#"myApp://localhost/backup?queryParamName=%#