Load NSAttributedString from File - iphone

Is there a way to have some sort of rich text in a file, and load the file into an NSAttributedString? I looked into loading a .rtf file into an NSAttributed String:
NSString *filePathFooter = [[NSBundle mainBundle] pathForResource:kFooterFileName ofType:#"rtf"];
NSError *error = nil;
NSAttributedString *string = [[NSAttributedString alloc] initWithFileURL:[NSURL URLWithString:filePathFooter]
options:nil
documentAttributes:NULL
error:&error];
But this leaves me with:
Error Domain=NSCocoaErrorDomain Code=258 "The operation couldn’t be completed. (Cocoa error 258.)"
Is there a way to do this?

Use URLForResource instead, and pass the resulting URL like so:
NSURL *url = [[NSBundle mainBundle] URLForResource:kFooterFileName withExtension:#"rtf"];
NSError *error = nil;
NSAttributedString *string = [[NSAttributedString alloc] initWithFileURL:url
options:nil
documentAttributes:NULL
error:&error];

NSCocoaErrorDomain error code 258 is NSFileReadInvalidFileNameError, so it looks like it can't find the file you're passing.

Related

Read string from a file return nil

I try to read string from a file with :
NSString* path = [[NSBundle mainBundle] pathForResource:#"dirty" ofType:#"txt"];
NSString* content = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&err];
And the string i get is nil. in the error i get :
Error Domain=NSCocoaErrorDomain Code=261 "The operation couldn’t be completed. (Cocoa error 261.)" UserInfo=0x21063410 {NSFilePath=/var/mobile/Applications/78889F89-B83C-480C-A246-999152EE757C/Myapp.app/dirty.txt, NSStringEncoding=4}
Any idea what is the problem?
As mentioned, Cocoa error 261 is NSFileReadInapplicableStringEncodingError which means the encoding of file is different than what you are passing in the API.
For Hebrew language encoding, try this answer
NSString* content = [[NSString alloc] initWithContentsOfFile:path encoding:CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingWindowsHebrew) error:&err];
Hope that helps!
The error seems to be the Encoding Issue ..
If you don't know the encoding of your file, you could try this method:
- (id)initWithContentsOfFile:(NSString *)path usedEncoding:(NSStringEncoding *)enc error:(NSError **)error
Refer HERE
And
Try Something like this:
NSError *error = nil;
NSStringEncoding encoding;
NSString *my_string = [[NSString alloc] initWithContentsOfURL:url
usedEncoding:&encoding
error:&error];
Try this.
NSString* content = [[NSString alloc] initWithContentsOfFile:path encoding:nil error:&err];
or change the type of "encoding" hope it will solve you problem.
Try This
NSString *pathtest = [[NSBundle mainBundle] pathForResource:#"dirty" ofType:#"txt"];
NSString* content = [[NSString alloc] initWithContentsOfFile:pathtest encoding:NSASCIIStringEncoding error:&error];
NSLog(#"Test: %#",content);

Get Print preview and save it in PDF file

I want to generate a print preview of a web page and save it to an PDF file.
i tried stringByEvaluatingJavaScriptFromString:#"document.body.scrollHeight";
and
stringByEvaluatingJavaScriptFromString:#"document.body.scrollWidth";
functions for finding the height and width but problem with this is the java script program stops after 10 second..
I want to use default print option that is used for airPrint, but i don't want that to print.
I just want to save the print preview in PDF.
Please any one help me..
NSString *webServiceUrlString =#"https://www.google.com"; //Google.com is for example use your url
NSData *pdfData = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:webServiceUrlString]];
NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:#"Documents"]];
NSFileManager *fm = [NSFileManager defaultManager];
NSError *error = nil;
for (NSString *file in [fm contentsOfDirectoryAtPath:resourceDocPath error:&error]) {
BOOL success = [fm removeItemAtPath:[NSString stringWithFormat:#"%#/%#", resourceDocPath, file] error:&error];
if (!success || error) {
}
}
NSString* fileName = #"Your File Name.pdf";
NSString *filePath = [resourceDocPath stringByAppendingPathComponent:fileName];
[pdfData writeToFile:filePath atomically:YES];
You can use UIGraphicsBeginPDFContextToFile to create a PDF file from your webpage content.
Check out the following tutorial.MAKING A PDF FROM A UIWEBVIEW

initWithContentsOfFile Deprecated

I was wondering what code I could use instead of initWithContentsOfFile: as I've been searching for something that isn't deprecated but cannot find anything. I'm trying to display a local HTML file within a webview, below is the code:
- (void)viewDidLoad {
[super viewDidLoad];
NSString *htmlPath = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"];
NSURL *bundleUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
NSString *html = [[NSString alloc] initWithContentsOfFile:htmlPath];
[self.myWebView loadHTMLString:html baseURL:bundleUrl];
}
And I'm getting a warning saying that initWithContentsOfFile: is deprecated and would like to know how to update it.
Try this:
NSError *error = nil;
NSString *string = [[NSString alloc] initWithContentsOfFile:#"/path/to/file.txt" encoding:NSUTF8StringEncoding error:&error];
Use the newer initializer:
NSString's [initWithContentsOfFile: encoding: error:] method, where you can get a useful error passed back to you if there are any problems. I've linked the documentation for you. If you don't know the encoding of the file, you can also use another method that has a usedEncoding parameter.

NSURL, creating pdfs

I'm trying to create some pdfs from pdfs added to my iphone as an exercise for myself in quartz and just getting better with the language. So I have this so far to get the multiple pdfs on my phone:
NSError *error;
NSString *bundleRoot = [[NSBundle mainBundle] bundlePath];
NSArray *dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:bundleRoot error:&error];
NSArray *onlyPDFs = [dirContents filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"self ENDSWITH '.pdf'"]];
How can I convert this to a NSURL object, or an array of NSURL objects? I've tried some different things like URLForResource, initWithString, but I don't think those are correct. Thanks.
You could do something like this to generate an NSURL for each PDF in onlyPDFs:
for (NSString *p in onlyPDFs) {
NSString *name = [p stringByDeletingPathExtension];
NSURL *url = [[NSBundle mainBundle] URLForResource:name withExtension:#"pdf"];
}

Unable to retrieve data in string

self.pdfPath = [[NSBundle mainBundle] pathForResource:#"iPhone_SDK_License" ofType:#"pdf"];
NSData *htmlData = [NSData dataWithContentsOfFile:pdfPath];
NSString *htmlstr = [[NSString alloc] initWithData:htmlData encoding:NSUTF8StringEncoding];
It is showing nil in htmlstr, so need some assistance on it.
A pdf is not an html file so I assume that initWithData:encoding: just cannot understand the data and so fails.
Use stringWithContentsOfURL:encoding:error: to see the details of the error.