view .doc, .docx, .rtf, .ppt file in iphone using Webview - iphone-sdk-3.0

I want to view .doc, .docx, .rtf, .ppt file in iphone.
But I guess something is going wrong at my side and its not working for the above formats but my code is working fine for .txt and .pdf files.
I have the read the document regarding Webview it states it supports viewing of the above document.
below is my snippet for .doc
[webView loadData:requestData MIMEType:#"application/msword" textEncodingName:#"UTF-8" baseURL:nil];
for .ppt I am using MIME type as "application/vnd.ms-powerpoint"
Note: If I am making MIME type as "text/html" for .doc/.rtf then it displays some garbage data So I think there is something missing in MIME type from my side.
Any help is highly appreciated.
Waiting for your reply.
Update:........
It seems there is some issue with NSData for opening these type of file formats.
My data is encrypted so I cannot use
requestWithURL directly and other thing is I need to pass credentials to get the file and the credentials doesnt work proplerly if I save the credentials using
[[NSURLCredentialStorage sharedCredentialStorage] setDefaultCredential:credential
forProtectionSpace:protectionSpace];
and then make the request using below
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]] ]
Is there any way I can use NSData for the above formats so that I can make async call and pass my credentials and then decrypt the data and show it in WebView.

Try using a QuickLook Framework which is an amazing feature given by Apple. If you have seen this that when you select ANY FILE on MAC System and click SPACE BAR then you will a quick preview of your file it may be any thing... *.pdf, *.doc, *.docx or any thing... Even if you have not installed those softwares for same files you will get a PREVIEW for those files...
QuickLook framework also gives same functionality... Please go through some of the links for the same..
http://robsprogramknowledge.blogspot.com/2011/02/quick-look-for-ios_21.html
https://github.com/rob-brown/Demos
https://github.com/rob-brown/RBFilePreviewer
http://robsprogramknowledge.blogspot.com/2011/02/quick-look-for-ios_21.html
(Recommended..) http://iosdevelopertips.com/data-file-management/preview-documents-with-qlpreviewcontroller.html

Not all of those formats (docx) are supported. This looks like the official word. In the example Apple does not specify a mime type.
This question suggests that you must use an NSURLRequest instead of loadData.
Edit:
As far as I have seen, you cannot directly pass data to loadData for these types.
The easy work around is to write to a temporary file. You can delete it in webViewDidFinishLoad or, technically, as soon as the the file has been opened.
The hard work around is to use NSURLCache. You should be able to implement a custom NSURLCache and have WebKit use it via setSharedURLCache. Your cache would basically know how to get your local encrypted files and pretend that they are cached. I have not tried this, but I think it is your best bet. This approach may be blocked just like loadData is.

