Display the rendered feed in a view which is in NSData - iphone

The below is the code I used to get a data of URL from a facebook page. I need to display those datas in a view. How to do it?
NSURL *URL = [NSURL URLWithString:#"https://www.facebook.com/104958162837/posts/10151442797857838"];
NSURLRequest* request = [NSURLRequest requestWithURL:URL];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse * response,
NSData * data,
NSError * error) {
if (!error){
NSLog(#"%#",data);
// I want to render this data in a view.
}
}];
I can get the proper values in the in NSData. I want to display those datas in a webview or uilabel or something to display those datas.

See this code.
id dataObject = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
You can get json string to NSObject(like NSDictionary, NSArray)

You need to convert NSData into NSString and then use that NSString for display.
NSURL *URL = [NSURL URLWithString:#"https://www.facebook.com/xxxxxx/posts/xxxxxxxxxx"];
NSURLRequest* request = [NSURLRequest requestWithURL:URL];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse * response,
NSData * data,
NSError * error) {
if (!error){
NSLog(#"%#",data);
// I want to render this data in a view.
NSString *myString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
//[lbl setText:myString];
/** EDIT : As you are getting HTML Response, you can display that in WebView like this **/
[webview loadHTMLString:myString baseURL:nil];
}
}];

Related

How do I fetch Data from a URL that contain an Image in iOS?

I need to fetch JSON data from a URL in iOS6. How do I get these data into iOS? What if there is a URL to an image inside the data, how do I fetch these image too?
Parse the json and Put the url into an Array or NSString
UIImageView *image1;
NSString *responseString;//it will contain the url
image1.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:responseString]]];
/**
HTTP CONSTANTS
**/
#define TIMEOUT_INTERVAL 60
#define CONTENT_TYPE #"Content-Type"
#define URL_ENCODED #"application/x-www-form-urlencoded"
#define GET #"GET"
#define POST #"POST"
-(NSMutableURLRequest*)getNSMutableURLRequestUsingPOSTMethodWithUrl:(NSString *)url postData:(NSString*)_postData
{
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:TIMEOUT_INTERVAL];
[req setHTTPMethod:POST];
[req addValue:URL_ENCODED forHTTPHeaderField:CONTENT_TYPE];
[req setHTTPBody: [_postData dataUsingEncoding:NSUTF8StringEncoding]];
return req;
}
-(NSMutableURLRequest*)getNSMutableURLRequestUsingGetMethodWithUrl:(NSString*)url
{
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:TIMEOUT_INTERVAL];
[req setHTTPMethod:GET];
return req;
}
NSString *_postData = {<Your postData>}
NSMutableURLRequest *req = [self getNSMutableURLRequestUsingPOSTMethodWithUrl:LOGIN_URL postData:_postData];
[NSURLConnection sendAsynchronousRequest:req queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
if (error)// it means server error
{
//error while connecting server
}
else
{
NSError *errorInJsonParsing;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&errorInJsonParsing];
if(errorInJsonParsing) //error parsing in json
{
//error in json parsing
}
else
{
#try
{
NSLog(#"json==%#",json);
// json is basically in key value format which is NSDictionary
// if you get the url of image inside this json, then again hit the url to get the image ( u can get the image in NSData which is declared above )
}
#catch (NSException *exception)
{
//exception
}
}
}
}];

json return to the format

