I'm using those line of codes to create an attributed string from a simple HTML string:
NSDictionary *importParams = #{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,NSCharacterEncodingDocumentAttribute: #(NSUTF8StringEncoding) };
NSError *error = nil;
NSData *stringData = [HTML dataUsingEncoding:NSUTF8StringEncoding] ;
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData:stringData options:importParams documentAttributes:NULL error:&error];
The issue is that is really slow (about seconds on simulator)even for few characters.
I don not understand the reason behind that and I don't want to use third party libs with custom HTML parser.
Is there any other way to create attributed string from HTML text faster than this?
Related
My Problem is that I am getting html content form backEnd so when I had convert my NSData by using this code:
dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
I have got content like this:
<div class=\"dropzone-container\"
Where on the place of " it will automatically convert it into \" and due that when I load that data on UIWebView it would not load it respective css so I need to it to be converted like this:
<div class="dropzone-container"
Please help in this Thanks in advance........ :(
Try this. It will convert HTML to NSString.
NSString *responseInHTML = #"<html><body> Some html string </body></html>";
NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[responseInHTML dataUsingEncoding:NSUnicodeStringEncoding] options:#{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
DLOG(#"String = %#",attrStr.string);
You can achieve your html by removing \ from string.
Example:
NSString *myHTML = [oldHTML stringByReplacingOccurrencesOfString:#"\" withString:#""];
I am trying to initialize an attributed string with HTML file contents, as following:
NSMutableAttributedString *attrStr = [[[NSAttributedString alloc] initWithFileURL:[NSURL fileURLWithPath:chapterPath]
options:#{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType}
documentAttributes:nil error:nil] mutableCopy];
and it does work with iOS7.1 and iOS8.1 and up, but something goes wrong with iOS8.0:
libc++abi.dylib: terminating with uncaught exception of type std::out_of_range: vector
Is this initialization method broken in iOS8.0? Any workaround?
BTW, I have tried to parse an RTF file and, with iOS8.0, it fails to collect any font information.
Did you try this?.
NSError *error = nil;
NSData *data = [NSData dataWithContentsOfFile:chapterPath options:NSDataReadingUncached error:&error];
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData: data options:#{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:&attributes error:nil];
Init with HTML for NSAttributedString is on main thread only. If you doing that in background be ready to crash.
I am getting data from the Yummly API and I would like to use it as though it were serialized JSON data. However, it is currently a string, and I cannot figure out how to turn it to data correctly. The code is as following:
NSString *searchParameters = #"basil"; //should be from text box
//NSError *error1 = nil;
NSString *searchURLName = [#"http://api.yummly.com/v1/api/recipes?_app_id=myAPIId&_app_key=myAPIkey&" stringByAppendingString:searchParameters];
NSURL *searchURL = [NSURL URLWithString:searchURLName];
NSString *searchResults = [NSString stringWithContentsOfURL:searchURL encoding:NSUTF8StringEncoding error:nil];
// Here, the search results are formatted just like a normal JSON file,
// For example:
/* [
"totalMatchCount":777306,
"facetCounts":{}
]
*/
// however it is a string, so I tried to convert it to data
NSData *URLData = [searchResults dataUsingEncoding:NSUTF8StringEncoding];
URLData = [URLData subdataWithRange:NSMakeRange(0, [URLData length] - 1)];
_searchArray = [NSJSONSerialization JSONObjectWithData:URLData options:NSJSONReadingMutableContainers error:nil];
Somewhere over the last four lines, it didn't do what it was supposed to and there is no data in the data object. Any advice or quick hints in the right direction are much appreciated! Thank you1
Look at the error being returned from the NSJSONSerialization object like
NSError *error;
_searchArray = [NSJSONSerialization JSONObjectWithData:URLData options:NSJSONReadingMutableContainers error:&error];
NSLog(#"%#", error);
This might give you a hint of what's wrong. This should work though.
And why exactly are you doing URLData = [URLData subdataWithRange:NSMakeRange(0, [URLData length] - 1)];? You don't need to copy the data, if that's why you're doing that.
Plus, it seems like you're assuming to get an array as the top level object (judging by
/* [
"totalMatchCount":777306,
"facetCounts":{}
]
*/
but this is a dictionary. Basically you probably want a dictionary, not array. This it should be
/* {
"totalMatchCount":777306,
"facetCounts":{}
}
*/
But the error getting returned will tell you that.
It looks like you're over-complicating things a bit. You do not need to bring in this data as an NSString at all. Instead, just bring it in as NSData and hand that to the parser.
Try:
NSString *searchParameters = #"basil"; //should be from text box
NSString *searchURLName = [#"http://api.yummly.com/v1/api/recipes?_app_id=myAPIId&_app_key=myAPIkey&" stringByAppendingString:searchParameters];
NSURL *searchURL = [NSURL URLWithString:searchURLName];
NSData *URLData = [NSData dataWithContentsOfURL:searchURL];
_searchArray = [NSJSONSerialization JSONObjectWithData:URLData options:NSJSONReadingMutableContainers error:nil];
Note that you'll want to verify that the parsed JSON object is indeed an array as expected, and is not/does not contain [NSNull null].
I have a NSString that contains a HTML page. I would to extract a particular tag by id and a particular attribute (of this tag) by name.
I used regular expression as this example:
NSError *error = nil;
NSString *authParameter = nil;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:#"<form id=\"theIdOfForm\"*.*method=\"post\">" options:NSRegularExpressionCaseInsensitive error:&error];
if (error == nil) {
NSTextCheckingResult *match = [regex firstMatchInString:aHtmlFirstResponse options:0 range:NSMakeRange(0, [aHtmlFirstResponse length])];
if (match) {
NSString *formHtmlTag = [aHtmlFirstResponse substringWithRange:[match rangeAtIndex:0]];
}
but, how can I extract the attribute by name from this tag?
What is an elegant and efficient way to obtains this?
I would like to avoid importing some HTML parser library.
You could use
https://github.com/zootreeves/Objective-C-HMTL-Parser
I have used this for one of my project.
I've read many Q/As on this problem but couldn't find an answer that fits my situation.
I retrieve a JSON response from a REST service I've created in PHP. This is my code:
NSURLResponse *response = nil;
NSError *theError1 = nil;
NSError *theError2 = nil;
NSURL *webServiceUrl = [NSURL URLWithString:#"http://..."];
NSURLRequest *request = [NSURLRequest requestWithURL:webServiceUrl cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:30];
NSData *theData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&theError1];
NSString *dataString = [[NSString alloc] initWithData:theData encoding:NSUTF8StringEncoding];
NSLog(#"%#", dataString);
id json = [NSJSONSerialization JSONObjectWithData:theData options:NSJSONReadingAllowFragments | NSJSONReadingMutableContainers error:&theError2];
if (theError2 != nil)
NSLog(#"%#", theError2);
When I invoke the REST call in the browser, I see the following response, which seems identical to what XCode logs:
{
"Name": "REST Service",
"Product": "REST Test",
"Version": "1.0.0.0",
"Copyright": "2013 Test Company"
}
When I execute above code, however, the following error is created and logged:
Error Domain=NSCocoaErrorDomain Code=3840 "The data couldn’t be read because it has been corrupted." (Invalid value around character 3.) UserInfo=0x100547430 {NSDebugDescription=Invalid value around character 3.}
What am I doing wrong?
OK, as always, checking the actual data instead of the string representation pays - thanks #Bavarious.
It turns out that the PHP script(s) in charge of creating the JSON were all "UTF8 with BOM", so PHP returned a BOM for every script involved.
Once I changed all the PHP files to "UTF8 without BOM" everything seems to be fine - need to test this on the MAC though.
Sorry to interrupt, keep up the good work.
(#Bavarious: If you'd like to write an answer, I'd be happy to upvote and accept it, as you pointed me to the solution).
Was able to parse the JSON now as expected. Making a mental note to always double-check the text file encoding.
NSURL *theURL = [NSURL URLWithString:#"http://yourdataurl"];
NSMutableURLRequest *storeRequest = [NSMutableURLRequest requestWithURL:theURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:storeRequest queue:queue
completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if (!connectionError) {
NSError *error;
NSString *dataStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSData *theData = [dataStr dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *jsonResponse = [NSJSONSerialization JSONObjectWithData:theData options:0 error:&error];
if (!jsonResponse || error)
{
NSLog(#"Error");
}
else
{
// Everything is ok..
}
}
}];