How to change file extension from .txt to .rtf in ios programmatically? - iphone

How do I change a file extension from .txt to .rtf in iOS programmatically?
In xml parsing I'm getting rtf data with rtf tags.
Is it possible to save data in a rtf file without tags?
If we load that file, this was loading with rtf tags in webview.
If we open and save that file physically, it opens in the WebView.
Is there any way to do this task (File Open & Save) programmatically using iOS.
If we load the rtf file from FTP server, upto tables it was loading(only plain text before tables)

using function of NSString
NSString *string2 = [[NSString alloc] initWithContentsOfFile:#"whateverFile.rtf"];
get the text only from the file and save it in .txt format using the
NSMutableString *mutstr2 = [[NSMutableString alloc] init];
[mutstr2 setString: string2];
[mutstr2 writeToFile:#"whatevertxt.txt" atomically:YES encoding:NSUnicodeStringEncoding error:&error];
you could write the file in .txt. format... i guess that is the best way to do what u want

Related

HTML file creation with image

I created an HTML file programmatically with images sources. The images are stored in the documents directory. The problem is that when I give the image source path as document directory path the images are not shown in Windows.
So, I choose the second option is to give only the image's name instead of whole path. Then it work well on Mac & Windows, but now images are not shown on the device's UIWebView.
Then I selected to open the HTML file in Safari insted of UIWebView, but the safari could not be opened because URL becomes NULL.
My code to open Safari is as follow:
NSLog(#"%#",documentsDirectory);
NSString *urlStr = [NSString stringWithFormat:#"%#/AccReport1.html",documentsDirectory];
NSLog(#"urlStr :: %#",urlStr);
NSURL *url1=[[NSURL alloc]init ];
url1 = [NSURL fileURLWithPath:urlStr];
NSLog(#"url :: %#",url1);
[[UIApplication sharedApplication]openURL:url1];
I want to mail the HTML file that contains images. And these HTML file and images are stored in document directory. I want to show this HTML file with images in windows browser, mac browser and iphone's safari.
How to do this?
One of the simple way to solve your problem is to keep Images and HTML file in same directory and refer those images in HTML files just by name and if you put them in a folder then give the complete path.
The images are not showing because the image path specified within the HTML code is not the documents directory path.
I suggest you might have to construct an html string and then subject the uiwebview to load that string
NSString *htmlString = [NSString stringWithFormat:#"<html><body><img src=\" %#/image.png \"></img></body></html>",documentsDirectoryPath];
[webView loadHTMLString:htmlString baseURL:nil];

Is it possible to open a download ebook file via iBook?

I got a question, which is, what if I can download a ebook file, eg .epub or PDF, then I list this file on a tableview, now I select one of the books.
Can I open via iBook?
Or I need to implement a reader to open the file?
Also got another question , I download a pdf file in my folder
The path I log it out , looks right
Here is the code:
NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];
NSString *ePubSubFolder = [docPath stringByAppendingPathComponent:#"Books"];
NSString *pdfPath = [ePubSubFolder stringByAppendingPathComponent:[NSString stringWithFormat:#"%#",self.pdfFileName]];
CFURLRef pdfURL = CFURLCreateWithFileSystemPath (NULL,(CFStringRef)pdfPath, kCFURLPOSIXPathStyle, FALSE);
NSLog(#"PDF URL: %#", pdfURL);
pdf = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL);
CFRelease(pdfURL);
The log result is
../iPhone%20Simulator/4.3.2/Applications/06BA5929-3531-4AC3-B524-6CC74DC7E2C9/Documents/Books/Repeat%20After%20Me%20User's%20Guide.pdf
Do I did something wrong ?I won't show any thing on my view.
Many thanks for all reply or answer :-)
in IOS when you open a pdf using any application you get an option on the top right corner click on it and you can see options like open in ibooks etc.. I have tried this with many apps.
this is what I do to use some pdf on my ios devices.. load the file into a dropbox folder on the comp open it in the device in dropbox and then move it over to ibooks..

Create documents folder iPhone SDK

I am developing an App that displays several pages of Text when the correct buttons are pressed. It is Static proprietary information. There is a different Text File for each of six buttons.
I am new to the ios SDK. Does creating a project in XCODE automatically create a Documents Folder? Is the "Documents Folder" what Apple is calling the "Sandbox"?
Can I simply write my Text, (that part which will display on the screen, LOTS of Text), drop it into the "Documents Folder", then display it in "scrolling mode" on the iPhone when a certain button is pressed?
I would prefer the Text to be part of the compile, since the information is proprietary, not simply a Text File, if there is a way to store and display large Text Files efficiently.
Yes Ken, the documents directory is there by default when you create an app, and yes, you can certainly write and read text data from it if you want to.
You cannot directly drop data into the Documents folder however, you need to do so programmatically.
Assume that one of your files are 'TextFile1.txt'. You should add this file to your project firstly, and somewhere in the appDelegate, write the following code;
NSString *fileBundlePath = [[NSBundle mainBundle] pathForResource:#"TextFile1" ofType:#"txt"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fileDocumentDirectorySavePath = [documentsDirectory stringByAppendingPathComponent:#"TextFile1.txt"];
NSFileManager *fm = [NSFileManager defaultManager];
if (![fm fileExistsAtPath:fileDocumentDirectorySavePath])
[fm copyItemAtPath:fileBundlePath toPath:fileDocumentDirectorySavePath error:nil];
This will copy the TextFile1.txt into your apps Documents folder from which you can read it anytime you need with the following code;
// You can get the fileDocumentDirectorySavePath same way as in the above code
NSString *stringToDisplay = [[NSString alloc] initWithData:[NSData dataWithContentsOfFile:fileDocumentDirectorySavePath] encoding:NSUTF8StringEncoding];
NSLog(#"String : %#", stringToDisplay);
You can do this for any number of text files you need to work with.
If you don't want to change the text dynamically (i.e., only when you submit an update), just add the files directly to your Xcode project and don't even worry about the sandbox/Documents folder. You can drag the files into the sidebar in Xcode (creating a custom folder for it would be very organized of you) and check the "Copy files to project folder?" when asked. As is stated, they're now copied and part of the compiled app. Then you can query the files and display them in a UITextView, automatically supporting scrolling of text.
Alternatively, you could do what I think is the easier method and include the files directly in your code. In a class file that loads the text, in the .h file (Header), add a UITextView as a property and a variable. In the .m file (Implementation), do yourTextView = [[UITextView alloc] init];, then set yourTextView.text to an NSString containing your text. Sounds confusing, but it will be quicker and easier to update in the end. That is, unless your text is formatted... Anyway, you could also just create a UITextView in your XIB/NIB file and add your text directly.
I'd suggest you do it in code. That will be the easiest to change.
Adding text to your app is one thing - making it secure is more difficult. I had to deal with a similar issue and decided to use unformatted text that I include encrypted in my app and only decrpt the part that is being shown. Really depends how "secret" you want to keep the text. Remember, anyone can read it anyway and copy it right from the screen with a screenshot. Also unencrypted text in apps can be read and extracted quite easily using a HexEditor!
Alternatively you can prepare the text in *.txt (unformatted) or html (formatted as you like) file format and just include it in your app. However, this is the easies way for others to just copy the file.

loading and formatting the rtf file in UIWebView

I am trying to load the contents of a rtf file in the UIWebView. I am successful in loading the contents, but it is unformatted. the fonts are too large and it doesn't have any format. it displays the color of the rtf content, but not the font or size.
So what should i do now. is there any other way to load the rtf and format it? I load the rtf in following way:
NSString *path = [[NSBundle mainBundle] pathForResource:#"Home" ofType:#"rtf"];
NSURL *url=[NSURL fileURLWithPath:path];
NSURLRequest *req=[NSURLRequest requestWithURL:url];
[menuWeb loadRequest:req];
So what should i do now?
Use HTML instead, it's the only way you're going to get the control you want.
http://cutesoft.net/example/editRTF.aspx
This page is a WYSIWYG HTML generator. It generates extremely clean HTML (*), which I can then save and insert in my project. I can then modify this HTML file from within Xcode. All very nice!
(*) provided you use it right... flip between the normal view, HTML view and final view -- don't make it export into the second text box otherwise everything comes out in an impenetrable chunk.

File Formats Supported by UIWebView

What are all the file formats supported by UIWebView?
In my testing, I found that it supports XLS, DOC, PPT, PDF but not XLSX, and DOCX, RTF.
It supports image files like, JPG, PNG, GIF, BMP, not sure about TIFF or
Exactly, what all types are supported is not clear...
The UIWebView documentation also doesn't state it clearly.
Could someone please help?
A Technical note is available on Apple Website about file formats supported by UIWebView:
Since iPhone OS 2.2.1
Excel (.xls)
Keynote (.key.zip)
Numbers (.numbers.zip)
Pages (.pages.zip)
PDF (.pdf)
Powerpoint (.ppt)
Word (.doc)
Since iPhone OS 3.0
Rich Text Format (.rtf)
Rich Text Format Directory (.rtfd.zip)
Keynote '09 (.key)
Numbers '09 (.numbers)
Pages '09 (.pages)
I am seeking a definitive answer on this, too.
While the Tech Note tells us which high-level formats are supported, it doesn't tell us which simple formats, e.g. image types, are supported. I need that information, though, in order to let a web server know which formats it can send me (i.e. via http's "Accept" header).
Update
Uh, actually, here's the docs from Apple on supported image formats by UIWebView: http://developer.apple.com/iphone/library/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/GraphicsandDrawing/GraphicsandDrawing.html#//apple_ref/doc/uid/TP40007072-CH10-SW8
.rtf files are apparently supported but I was unable to get the UIWebView to display them properly. It would format the text correctly (size, colour, font etc) but images just plain didn't render (I tried .gif, .png and .jpg to no avail). chances are if you are going to the trouble of using .rtf, you are probably hoping to display images in the UIWebView, since the main benefit of rtf is that you can embed images into the file. This was tried on an actual iPad 1 (4.3) and on a simulated iPhone (4.3).
The code done to display the rtf in a UIWebView required the rtf to be written to a file with the rtf file extension. It refused to load the file if you use no file extension or an incorrect one so make sure you write it as .rtf.
Here is an Objective C function to take an input string (which should contain the rtf you wish to display) and get the UIWebView to load it into view...
-(void) loadRtf : (UIWebView*) webView : (std::string) rtfFile
{
// This function will write the rtf to a file in your apps bundle location on the iDevice and use
// the UIWebView to load it...
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath = ([path count] > 0) ? [path objectAtIndex:0] : nil;
NSString *fullPath = [basePath stringByAppendingPathComponent:#"rtfData.rtf"];
std::string fp = [fullPath cStringUsingEncoding:NSASCIIStringEncoding];
std::ofstream fs;
fs.open(fp.c_str(), std::ios_base::binary);
if( !fs.is_open() )
return;
fs << rtfFile;
fs.close();
NSURL *url = [NSURL fileURLWithPath:fullPath];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
}