Api Address:
http://suggest.taobao.com/sug?area=etao&code=utf-8&callback=KISSY.Suggest.callback&q=iphone
return:
KISSY.Suggest.callback({"result": [["iphone4s", "9809"], ["iphone5", "13312"], ["iphone4 手机", "69494400"], ["iphone5 港行", "14267"], ["iphone5三网", "2271160"], ["iphone4手机壳", "6199679"], ["iphone 5手机壳", "2527284"], ["iphone 5 保护壳", "5727586"], ["iphone 4贴膜", "147271"], ["iphone5壳", "2628540"]]})
NSURL * url = [NSURL URLWithString:#"http://suggest.taobao.com/sug?area=etao&code=utf-8&callback=KISSY.Suggest.callback&q=iphone"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
NSHTTPURLResponse* urlResponse = nil;
NSError * error = nil;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
NSData *date = [NSData alloc]init
SBJsonParser *jsonParser = [[SBJsonParser alloc] init];
// NSMutableArray *array=[NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves error:&error];
NSMutableArray *array = [jsonParser objectWithData:responseData];
NSLog(#"%#",array);
this array is null. i dont know the reason.
as i refer you request URL ,it has callback in it, if you keep it, it will not return you json as response, so remove "&callback=KISSY.Suggest.callback" from your URL
// Make sure you have include SBJSON files in your Project, as well you have imported header in your View Controller
#import "JSON.h"
// your request URL
NSURL * url = [NSURL URLWithString:#"http://suggest.taobao.com/sug?area=etao&code=utf-8&q=iphone"];
// URL Request
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
NSHTTPURLResponse* urlResponse = nil;
NSError * error = nil;
// initiate Request to get Data
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
// Encode your Response
NSString *content = [[NSString alloc] initWithBytes:[responseData bytes] length:[responseData length] encoding:NSUTF8StringEncoding];
// Now read a Dictionary from it using SBJSON Parser
NSDictionary *responseDict = [content JSONValue];
NSLog(#"Response [%#]",responseDict);
I'm not familiar with the SBJsonParser, but the format of the returned string looks like JSONP, not JSON. I would imagine simply cleaning out the wrapper call would get you what you are after.
Also, note that the 'root' of your response is a dictionary, not an array.
{"result": [[...
means that the code might should look like this:
NSDictionary *response = //... decode
NSArray *results = [response objectForKey:#"result"];
Edited
You just need to use http://suggest.taobao.com/sug?area=etao&code=utf-8&q=iphone instead of http://suggest.taobao.com/sug?area=etao&code=utf-8&callback=KISSY.Suggest.callback&q=iphone you own code will work..

-JSONValue failed. Error trace is: in Google currency converter in iPhone

I need live currency rates. I am using a Google API available in the URL
http:://www.google.com/ig/calculator?hl=en&q=1USD=?INR
Whenever am hitting that URL using json parsing, then response data getting nil. I am not getting what is the exact error in that code
#define openexchangeURl #"http://www.google.com/ig/calculator?hl=en&q=1USD=?INR"
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:openexchangeURl]];
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
values =[responseString JSONValue];
This will get you live rates:
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:#"http://rate-exchange.appspot.com/currency?from=USD&to=INR&q=1"]];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
NSDictionary *parsedDict = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
CGFloat value = [parsedDict[#"rate"] floatValue];
NSLog(#"Value: %f", value);
}];
The JSON response from that api looks like this:
{
to: "INR",
rate: 54.8245614,
from: "USD",
v: 54.8245614
}
Your original request didn't have an NSURLConnection and the response was not valid JSON (did not have double-quoted values for each item in the hash).

Sending Data from UITextField to PHP script via NSURLConnection or JSon

Hi I would like to know how I can go about sending data from 4 UITextField's to a PHP script on a website using NSURLConnection or JSon. Whatever is easier, how can I do this exactly?
Thanks in advance!
Easiest way (synchronous)
NSString strForTextField1 = textField1.text;
NSString strForTextField2 = textField1.text;
NSString strForTextField3 = textField1.text;
NSString strForTextField4 = textField1.text;
//Build the request
NSString *stringOfRequest = [NSString stringWithFormat:#"www.yourserver.com?var1=%#&var2=%#&var3=%#&var4=%#", strForTextField1, strForTextField2, strForTextField3, strForTextField4];
NSURL *url = [NSURL URLWithString:stringOfRequest];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLResponse *response;
NSError *error;
//send it synchronous
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
if(!error)
{
//log response
NSLog(#"Response from server = %#", responseString);
}

iphone textview contents to url?

How can i have a uitextview value be taken and send to a predefined url using obj-c only without any html /php.
Take a look at NSURL. Use the text value of the UITextView as a parameter.
e.g.
NSURL *webURL = [NSURL URLWithString:MY_TEXTVIEW.text];
NSURLRequest *req = [NSURLRequest requestWithURL:webURL];
NSString *methodString = #"method";
NSString *parameterString = [NSString stringWithFormat:#"value=%#", textField.text];
NSString *apiRoot = #"http://www.domain.com";
NSURL *apiUrl = [NSURL URLWithString:[NSString stringWithFormat:#"%#/%#?%#", apiRoot,methodString,parameterString]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:apiUrl];
NSURLResponse *response = nil;
NSError *error = nil;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];