Open the files in my application - iphone

I am having an application which consists of many different file formats like ppt,pdf,txt,png etc, I want to open it whenever the user taps the particular file. How this can be done?Please help me
Can I use WebView for all the files ?

Have a look at the quick look framework with UIDocumentInteractionController class it will have you to open different format of file in its view controller..
QuickLook is a framework that provides quick previewing of a range of document types – supported documents include iWork documents, Microsoft Office, Rich Text Format, PDF, images, text files and comma-separated (csv) files.
Sample Application

UIWebview allow you to display office documents :
You can open them by loading the file data, example with RTF file :
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"MyFile" ofType:#"txt"];
NSData *myData = [NSData dataWithContentsOfFile:filePath];
[self.webView loadData:myData MIMEType:#"application/rtf" textEncodingName:#"latin1" baseURL:nil];
Or you can load them by creating a request with local path :
NSString *pdfPath = [[NSBundle mainBundle] pathForResource:#"fileName" ofType:#"pdf"];
NSURL *url = [NSURL URLWithStringdfPath];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[webView loadRequest:urlRequest];
Note that for Office documents with complex layout you will have a very (very) poor rendering.
The only solution is to call a separate application that handles this kind of documents (Pages, Quick office, etc)
Have a look at UIDocumentInteractionController to open your documents in another app
Hope this helps,
Vincent

Related

Using Local Resource and Relative Link in the same UIWebView

I have a problem with a WebView. I'm loading an Html String into a web view. As base URL I need to use my site URL (because there are some relative link inside the html and I cannot modify it).
The problem is that I also need to use some images and css that are stored inside the Application bundle.
So I'm creating a reference to the local resource using this code (the result string is used as src in html file):
NSString *cssUrl = [NSString stringWithString:#"file:"];
cssUrl = [cssUrl stringByAppendingString:[[NSBundle mainBundle] resourcePath]];
cssUrl = [cssUrl stringByAppendingString:#"/story.css"];
Then I've also added:
cssUrl = [cssUrl stringByReplacingOccurrencesOfString:#"/" withString:#"//"];
cssUrl = [cssUrl stringByReplacingOccurrencesOfString:#" " withString:#"%20"];
Any idea how to solve the problem and how to create a reference to a file that can be used inside a web view?
Thanks
Francesco
First, import a directory, e.g. one called web, into your project where all the files reside. Make sure you select "Create Group References For Any Folders" when Xcode asks you.
Now you can load your initial string like this after putting it into a file inside web, say index.html:
NSString* path = [[NSBundle mainBundle] pathForResource:
#"index" ofType:#"html" inDirectory:#"web"];
if (path)
[self.webView loadRequest:
[NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]]];
Now all relative source URLs in your string will work as expected, including <img>, <a> etc.
How about this?
NSString *resourcePath = [[NSBundle mainBundle] pathForResource:#"story" ofType:#"css"];
NSURL *url = [NSURL fileURLWithPath:resourcePath];
NSString *theMagicalURLString = [url absoluteString];
It seems that what I was trying to do it's not possible. To use the bundled resource you have to set the base URL of your web view to the resource path.
Thanks to everyone for your help
Francesco

how to access the content within the zip file in iphone

I have a zip file. In that zip file I have some ppt presentation.
I want to show that ppt presentation in my UIWebView.
I cannot extract and show ppt files there directly .
How do I access the ppt inside the zip in objective c?
This isnt easy. You might want to consider doing this another way. For example you could put the file unzipped on a server and open the link in a webview.
However, if you really want to do it this way there is 2 step.
1. Unzip the file.
You want end up with NSData. Read though some of the links suggested in the comments. You will need to use a 3rd party library to achieve this.
2. Load the data in to a UIWebView.
Write the NSData to the temp directory then point the UIWebView at it.
NSString *path = // .. Get a location in the NSTemporaryDirectory
if ([pptData writeToFile:path atomically:YES])
{
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webview loadRequest:request];
}

To test working of xml in iphone?

HI can i place both xml files and images that the xml file transports in xcode to test the working since i don't have the internet ? If it can be done , what should be the path name for the xml to be coded in program and images to be typed in xml.?
NSString *stringToXML = [[NSBundle mainBundle] pathForResource:#"theXMLDoc" ofType:#"xml"];
NSURL *XMLURL = [NSURL fileURLWithPath:stringToXML];
Using that you can get XML documents from the resource folder and use them.
Just substitute your url in your code for the variable XMLURL and you should be fine. Its the same for images, you just need to make an NSURL out of the path resource.

Displaying localized text unavailable at compile time on iPhone

For use in a questionnaire application, a web service will provide a list of questions in one of several languages, chosen by the user at runtime. The questions will be downloaded from the web service in the chosen language and displayed to the user.
The problem: I have no idea how to do this.
As a sample, I tried loading in UTF-8 text files (e.g. arabic.txt) in the resources containing samples of text in said languages. The files open and render properly in TextMate and TextEdit, but are illegible in Xcode. They are successfully read in, but their contents will not display.
Example:
I create a UITextView and, at initialization:
...
NSString *path = [[NSBundle mainBundle] pathForResource:#"arabic" ofType:#"txt"];
NSString *arabicString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringENcoding error:&error];
myTextView.text = arabicString;
...
The NSError returns NULL, so there's no error reading in the text file, but the contents of the UITextView is not set. Nothing happens. No error (compilation or runtime), nothing.
Any ideas for a different approach? Or a way to make this work?
Thanks so much.
What you are currently doing sounds reasonable, but for a different approach that I have used (that worked for me), try using a UIWebView. Something along the lines of:
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/arabic.html",path]];
NSData *data = [NSData dataWithContentsOfURL:url];
[self.helpWebView loadData:data MIMEType:#"text/html" textEncodingName:#"utf-8" baseURL:baseURL];
(Be interesting to see if using arabic.txt and mime-type of text/plain loads any differently in the web view.)

How to open xls file in iphone application?

I want to devlop an application in iphone where user can open a xls file ,Can do some editing on data already present and finally save it.
How can i do it any idea?
Yes, Hardik it is very simple if you want to open a local xls file.
Add a local xls file in to your project
Drag and drop an UIWebView control on the view.
Connect Files owner and UIWebView Object
Add this:
- (void)viewDidLoad {
[super viewDidLoad];
//Get the path where your local xls file is located
NSString *FilePath = [[NSBundle mainBundle] pathForResource:#"sample" ofType:#"xls"];
//Now To use a local resource file in a web view you need to use NSURL fileURLWithPath:
NSURL *url=[NSURL fileURLWithPath:FilePath];
//URL Requst Object
NSURLRequest *requestObj=[NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[m_webView loadRequest:requestObj];
}
Now just Build and Go
You can use UIWebView to view an XLS file, but you won't be able to edit it. There's nothing built in to the iPhone SDK that will do this for you. You could try contacting these folks to see if they would license their software to you.