Reputation:
I am currently using AFNetworking to consume a JSON web service from a Wordpress API.
This is the format that I am using:
[
{
id: 4,
title: "post1",
permalink: “http://www.example.com/test-post”,
content: “blah blah blah blah blah <iframe src="//player.vimeo.com/video/1010101?title=0&byline=0&portrait=0&color=ff5546" height="638" width="850" allowfullscreen="" frameborder="0"></iframe> more text blah blah <a href="http://example.com/wp-content/uploads/2014/01/testpic.png"><img class="aligncenter size-full wp-image-1180" alt="testpic" src="http://example.co/wp-content/uploads/2014/01/testpic.png" width="850" height="611" /></a>",
excerpt: " blah blah blah",
date: "2014-02-24 23:32:19",
author: "admin",
categories:
[
"Uncategorized"
],
tags: [ ]
},
{
id: 1,
title: "Hello world!",
permalink: “http://www.example.com/test-post”,
content: "Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!",
excerpt: "Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!",
date: "2014-02-24 22:48:12",
author: "admin",
categories:
[
"Uncategorized"
],
tags: [ ]
}
]
And I am consuming it as follows:
-(void)makeWordpressRequest
{
NSURL *URL = [NSURL URLWithString:@"http://example.com/feed/json"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]
initWithRequest:request];
operation.responseSerializer = [AFJSONResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
self.wordpress = responseObject;
[self.tableView reloadData];
NSLog(@"%@", responseObject);
} failure:nil];
[operation start];
}
Now here is my question.. I can successfully parse and display "content" string of the JSON, but I haven't figured out how to remove the "iframe" of the video and "a href" from that string to display it.
Is there anyway to embed them directly? Or could I split them from the JSON string programmatically and then embed them another way? I am currently using UITextView to display @"content".
Edit:
Not only remove the HTML but also a way to embed it correctly if possible..
Thank you for your help
Upvotes: 0
Views: 442
Reputation: 993
//Download and #import "RegexKitLite.h" myregex is reference of RegexKitLite.
NSString *HTMLTags = @"<[^>]*>"; //regex to remove any html tag
NSString *htmlString = @"<html>bla bla bla bla bla bla bla bla</html>";
NSString *stringWithoutHTML = [hstmString stringByReplacingOccurrencesOfRegex:myregex withString:@""];
Hope it helps you...
Upvotes: -1
Reputation: 66252
Your API should not be giving you XML/HTML inside of JSON.*
Ideally, you should get the API to resolve this, because parsing XML/HTML inside of JSON opens up new possibilities for bugs. For example, there could be character encoding issues, escaping issues, etc. This is bad API design. It looks like you're using Wordpress, so you may want to post a separate question here about how to modify the Wordpress JSON API to get the URLs you need in your app.
If you really can't modify the API, you'll have to take the string out of the JSON, and then run it through NSScanner or NSXMLParser to try to pick out the part you want. I would start with NSScanner since it's simpler and I'm guessing your API won't guarantee you'll get valid XML. Here's some sample code to get your started.
Finally, slightly off-topic: you may want to use the AFNetworking class AFRequestOperationManager instead of building operations and starting them immediately. It can make your code simpler and more efficient. Not urgent right now, just wanted to encourage you to look at it.
* (Probably; there are exceptions to this rule. Perhaps it'd be acceptable if you're supposed to display the content in a UIWebView.)
Upvotes: 2