UIApplication openURL can not open a url - uitableview

I'm trying to implement a job search App. The results are shown to the user in a UITableView.
When a user clicks on a cell, it should open the original Job announcement.
To do this, i implemented the following method:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *url = [[NSString alloc]init];
url=[[[xmlParser jobs] objectAtIndex:indexPath.row] urlAddress]; //UrlAddress is an instance variable of type NSString
NSURL *urlJobDetail = [NSURL URLWithString:(url)];
[[UIApplication sharedApplication] openURL: urlJobDetail];
}
The interesting part is: if i type an NSString like #"http://www.google.com" or any other link, it works. But when i try to open a "the urlJobDetail", it just doesn't work... Nothing happens at all...
And i searched it in stackoverflow.com and found this:
url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
Than the url works but this method changes the original url address and adds lots of % signs like:
"http://www.google.com%20 %20 %20"
So i get an page not found error.
I don't understand why this function doesn't accept a regular NSString variable as?
I checked it with NSLog and the url seems to be perfectly in order.
Any help would be much, very much appreciated !
Thanks in advance

Because it's URL specification related restrictions
Spaces and control characters in URLs must be escaped for transmission in HTTP, as must other disallowed characters... It is necessary to encode any characters disallowed in a URL, including spaces and other binary data not in the allowed character set, using the standard convention of the "%" character followed by two hexadecimal digits.

Related

How to pass GET request for PayTm transaction in ios Objective C