You need to supply something to baseURL:. For example:
[webView loadData:requestData MIMEType:#"application/msword" textEncodingName:#"UTF-8" baseURL:[NSURL URLWithString:#"http://localhost/"]];
Works for me now.

Try it on an iPhone instead of Simulator

Related

iOS access to Safari Reader feature through UIWebView

I am using iOS 4.3 & was wondering if there is any way that I can access the Safari's "Reader" feature through which webpages are removed of ads & other riff raff & the content takes the center stage.
If one opens any article in Safari (on say Wikipedia website), then a "Reader" button appears on the URL bar. Clicking on it presents a new window presenting the content beautifully.
How can I leverage this this functionality in iOS through UIWebView ?
PS: I know there is something called Readability Project. But I have no idea how to use this through UIWebView. Also for some websites Safari's Reader takes a call not to enable "Reader" feature, maybe it has no sufficient confidence?
Important: THIS ANSWER NO LONGER WORKS!
Readability shut down on September 30, 2016.
Here is something they recommend as a replacement:
https://mercury.postlight.com/web-parser/
Keeping the answer as a historical reference
--- Original answer ---
You can use Readability mobilizer for this. You will get a cleaned up version of any article, in the Readability styling:
http://www.readability.com/m?url=http://{URLOFTHEARTICLE}
Just prepare the URL and load it in your UIWebView. Here is how it looks in action:
http://www.readability.com/m?url=http%3A%2F%2Fwww.cnn.com%2F2013%2F01%2F11%2Fshowbiz%2Ftv%2Fgolden-globes-tv-vineyard%2Findex.html%3Fhpt%3Dhp_abar
Apple is making a pretty big deal about the inclusion of "Reader" in iOS 5. I'm assuming by the noise it's not available in 4.3
re: How to use through UIWebView
I can't find any mention of it in the Web Content Guide.
There's nothing about it in the UIWebView class reference.
And there's nothing in QA1630.
Dont parse HTML natively on iOS, I have done it before and its a messy business. Either create your own web service to do all the nasty work or look into using readability (readability.com) they provide an API.
There is also an open source ruby, python and php readability port that you can find here
https://github.com/iterationlabs/ruby-readability
https://github.com/gfxmonk/python-readability
http://code.fivefilters.org/p/php-readability/source/tree/master/
For you ruby enthusiasts, readability is also available as a gem, just google it.
Actually reader button do a bit of analysis where it parse the HTML Page and then it sees a clear body tag to parse. If that plugin is able to extract the exact body it will enable the reader button (My understanding from the readability source code). Now to implement the same for webview you just need to embed java script in your code (this java script is already available in the readability source code) and then you can achieve the same effect.
But I suspect the future plan from apple for the same. Because they can not just let anyone else do this content extraction with the huge business opportunity associated with iCloud with the combination of readability.
If you want you can simple extract the HTML from UIWebView and then extract the body and use it for your purpose. It's not a very rocket science to extract.
For analysis point of view, just have randomly some 10 HTML pages with Reader button enabled, you will see the core cotent belongs to body only and rest of the add, header, footer are separated.
I believe this is the time to re-invent the web content we use, and this is the perfect example of doing the same.
You can even do this by injecting javascript.
#define readJS #"(function(){window.baseUrl='https://www.readability.com';window.readabilityToken='';var s=document.createElement('script');s.setAttribute('type','text/javascript');s.setAttribute('charset','UTF-8');s.setAttribute('src',baseUrl+'/bookmarklet/read.js');document.documentElement.appendChild(s);})()"
And then when your webpage finishes loading
- (void)webViewDidFinishLoad:(UIWebView *)webview
{
[webview stringByEvaluatingJavaScriptFromString:readJS];
You can do it in iOS9.
first import SafariServices:
#import <SafariServices/SafariServices.h>
Afterwards we are instantiating SFSafariViewController and adding it as a subview. We have two options doing so:
Creating with only base URL
Creating with bas URL as well as entering 'Reading Mode' in case it is available
NSString *sURL = #"http://google.com";
NSURL *URL = [NSURL URLWithString:sURL];
SFSafariViewController *safari = [[SFSafariViewController alloc] initWithURL:URL]; // 1.
SFSafariViewController *safari = [[SFSafariViewController alloc] initWithURL:URL entersReaderIfAvailable:YES]; // 2.
[self presentViewController:safari animated:YES completion:nil];

Show contents of a zip file in a WebView

I want to have a WebView that displays some static files from the application bundle. Since I have a large number of small files, I'd like to pack them all into a compressed archive so the application doesn't take up too much space. What's the best way to make this happen?
This should help you out: http://code.google.com/p/ziparchive/
To display data in the WebView:
On Mac OS X use WebFrame's loadHTMLString:baseURL:
On iOS use UIWebView's loadHTMLString:baseURL:
What you probably want to do, is implement an NSURLProtocol subclass that will resolve relative URLs by reading them from the zip archive. That way, you only need to initially read the "main" HTML file from the zip into memory, and the others will be read in on demand. To get WebKit to use your custom URL protocols for resolving relative paths, you could instantiate the WebView like this:
[[web_view mainFrame] loadHTMLString:your_main_html baseURL:[[NSURL alloc] initWithString:#"zip:///"]];
Apple has a really good example of combining a custom URL protocol with a WebView here:
https://developer.apple.com/library/mac/#samplecode/SpecialPictureProtocol/Introduction/Intro.html#//apple_ref/doc/uid/DTS10003816

How to use UTIs to make a new file type?

I have been working on making an application for editing game plugins for a game I made. I needed the files it saves to have a custom format for obvious reasons. Unfortunately, I cannot figure out how to give the file a custom icon.
I did some research last night into UTIs, mostly because they are mentioned wherever icons are talked about. I did what the Apple Documentation said, but it didn't work.
Here is my code so far:
[#"test" writeToFile:#"test.hsimg" atomically:YES encoding:NSUTF8StringEncoding error:NULL];
That works perfectly. The only problem I have now is that there is no icon. Could someone please tell me how I need to setup the UTIs to make it work?
Take a look at these questions:
How do I associate file types with an iPhone application?
Creating my own file extension based on plist

Adding Help to a Cocoa App

I want to add a simple one-page HTML page help to my Cocoa app. Can you tell me how to do it? I assume I just have to throw in one lousy .html (and maybe one .css?) file somewhere into my Cocoa project in Xcode?
Creating Apple Help documents that are opened in the Help viewer is straightforward but you must follow the directions in the documentation exactly.
Help files are HTML but you need to place a couple of special tags in the page and name the files in a particular way.
It's all explained in the documentation.
Today I've been facing the same problem. I found no up-to-date howto so here is one of my own. Help is nicely working with this Step by Step to create Apple Help in your Cocoa Xcode Application.
If you only want a single HTML page and not a proper help file, you could add an HTML document and CSS file to your project. These will be copied to your application's Resources directory inside the app bundle when you compile the project. To load the document, you'll need to get its address. This is actually quite easy:
NSString *helpFilePath = [[NSBundle mainBundle] pathForResource:#"YourHelpDocumentHere" ofType:#"html"];
NSURL *helpFileURL = [NSURL fileURLWithPath:helpFilePath];
The resulting URL will be a file URL that a WebView can display inside your application, or you can pass it off to the operating system using NSWorkspace and it will be opened in the user's default web browser.

Reading .ppt (MS PowerPoint) file in Cocoa Touch

Any idea how to read a .ppt file in Cocoa Touch ?
I tried to load the contents of the file in UIWebView but it didn't work.
Here is the code :
[aWebView loadData:[NSData dataWithContentsOfFile:filePath]
MIMEType:#"application/vnd.ms-powerpoint"
textEncodingName:#"utf-8"
baseURL:[NSURL fileURLWithPath:filePath]];
[powerWeb loadData:[NSData dataWithContentsOfFile:filePath]
MIMEType:#"application/vnd.ms-powerpoint"
textEncodingName:#"utf-8"
baseURL:[NSURL fileURLWithPath:filePath]];
All suggestions are highly appreciated.
Thanks
UIWebView is for displaying web content, in a format you could view directly in a browser. It has no idea about how to display PowerPoint files — most applications don't, since it's a proprietary Microsoft format. (I stand corrected — apparently, UIWebView does know how to display PowerPoint files and others. If it's not working, I'd suggest trying a different MIME type, such as application/mspowerpoint.) Just remember that simply loading a file into an NSData doesn't mean that anyone else you pass that data to will know how to interpret the bytes.
You might check whether Microsoft offers any tools for parsing PPT files, or look around for open-source tools — for example, Google searches often convert to HTML. Just be aware that your browser is usually not loading a PowerPoint version of the file directly.
If Webkit doesn't have a PPT parser, you're on your own - you have to manually load PPT files, parse them, and render them; it might be easiest to make a web service to do this (this way you get real libraries), then have them download the images over HTTP so the client-side implementation is simple
NSString *powerPointFilePath = [[NSBundle mainBundle] pathForResource:#"myFile" ofType:#"ppt"];
NSURL *powerPointFileURL = [NSURL fileURLWithPath:powerPointFilePath];
[self.webView loadRequest:[NSURLRequest requestWithURL:powerPointFileURL]];