I try to create HTML file programaticatically. But generated file shown me as blank file. I don’t know the issue.
Here is my code:
- (IBAction) createFileAction
{
NSLog(@"************* createFileAction *************");
NSMutableString *htmlString = [[NSMutableString alloc] init];
NSLog(@"%@",htmlString);
[htmlString appendString:@"<html><head><title></title><style type='text/css'>.style1{height: 27px;}#Text1{height: 19px;width: 210px;}.style2{width: 255px;}.style3{height: 27px;width: 255px;}#petValue{width: 206px;}#nameValue{width: 206px;}#dateValue{width: 209px;}"];
[htmlString appendString:@"#Text2{width: 215px;}#Text3{width: 210px;height: 22px;}#Text4{width: 209px;}.style8{width: 13px;}.style9{ width: 263px;}.style10{height: 27px;width: 263px;}"];
[htmlString appendString:@".style11{width: 418px;}.style12{width: 199px;}.style13{height: 27px; width: 199px;}.style14{height: 24px;}.style15{width: 410px;}</style> </head>"];
[htmlString appendString:@"<body>"];
NSString *originalString = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.makepartsfast.com/2012/09/4337/more-3d-printing-in-metals-ex-one-introduces-the-m-flex-3d-printing-system/"] encoding:NSUTF8StringEncoding error:nil];
NSScanner *scanner = [NSScanner scannerWithString:originalString];
NSString *extractedString = nil;
[scanner scanUpToString:@"<div class=\"postarea\">" intoString:nil];
[scanner scanString:@"<div class=\"postarea\">" intoString:nil];
[scanner scanUpToString:@"<div style=\"clear:both;\">" intoString:&extractedString];
if (extractedString)
{
// string was extracted
NSLog(@"%@", extractedString);
}
NSLog(@"BEFORE= %@",htmlString);
[htmlString appendString:extractedString];
NSLog(@"AFTER= %@",htmlString);
[htmlString appendString:@"</body></html>"];
NSLog(@"FINAL=%@",htmlString);
// check to see if file already exists in documents directory
if([self.fileMgr fileExistsAtPath:self.file])
{
NSLog(@"File already exists");
}
else
{
NSLog(@"file does not exist");
NSLog(@"Try creating the file");
// file does not already exist so create it
[self.fileMgr createFileAtPath:file contents:nil attributes:nil];
NSLog(@"%@",htmlString);
[htmlString writeToFile:self.file atomically:NO encoding:NSStringEncodingConversionAllowLossy error:nil];
//Test again whether this file exists now that we have tried creating it
if([self.fileMgr fileExistsAtPath:self.file])
{
NSLog(@"File exists now");
}
else
{
NSLog(@"File still doesn't exist");
}
}
}
In this method, I get only select portion from the URL in extractedString in html format & append it to htmlstring. when i try to append different string then it works fine.
It’s because you passed
NSStringEncodingConversionAllowLossyas theencodingargument. You should be passing one of the string encoding arguments. What’s happening is thatNSStringEncodingConversionAllowLossyis equal to 1. ButwriteToFile:atomically:encoding:error:is expecting a string encoding constant. The string encoding constant equal to 1 isNSASCIIStringEncoding. But your string can’t be converted to ASCII, so you get an empty file. Change that line to use UTF8 and you get the expected non-empty file:You had trouble with this at least partly because you are not checking return types or error conditions. The method call returns
BOOLand will create anNSErrorif anything goes wrong. This would have helped you figure out what was wrong, if you were listening. If you had done this:You would have seen an error message reading something like:
That might not be obvious at first but the fact that it specifically mentions trying to use ASCII would have been a huge clue to the source of the problem.