Fetching JSON with %20 in the link - ios

I have variable date=#"2014-06-18 12:59:46"
I do the following:
NSString* formated_date = [[Constants shared].date stringByReplacingOccurrencesOfString:#" " withString:#"%%20"];
which gives me the wanted output of the new-formed variable: "2014-06-18%2012:59:46".
Now, when I put all that into string, which represents the url, I cannot fetch JSON from within the app.
BUT, when i NSLog the generated url, and copy paste it in browser - it works.
Also, if i take, c/p the Logged url, and hardcode it into the json request, I get the demanded json just as intended.
This is my code so far:
NSString* formated_date = [[Constants shared].date stringByReplacingOccurrencesOfString:#" " withString:#"%%20"];
NSString* url = [NSString stringWithFormat:
#"http://api.fessor.da.kristoffer.office/homework?rest&_rp[date]=%#&_rp[uuid]=%#&_rp[workspace]=parents&_rp[token]=%#&_rp[user_id]=%#&child_id=%#&type=parents&start_date=%#&end_date=%#",
formated_date,[Constants shared].uuid,[Constants shared].token,[Constants shared].user_id, [Constants shared].user_id,[Constants shared].start_date, [Constants shared].end_date
];
NSLog(url);
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
//Load the json on another thread
[Constants shared].jsonDataHomework = [[NSData alloc] initWithContentsOfURL:
[NSURL URLWithString:url"]];
dispatch_async(dispatch_get_main_queue(), ^{
[self getPresentHomework];
});
});
[self getPresentHomework] then returns an error, since the data from the json is nil.
I repeat once again, i get nil data when the code for fetching json is like this:
[Constants shared].jsonDataHomework = [[NSData alloc] initWithContentsOfURL:
[NSURL URLWithString:url]];
but if i hardcode the url, then i get the desired result
[Constants shared].jsonDataHomework = [[NSData alloc] initWithContentsOfURL:
[NSURL URLWithString:#"http://api.fessor.da.kristoffer.office/homework?rest&_rp[date]=2014-06-18%2012:59:46&_rp[uuid]=289A6F6F-BB71-444A-B16B-DCAF0070E1D3&_rp[workspace]=parents&_rp[token]=7fe3768108445570f11bf333cb821b0bee9213d2cced91a1f63f8c648fbc3e6a&_rp[user_id]=22066&child_id=22066&type=parents&start_date=2014-06-18&end_date=2014-06-18"]];
What am I doing wrong?
Should I change the order of threads or something like that?

Try to append your parameters to the URL string without adding percentage and then use stringByAddingPercentEscapesUsingEncoding before assigning it to your NSURL.
NSString *urlString = [NSString stringWithFormat:
#"format your url"];
NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

The URL string contain special character so use
NSString *strUrl=[#"http://api.fessor.da.kristoffer.office/homework?rest&_rp[date]=2014-06-18%2012:59:46&_rp[uuid]=289A6F6F-BB71-444A-B16B-DCAF0070E1D3&_rp[workspace]=parents&_rp[token]=7fe3768108445570f11bf333cb821b0bee9213d2cced91a1f63f8c648fbc3e6a&_rp[user_id]=22066&child_id=22066&type=parents&start_date=2014-06-18&end_date=2014-06-18" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

Related

How to convert NSData with textEncoding utf-8 into NSURL

I want to convert NSData (textEncoding utf-8) into NSURL.
I am writing below code for this, but conversion from NSData to NSString returning nil. (May be due to encoding type)
NSString *stringFromData = [[NSString alloc] initWithData:myData encoding:NSUTF8StringEncoding]; // stringFromData is nil after execution of this line.
NSURL *url = [[NSURL alloc] initWithString:stringFromData];
So what should I do to convert NSData into NSURL in my case.
But when I am trying to load this data into webview, Its working good. Here is my code to load this data into webview.
[self.webView loadData:myData MIMEType:#"application/pdf" textEncodingName:#"utf-8" baseURL:nil]; //PDF is showing in my webview.
but I am not able to convert this NSData into NSURL. What encoding should I use to convert NSData with textEncoding utf-8 into NSURL ?
You're trying to load data that represents a PDF into an NSString. A PDF file does not consist of UTF-8 encoded characters that represent text, it's a file that contains header information, fonts, vector graphics AND text.
The only solution to your problem in my mind is change the source of the NSData to something that will provide UTF-8 encoded characters that make up a URL.
If you can't get data any other way, why not check if you can extract the textual data from the PDF? https://github.com/zachron/pdfiphone
Have you tried writing the data to local file,
NSString *docPath = [NSHomeDirectory() stringByAppendingString:#"/Documents"];
NSString *pdfFilePath = [docPath stringByAppendingPathComponent:#"pdfFile.pdf"];
BOOL success = [myData writeToFile:pdfFilePath atomically:YES];
if (success) {
NSURL *url = [[NSURL alloc] initFileURLWithPath:pdfFilePath];
NSLog(#"url from data : %#",url);
}
Hope this helps.
Thanks
Ok, try this:
//Convert data to string
NSString *urlString = [[NSString alloc] initWithData:self encoding:NSUTF8StringEncoding];
//Convert the string to URL
NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

iOS NSURL String

I am trying to place strings inside my NSURL, I have tried most suggestions but all seem to fail, i'm trying to find a easy way to do it instead of having to use some long request with long code. I'm wondering is there anything small here i'm forgetting?
NSString *lat = #"53.350628";
NSString *lng = #"-6.254997";
NSURL *blogURL = [NSURL URLWithString:#"http://url.com/jsontest.php?lat=%#&lng=%#&max=5", lat, lng];
NSData *jsonData = [NSData dataWithContentsOfURL:blogURL];
Thanks,
Curtis
+[NSURL URLWithStirng:] does not take a format string and a variable argument list. It just takes a string, so you should use something like +[NSString stringWithFormat:] first before passing it to URLWithString, like this:
NSURL *blogURL = [NSURL URLWithString:[NSString stringWithFormat:#"http://url.com/jsontest.php?lat=%#&lng=%#&max=5", lat, lng]];
Format the string, than add that to the NSURL
NSString *lat = #"53.350628";
NSString *lng = #"-6.254997";
//Format String
NSString *urlString = [NSString stringWithFormat:#"http://url.com/jsontest.php?lat=%#&lng=%#&max=5", lat, lng];
NSURL *blogURL = [NSURL URLWithString:urlString];
NSData *jsonData = [NSData dataWithContentsOfURL:blogURL];

Unable to create NSURL with special characters (éè) with Objective C

I am unable to load an image because of nil returned by NSURL. The url looks like this : http://www.example.fr/wp-content/uploads/2014/06/préliminaire.jpg. When I enter my URL on firefox, I get my image but when I try in firefox the encoder http://www.example.fr/wp-content/uploads/2014/06/pr%3Flinaire.jpg I have no image...
Here is the code I use to load my image :
NSString *myString = #"http://www.example.fr/wp-content/uploads/2014/06/préliminaire.jpg";
NSURL *url = [NSURL URLWithString:myString];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc]initWithData:data];
That returns nil for url, so everything after is nil..
Thank you for your help guys
That's because those characters are not valid in a URL. Escape them first:
NSString *escaped = [myString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:escaped];
// etc.

Getting an image fails to create NSURL

Im trying to get an image from a url link.
NSURL *thmbnailUrl = [NSURL URLWithString:[NSString stringWithFormat:#"http://%#:800/fishtune/movie/%#.png",[[DBHelper sharedInstance] currentHostIP],[arr objectAtIndex:0]]];
NSLog(#"thmbnail:%#",[NSString stringWithFormat:#"http://%#:800/fishtune/movie/%#.png",[[DBHelper sharedInstance] currentHostIP],[arr objectAtIndex:0]]);
NSData *data = [NSData dataWithContentsOfURL:thmbnailUrl];
NSLog(#"movimgdata:%#",data);
UIImage *img = [[UIImage alloc]initWithData:data];
NSLog(#"movimg:%#",data);
NSData *imgData = UIImagePNGRepresentation(img);
when i log thmbnail i get this "ipofserverinnetwork/fishtune/movie/Our Logo 480p back.png"
Note:i didn't put the original ip.
I get null when i log data and of course img will also be null. I really dont know why I'm getting null. because when I tried entering the url in a browser it displays the image. I used the same code in another part of my app and it works but when i used it in this it fails. Is it because of the spaces between "Our_Logo_480p"?
you might need to use
NSString* escapedUrl = [yourstring stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
yes it's because of spaces, use
NSString *myUrl = [NSString stringWithFormat:#"http://%#:800/fishtune/movie/%#.png",[[DBHelper sharedInstance] currentHostIP],[arr objectAtIndex:0]];
NSString *url = [myUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

NSURL object value is null

I am generating a URL as given by string parameters but url gives null value.
For generating URL I have implemented the following code .
NSURL *url = [[NSURL alloc] init];
NSString *strURL = #"ftp://Administrat:ABC(R%-#TRDOP#xx.xx.xx.xx/arrows.png";
url = [NSURL URLWithString:[NSString stringWithString:strURL]];
NSLog(#"URL :: %#",url);
Thanks
You need to escape the special characters in the url query string.
Use:
NSString *strURL = #"ftp://www.jerox.com/Administrator:#123#TRDOP#%$/arrows.png";
strURL =[strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:strURL];
NSLog(#"URL :: %#",url);
Also I need to mention some mistakes in your code:
No need to allocate and init the NSURL object, because you are again assigning another object to that pointer
No need of using stringWithString: there
Make use of the following code.
NSString *sURL = #"ftp://www.jerox.com/Administrator:#123#TRDOP#%$/arrows.png";
NSURL *url = [[NSURL alloc] initWithString:[sURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSLog(#"URL :: %#",url);
From your posted code sample, here is the problem:
NSString *strURL = #"ftp://Administrat:ABC(R%-#TRDOP#xx.xx.xx.xx/arrows.png";
Look carefully at the authority component (username, password and host). An # symbol is used in URLs to separate username & password from the host. Because your password contains an # character, it must be percent escaped. The percent encoding of # is %40, giving you instead the code:
NSString *strURL = #"ftp://Administrat:ABC(R%-%40TRDOP#xx.xx.xx.xx/arrows.png";
You really ought to be escaping other URL-specific characters in there too, like the lone % symbol.

Resources