I am using this code to fetch application screenshots of an application from appStore using only appID,
NSString *numericIDStr = appID;
NSString *urlStr = [NSString stringWithFormat:#"http://itunes.apple.com/lookup?id=%#", numericIDStr];
NSURL *url = [NSURL URLWithString:urlStr];
NSData *json = [NSData dataWithContentsOfURL:url];
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:json options:0 error:NULL];
NSArray *results = [dict objectForKey:#"results"];
NSDictionary *result = [results objectAtIndex:0];
myArray = [result objectForKey:#"screenshotUrls"];
I managed to cache screenshots by using SDWebImage and it works perfect, but I noticed that every time that I call this method it delays about 3-4 seconds, and on 3G its even more, about 10-15 seconds.
and other issue that my app crash when not connected to the internet, is there any way to kinda cache this method or something similar?
You can save your response(cache) using the following libraries on disk or in memory.
TMCache
or EGOCache
Use AFNetworking. It will take one line of code and do the caching for you.
[yourImage setImageWithURL:yourNSUrl];
That's it! It does all the caching for you.
Related
NSString *urlString = [NSString
stringWithFormat:#"http://192.168.1.15/abc/service.asmx?op=GetCenter"];
NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSData *data = [[NSData alloc] initWithContentsOfURL:url];
NSError *error;
NSMutableArray *json =[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSLog(#"%#",[json description]);
I have try this but it returns null value insted of call function.
Program ended with exit code: 9(lldb)
How to recognized if webserive is called or not or how call that function?
If you could provide a bit more information as to what you mean by the function is not called, that would be very helpful, is there any stack trace or other information shown when it crashes?
I would troubleshoot this by stepping through each line of code in the debugger to make sure you are getting data back and then as suggested to print out the error from the serialization function.
It also might be worth using these methods:
NSString* newStr = [NSString stringWithContentsOfURL:url encoding: NSUTF8StringEncoding error:&error];
or
NSString* newStr = [[NSString alloc] initWithData:theData encoding:NSUTF8StringEncoding];
to get a string from your URL or data once you have downloaded it and then print out or view the contents.
Longer term you might want to think about using something like AFNetworking to do your networking calls as what you have is synchronous and ideally, you should be doing all networking asynchronously.
Good afternoon,
I'm trying to store a "text" using NSString from my JSON output but it's not working because it's always NULL. It's working fine with the other info (because they are integers) but when I have to store the info (the picture) in a NSString it's always NULL.
I have tried to search a lot of places in order to see how I have to declare that "NSString" and make it work but it's not working and I need some help.
Can you help me with that? How can I fill the NSString with the information in "picture" from my JSON output?
ProfileViewController:
NSString *responseData = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
SBJsonParser *jsonParser = [SBJsonParser new];
NSDictionary *jsonData = (NSDictionary *) [jsonParser objectWithString:responseData error:nil];
NSDictionary *jsonData = (NSDictionary *) [jsonParser objectWithString:responseData error:nil];
NSString *picture = [jsonData objectForKey:#"picture"];
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:picture]];
self.profileimage.image = [UIImage imageWithData:imageData];
JSON output:
{"success":1,"stars":0,"photos":0,"followers":0,"picture":http://m.c.lnkd.licdn.com/mpr/mpr/shrink_200_200/p/3/000/25b/1fe/200e9f3.jpg}
Thanks in advance.
The code looks correct, however the JSON output is not valid according to http://jsonlint.com/
You need to wrap the link with quotes ("http://m.c.lnkd.licdn.com/mpr/mpr/shrink_200_200/p/3/000/25b/1fe/200e9f3.jpg") and then try again.
I have the JSON Response in this format:
{“ContentPage”: {“ContentImage”: “avdsfg.png”, “Content”: “have html data ” } }
How do I parse the response so as to get the Image and Content for the Imageview and webView respectively in iOS. Guidance needed. A Beginner in iOS.
Thanks.
Try doing something like this. This is not the exact solution and you'll have to work around your way.
NSString *link = [NSString stringWithFormat:#"yourServiceURL"];
NSString *encdLink = [link stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url=[NSURL URLWithString:encdLink];
NSData *response = [NSData dataWithContentsOfURL:url];
NSError *error;
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:response options: NSJSONReadingMutableContainers error: &error];
Now you have your data in the array and extract it out using objectForKey or valueForKey.
This is what I am doing to fetch images from JSON and showing in my app. Firstly get the array from the above code. Then extract out the image content like this :
_ImageArray = [jsonArray valueForKey:#"ContentImage"];
Now you have the image name in your ImageArray.Since you have only imageName,you'll have to prepare the image URL in your code. Now to load the image in the ImageView, you need to do something like this.
NSURL *imageurl = [NSURL URLWithString:[_ImageArray objectAtIndex:indexPath.item]];
ImageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageurl]];
This is for image. You can do it for the webview also. Some changes will be required as its a Webview. But it'll be done.
My app is calling a basic PHP script that outputs JSON data.
I am calling my PHP script like this:
NSString *buildURL = [NSString stringWithFormat: #"http://xxxx.com/api.php?action=authenticate_user&email=%#&password=%#&deviceToken=%#", _emailAddress, _password, deviceToken];
NSURL *url = [NSURL URLWithString: buildURL];
NSData *data = [NSData dataWithContentsOfURL:url];
NSString *ret = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
// Parse the result
NSString *ret2 = [ret stringByReplacingOccurrencesOfString:#"null" withString:#""];
NSError* error;
NSDictionary *JSON =
[NSJSONSerialization JSONObjectWithData: [ret2 dataUsingEncoding:NSUTF8StringEncoding]
options: NSJSONReadingMutableContainers
error: &error];
NSString* status = [JSON objectForKey:#"status"];
NSString* email = [JSON objectForKey:#"email"];
NSString* userID = [JSON objectForKey:#"userID"];
NSString* agentID = [JSON objectForKey:#"agentID"];
NSString* picture = [JSON objectForKey:#"picture"];
For some reason when running this code on my iPhone all the variables are (null), but
in the simulator I get the correct data.
The output of my php script is:
{"status":"1", "agentName":"Bill", "picture":"http://xxxx.com/thumb_ad1-8908968097.jpg", "departmentName":"", "email":"test#gmail.com", "agentID":"513", "userID":"3"}null
Any ideas what I'm doing wrong?
I surely guess that your webservice is not live (your web service are on local server make them global).
Device is not connected with your current php server , from where you called webservice, from simulator it gives output because mac os is connected with server.
In my iOS 5.1 application, I'm trying to load a JSON text file into a NSDictionary.
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"stuff" ofType:#"json"];
NSDictionary *stuff = [NSDictionary dictionaryWithContentsOfFile:filepath];
The filepath is valid, the file exists, but stuff is nil, which means there was a problem decoding the file (according to the documentation).
(How) Can I get more debugging information on why [NSDictionary dictionaryWithContentsOfFile:] - or any kind of API call - returns nil?
Read the contents as NSData and then use +[NSPropertyListSerialization propertyListWithData:options:format:error:].
As you said: Your file contains a JSON string. First, you have to load the string and then you have to parse it using some JSON library.
E.g.:
NSError *error;
NSString *content = [NSString stringWithContentsOfFile:filepath encoding:NSUTF8StringEncoding error:&error]; //error checking omitted
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSDictionary *json = [parser objectWithString: content];