I am integrating PayTm with my app and I want to pass the parameters using GET method.
My code is as follows:
NSString *urlString = [NSString stringWithFormat:#"https://secure.paytm.in/oltp/HANDLER_INTERNAL/TXNSTATUS?JsonData={%22MID%22:%22%#%22,%22ORDERID%22:%22a84afd6c-0e54-42df-b29a-2b057f9e7c53%22}",MIDValue];
where MIDValue is a string.
When I use this code I'm getting error message.
Please give a suggestion to remove the error.
Thank You
Maybe your variable MIDValue contains space and/or &.
You first have to encode it as URL and then pass it as parameter.
Visit this for details, I guess this is what you are looking for
NSString *newParam = [MIDValue stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]]
NSString *urlString = [NSString stringWithFormat:#"https://secure.paytm.in/oltp/HANDLER_INTERNAL/TXNSTATUS?JsonData={%22MID%22:%22%#%22,%22ORDERID%22:%22a84afd6c-0e54-42df-b29a-2b057f9e7c53%22}", newParam];

split string in ios string from json data?

I have one string. Now I wanted to split this string. For static separation I know the code but I don’t code for dynamic value.
my string is
NSString *str = #"https://graph.facebook.com/v2.5/181054825200000/feed?fields=created_time,message,picture,full_picture,comments.limit%280%29.summary%28true%29,likes.limit%280%29.summary%28true%29&limit=5&format=json&access_token=CAALjFrE5mNYBAOg1EDiUrsE2kr1kIRrLIv7g4OweSMvHso2exB5Dttshn7dgOlW24ZCXSnDZAWiV6xMUKXedTXUhiHpdmZBPCGzD1orFlrLRP2gaBZCbZBZBnjUHewF9hZBmJKxtiwVzpw9gnnQXk5Hfx0ZBM2ksAUzkSWR5feaNMbf3UUmUpJlxeh0gKdDrzWBvIJRPy0xGqL0ZAMFsRhyCZCTX42l1sZAceZB0VCeDZB95mrAZDZD&until=1456345291&__paging_token=enc_AdCKD3tSYMoZB3MCKaJkYnbVmBgUyY2tBceGDD2G1hqxRDiQKZCsSbmvWZASLvlCMf0BVzq2uZAScSWp7ZAavZB2d72BIHJISefk09noRuv9gA5b5hFwZDZD";
but i don’t how to show any value dynamically .(for e.g. until (in string))
please help me for this issue.
Thank You.
If you are parsing a URL you should really use NSURLComponents. It makes breaking a URL into the different parts much easier, and the code is tested and verified by Apple.
For separate string by a separator you can use this.
NSString *url = #"<url>";
NSArray *array = [url componentsSeparatedByString:#"<seperator string>"];
NSLog(#"%#", array);
But for URL parsing ,As per Duncan's answer, yes it is good to parse a URL using NSURLComponents. By using this class you can get any desired part of an URL.

NSURL breaking port string with colon

I'm trying to add a base url for my networking code, the problem is that this URL gets broken when passing to the URLWithString:relativeToURL: method. This URL has the port i'm using, however, after calling the described, the URL is wrong, not including my current port number. I think this is a problem with percent escapers, but i've tried some methods to solve this problem without success.
Here is my code:
// absolute string returns a url a with broken path
[[NSURL URLWithString:#"api/whatever" relativeToURL:[NSURL URLWithString:#"193.178.0.99:9000"]] absoluteString]
// printed absolute path 193.173.0.99:///api/whatever
Other tried approaches:
NSString *baseURLString = (NSString *)CFURLCreateStringByAddingPercentEscapes(NULL,(CFStringRef)#"193.178.0.99:9000",NULL,(CFStringRef)#":",kCFStringEncodingUTF8);
[[NSURL URLWithString:#"api/whatever" relativeToURL:[NSURL URLWithString:baseURLString]]
// Printed path : 193.173.0.99%3A8000/api/whatever, this path is still not working, although i have the percent escape set.
NSString *baseURLString = [#"193.173.0.99:8000" stringByAddingPercentEscapesUsingEncoding : NSUTF8StringEncoding];
// ... The same final code from above.
// Printed -> the very same first result.
EDIT : From the comment above the URLWithString:relativeToURL: : "These methods expect their string arguments to contain any percent escape codes that are necessary."
Does anybody have a solution to this problem ?
Thanks.
Actually the solution is pretty simple... just add the scheme.
Something like this:
NSURL *baseURL = [NSURL URLWithString:#"http://193.178.0.99:9000"];
NSString *absoluteString = [[NSURL URLWithString:#"api/whatever" relativeToURL:baseURL] absoluteString];
// Prints => http://193.178.0.99:9000/api/whatever
That behavior is actually quite understandable (from an RFC point of view): the relativeToURL part is expected to be a full-fledged URL root, including the URL scheme.
So here in your example, as you didn't provide an http:// scheme or similar, 193.178.0.99 is considered to be the scheme — like it would be http or https or ftp or tel or mailto — and the 9000 port considered to be the host part of your URL (but as 9000 is probably not a valid host according to the RFC, it's probably why you have the warning by the way)
In a way, 193.178.0.99:9000 is interpreted in a similar manner a phone-number URL tel:1-541-754-3010 or a mail URL mailto:john.doe#nowhere.com would; the : separating the URL scheme from the host, not separating the host from the port.
To solve this, simply include the URL scheme (like http or https or whatever the protocol you intend to use) in the relativeToURL parameter:
[[NSURL URLWithString:#"api/whatever"
relativeToURL:[NSURL URLWithString:#"http://193.178.0.99:9000"]]
absoluteString]; // ^^^^^~~ this is the important part
Note: as an alternate solution to build your URL, you could use the iOS7's NSURLComponents class to manipulate NSURL parts separately, that's another way to break down and build up URLs

NSURL withString adds gibberish

I am trying to build a NSURL using a path string. The string looks fine but when I try to put that in an NSURL it gets a bunch of gibberish in it. Here is the code I am using to build up the url:
NSString* path = [[AppSettings instance].contentRootPath stringByAppendingPathComponent: item.fileName];
NSLog(path);
item.contentPath = [NSURL URLWithString:[path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSLog(item.contentPath.absoluteString);
And here is the log:
2012-07-25 10:28:39.983 mxHub[44036:207] /Users/casey.borders/Library/Application Support/iPhone Simulator/5.0/Applications/C2C1975D-7FAB-4E6C-A091-DC389223CC57/Documents/.content/PREZISTA-PI.pdf
2012-07-25 10:28:41.654 mxHub[44036:207] /Users/casey.borders/Library/Application扡潳畬整瑓楲杮䄀䅖獳瑥慃档e獡敳䍴捡敨楗桴剕㩌猀穩佥䕦瑮祲潆䭲祥:ㅱ䀲㨰䀴8敲潭敶湅牴䙹牯敋㩹挀牵敲瑮楓敺猀瑥慍䕸瑮祲楓敺:慭䕸瑮祲楓敺猀瑥慍卸穩㩥洀硡楓敺䀀䄢䅖獳瑥慃档䥥瑮牥慮≬䄀䅖獳瑥慃档䥥瑮牥慮l湩瑩楗桴楄瑣潩慮祲:慤慴獕湩䕧据摯湩㩧污潬䱷獯祳潃癮牥楳湯:慣䉮䍥湯敶瑲摥潔湅潣楤杮:湩瑩楗桴慄慴攺据摯湩㩧攀瑸湥敤䱤杯慄慴瑓楲杮湅潣楤杮攀瑸湥敤䱤杯慄慴䄀偖慬敹䥲整䅭捣獥䱳杯开捡散獳潌䅧牲祡攀敶瑮s灟慬敹䥲整䅭捣獥䱳杯䀀䄢偖慬敹䥲整䅭捣獥䱳杯湉整湲污"噁汐祡牥瑉浥捁散獳潌䥧瑮牥慮l潬䅧牲祡䄀偖慬敹䥲整䅭捣獥䱳杯癅湥t畮扭牥晏牄灯数噤摩潥牆浡獥椀摮捩瑡摥楂牴瑡e扯敳癲摥楂牴瑡e畮扭牥晏祂整味慲獮敦牲摥渀浵敢佲卦慴汬s畤慲楴湯慗捴敨d敳浧湥獴潄湷潬摡摥畄慲楴湯瀀慬批捡卫慴瑲晏獦瑥瀀慬批捡卫獥楳湯䑉渀浵敢佲卦牥敶䅲摤敲獳桃湡敧s敳癲牥摁牤獥s剕I汰祡慢正瑓牡䑴瑡e畮扭牥晏敓浧湥獴潄湷潬摡摥开汰祡牥瑉浥捁散獳潌䕧敶瑮䀀䄢偖慬敹䥲整䅭捣獥䱳杯癅湥䥴瑮牥慮≬吀ⱤⱒN噁汐祡牥瑉浥捁散獳潌䕧敶瑮湉整湲污搀捩t┊#噁汐祡牥瑉浥牅潲䱲杯开牥潲䱲杯牁慲y灟慬敹䥲整䕭牲牯潌g≀噁汐祡牥瑉浥牅潲䱲杯湉整湲污"噁汐祡牥瑉浥牅潲䱲杯湉整湲污䄀偖慬敹䥲整䕭牲牯潌䕧敶瑮攀牲牯潃浭湥t牥潲䑲浯楡n牥潲卲慴畴䍳摯e灟慬敹䥲整䕭牲牯潌䕧敶瑮䀀䄢偖慬敹䥲整䕭牲牯潌䕧敶瑮湉整湲污"噁汐祡牥瑉浥牅潲䱲杯癅湥䥴瑮牥慮l椀剳捥牯楤杮攀牲牯楗桴潄慭湩挺摯㩥獵牥湉潦:潶捩䍥湯牴汯敬䕲摮汐祡慢正湉整牲灵楴湯:潶捩䍥湯牴汯敬䕲摮敒潣摲湉整牲灵楴湯:潶捩䍥湯牴汯敬偲慬批捡䉫晵敦䅲慶汩扡敬戺晵敦㩲瘀楯散潃瑮潲汬牥楄卤慴瑲汐祡湩㩧畳捣獥晳汵祬:upport/iPhone ㈜ǪᶀЀꀠ/Āࠀ쵠޴imulator/5.0/Applications/C2C1975D-7FAB-4E6C-A091-DC389223CC57/Documents/.content/Demo.pdf
It's to do with NSLog, nothing to do with your urls :)
Try this :
NSString* path = [[AppSettings instance].contentRootPath stringByAppendingPathComponent: item.fileName];
NSLog(#"%#", path);
item.contentPath = [NSURL URLWithString:[path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSLog(#"%#", item.contentPath.absoluteString);
The first parameter for NSLog is a format string - this tells NSLog that there might be other values to insert into the output (these are the other optional parameters that NSLog takes - for example NSLog(#"%i", 5); would replace the %i with 5).
The NSLog(path); is fine because there aren't any special formatting characters in the path so it outputs as you would expect.
The second NSLog has % characters in - you added them when you asked to % escape the path :) For example, you replaced the space in 'Application Support' with %20.
This means that your url now contains the formatting code %20S (the S is the next character after the space, the first character in the word 'Support').
%S is interpreted by NSLog as 'take the next paramter that I have been given and treat it as a null terminated c-string'.
Unfortunately, you didn't give NSLog another parameter :) However, NSLog didn't know that - it just dutifully did as it was told and took the next value on the stack and added it to the output string.
The next value on the stack is garbage which is why your output is garbage!
You can see that the corrupted values appear instead of the spaces in your path - both spaces happen to be followed by an S so NSLog is dumping garbage in two places. That's why after the first load of garbage you can see 'upport/iPhone' and after the second 'imulator/5.0' :)
When you use NSLog(string), that string is treated as a format statement and any '%' characters will be interpreted.
Try printing the strings out using this form: NSLog(#"%#", string). As a general statement you should ALWAY use the above form and never the one you are using.

NSURL not returning nil for one-character non-ascii strings

I have been using NSURL to do a simple URL validation, mostly to weed out non-ascii special characters, which I do not want in my particular application. I take a URL as input into an NSString, then try to create an NSURL using URLWithString. If this returns nil, the app presents an error message.
For example if I enter "あか" as input (that is two Japanese characters), then the NSURL is nil. This has been working as expected. However I recently noticed that entering a string that is contains only one single non-ASCII character, NSURL processes it and returns a URL-encoded value. So if I enter "あ" as input, the resulting NSURL is NOT nil. The absoluteString value is "%E3%81%82".
I'm wondering if this is a bug in NSURL, or some kind of loophole that I'm not understanding.
I'm using Xcode 3.2.5, and the iOS 4.2 SDK.
I can't explain the behavior you are seeing above, however, if all you are trying to do is determine whether a URL string contains any non-ASCII characters, you could achieve this with the following code:
NSString *testURLString = #"http://www.googleあか.com";
NSCharacterSet* ascii = [NSCharacterSet characterSetWithRange: NSMakeRange(0, 128)];
NSCharacterSet* nonAscii = [ascii invertedSet];
if ([testURLString rangeOfCharacterFromSet:nonAscii].location != NSNotFound) {
NSLog(#"This string contains non-ASCII characters");
}

Resources