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]];
Related
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.
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 tried constructing the following NSURL for a custom URL scheme:
NSURL *url = [NSURL URLWithString:#"tweetie:///post?message=안녕 모두"];
[[UIApplication sharedApplication] openURL:url];
but it doesn't perform correctly. How would you construct such a URL with non-English elements?
Apparently you may only send US-ASCII characters in URI strings. Try adding percent escapes to your string before sending it:
NSURL* url = [NSURL URLWithString:
[#"tweetie:///post?message=안녕 모두"
stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
See String Conversion in Objective-C
I have following code in my application.
NSData *data=[NSData dataWithContentsOfURL:[NSURL URLWithString:pathOfThumbNail]];
pathOfThumbNail has following path
http://70.84.58.40/projects/igolf/TipThumb/GOLF 58B.jpg
When I open above path in safari browser - path is changed automatically & image is successfully displayed.
http://70.84.58.40/projects/igolf/TipThumb/GOLF%2058B.jpg
But in iPhone, due to space in path, image isn't loaded in nsdata.
Use: stringByAddingPercentEscapesUsingEncoding:
Returns a representation of the receiver using a given encoding to determine the percent escapes necessary to convert the receiver into a legal URL string.
-(NSString *)stringByAddingPercentEscapesUsingEncoding:(NSStringEncoding)encoding
A representation of the receiver using encoding to determine the percent escapes necessary to convert the receiver into a legal URL string. Returns nil if encoding cannot encode a particular character
Added per request by #rule
NSString* urlText = #"70.84.58.40/projects/igolf/TipThumb/GOLF 58B.jpg";
NSString* urlTextEscaped = [urlText stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString: urlTextEscaped];
NSLog(#"urlText: '%#'", urlText);
NSLog(#"urlTextEscaped: '%#'", urlTextEscaped);
NSLog(#"url: '%#'", url);
NSLog output:
urlText: '70.84.58.40/projects/igolf/TipThumb/GOLF 58B.jpg'
urlTextEscaped: '70.84.58.40/projects/igolf/TipThumb/GOLF%2058B.jpg'
url: '70.84.58.40/projects/igolf/TipThumb/GOLF%2058B.jpg'
A swift 3.0 approach (stringByAddingPercentEscapesUsingEncoding and stringByAddingPercentEncodingWithAllowedCharacters seems now deprecated):
let urlString ="your/url/".addingPercentEncoding( withAllowedCharacters: .urlQueryAllowed)
stringByAddingPercentEscapesUsingEncoding has been deprecated in iOS 9.0, it is recommended you use stringByAddingPercentEncodingWithAllowedCharacters instead.
Here's the Objective-C code for > iOS 9.0
NSString* urlText = #"70.84.58.40/projects/igolf/TipThumb/GOLF 58B.jpg";
NSString* urlTextEscaped = [urlText stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
NSURL *url = [NSURL URLWithString: urlTextEscaped];
NSLog(#"urlText: '%#'", urlText);
NSLog(#"urlTextEscaped: '%#'", urlTextEscaped);
NSLog(#"url: '%#'", url);
I have code similar to the following with a URL like this... If I use the first *url, webpage will return null. If I put this URL into a URL shortening system like bit.ly it does work and returns the pages HTML as a string. I can only think I have invalid characters in my first *url? Any ideas?
NSString *url =#"http://www.testurl.com/testing/testapp.aspx/app.detail/params.frames.y.tpl.uk.item.1.cm_scid.TB-test/left.html.|metadrill,html/walk.yah.ukHB?cm_re=LN-_-OnNow-_-TestOne";
//above *url does not work, one below does
NSURL *url =[NSURL URLWithString: #"http://bit.ly/shortened"];
NSString *webpage = [NSString stringWithContentsOfURL:url];
You probably need to escape some characters in the first URL, as follows:
NSString *url =#"http://www.testurl.com/testing/testapp.aspx/app.detail/params.frames.y.tpl.uk.item.1.cm_scid.TB-test/left.html.|metadrill,html/walk.yah.ukHB?cm_re=LN-_-OnNow-_-TestOne";
NSString *escapedURL = [url stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSString *webpage = [NSString stringWithContentsOfURL:[NSURL URLWithString:escapedURL]];
The construction of the URL and its fetch will fail if the URL contains characters that aren't escaped properly (looking at your URL, it's probably the pipe (|), question mark, or underscore).