在NSData中的视图中显示呈现的Feed

时间:2013-03-05 05:06:14

标签: iphone ios objective-c facebook nsdata

以下是我用来从facebook页面获取URL数据的代码。我需要在视图中显示这些数据。怎么做?

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.

                           }

                       }];

我可以在NSData中获取正确的值。我想在webview或uilabel中显示这些数据,或显示那些数据。

2 个答案:

答案 0 :(得分:2)

请参阅此代码。

id dataObject = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];

您可以将json字符串添加到NSObject(如NSDictionary,NSArray)

答案 1 :(得分:1)

您需要将NSData转换为NSString,然后使用NSString进行展示。

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];



                           }

                       }];