Unable to display macro enable word file in UIWebview - ios

I'm trying to display MS office files(word,powerpoint,excel) using UIWebview some of the files have macros enable UIWebview is unable to display these files any idea why this happen? is there a way to make UIWebview render these files?.
Note: I do not want the macros to work if i can display the content of the file that will be enough.

I know this is old, but I ran into it today. It looks UIWebView will NOT open macro-enabled Office files directly. For example, the following code fails -
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:filePath]]];
The code above fails ONLY for macro-enabled Office files - it works just fine for .docx, .pptx, .rtf, .pdf, .txt, etc. files. However, if we pull the file into NSData and then provide the mime type explicitly to UIWebView, the file will open. The code below will open these macro-enabled Office files -
// this will open a .pptm file - replace mime type as necessary for other macro-enabled file types
NSData* fileData = [NSData dataWithContentsOfFile:filePath];
[webView loadData:fileData MIMEType:#"application/vnd.ms-powerpoint.presentation.macroEnabled.12" textEncodingName:nil baseURL:nil];
Tested with .pptm, .ppsm, .potm, .docm, .xlsm

Related

Loading NSData into WKWebview shows doc and XLS file in some encoded format

I have a requirement where I need to load NSData (from a local file like doc or xls) into WKWebView.
Loading a file URL works fine but converting into data and the loading it using loadData shows xls and doc files in some encoded format.Converting file into
NSData using - [NSData dataWithContentsOfFile:self.fileURLPath];
and loading data into wkwebview using
[self.webView loadData:data MIMEType:self.MIMEType characterEncodingName:#"" baseURL:self.fileURL.NSURL];
Observations -
Works fine with UIWebView and also when loading fileurl directly into WKWebView.

How to open .csv link in UIWebView

I want to open .csv link in the UIWebView. I am getting link some .csv links from the web how to open that link in the UIWebView. I am using the below code
NSString *urlString = #"https://xoxoengage-images-test.s3.amazonaws.com/image/clients/gxoxo/annoucement/XXXXdetails201805021526899124.csv";
NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];//[NSURL URLWithString:urlString];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[_webviewobj loadRequest:urlRequest];
but the result is shown in the below image, help me out to sort this issue.
The UIWebView doesn't support the CSV format natively.
See this: File Formats Supported by UIWebView
If your server returns "text/plain" (or you have this file as ".txt") you'll see its raw text in the web view.
If you want a grid table view, then you need to convert csv to to one of the supported formats. The obvious choice would be HTML.
If you can convert on the server side - that's better, otherwise download the csv file, parse it line by line, and output as HTML. Load the HTML into the web view. loadHTMLString is what you could use.
Also with UIWebView this can be done sort of transparently using a custom NSURLProtocol.
This might be helpful: Where can I find a CSV to NSArray parser for Objective-C?

How to open a web (.html) ressource file with native code (ios app)

I have an ios app built with Cordova.
I would like to open a web ressource file (.html) after a deep linking, in a UIWebView with the handleURL ios method.
I know I'm supposed to do this within my cordova JS files, but I would like to know the way to do it natively?
Let's say I define a my-app URL scheme and want to open file1.html.
What are the ways for doing that?
I have found this:
// Load the html as a string from the file system
NSString *path = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"];
NSString *html = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
// Tell the web view to load it
[WebView loadHTMLString:html baseURL:[[NSBundle mainBundle] bundleURL]];
But nothing happens, like it can't find that file.
Why is that?
put all files in a folder and while adding drag drop in project. and select create folder references and copy items if needed. and try to load in UIWebView
You make a new class that inherits from NSURLProtocol.
You need to call [NSURLProtocol registerClass:YOURCLASS] in your app
In your class, implement + (BOOL) canInitWithRequest:(NSURLRequest *) to return true if the scheme matches your scheme (e.g. my-app)
See the docs for NSURLProtocol to see how to implement the rest of the functionality. For simple file system reads, you can implement startLoading and call self.client callbacks (e.g. URLProtocol:didLoadData:). For more complex things, you might need to override more.
See this tutorial: http://www.raywenderlich.com/59982/nsurlprotocol-tutorial

how to save existing pdf file in NSDocumentDirectory in ios

I have one pdf file and I want to save that file using NSDocumentDirectory and retrieve it.how do i convert pdf file into NSData so that I can save. Please help me. Thanks in advance.
Brad,
Welcome to Stack Overflow.
This is not a site where people give you solutions to your problems "out of whole cloth". You need to show what you have attempted, and the specific places where you are stuck.
You say you "have one pdf file". Is it a file on disk somewhere, in memory, or what? If it's already on disk then you can use the file manager to copy it to the documents directory. Take a look in the Xcode docs under NSFileManager and read the class reference. There are tons of useful methods for creating and copying files.
You say "..how do i convert pdf file into NSData so that I can save." Erm, if it's a file, why do you need to convert it to NSData in order to save it? It's already a file.
NSData has methods for creating a data object with the contents of a file (dataWithContentsOfFile and dataWithContentsOfURL) as well as methods for writing an NSData object to a file (again using a path or an NSURL, like the dataWith... methods)
As you said you have PDF file so i guess it will be a ready file.
Now in that case you don't need to put it in document directory. Just put it into your resources folder where you placed images & other files.
After that to read that pdf file Simply do this:
UIWebView *myweb = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
myweb.scalesPageToFit = YES;
NSString *path = [[NSBundle mainBundle] pathForResource:#"myPDF" ofType:#"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[myweb loadRequest:request];
[self.view addSubview:myweb];
Hope it will work for you.

How to open & view view .doc, .docx, .rtf, .ppt, .pptx, .xlsx, .xls file in iphone using UIWebview in Document directory?

Can we open & view .doc, .docx, .rtf, .ppt, .pptx, .xls, .xlsx file in iphone using UIWebview?
I am using Document directory to show the file.
.doc file is working fine here… but rest of the files' extensions are not working..
if any one has implemented this then please help me...
any code snippet or any web link help...
thanks in advance...
You may want to look at the QuickLook Framework for iOS:
https://developer.apple.com/library/ios/documentation/FileManagement/Conceptual/DocumentInteraction_TopicsForIOS/Articles/UsingtheQuickLookFramework.html
It supports the following formats:
iWork documents
Microsoft Office documents (Office ‘97 and newer)
Rich Text Format (RTF) documents
PDF files
Images
Text files whose uniform type identifier (UTI) conforms to the public.text type (see * Uniform Type Identifiers Reference)
Comma-separated value (csv) files
More specifically you may want to look at the QLPreviewController: https://developer.apple.com/library/ios/documentation/NetworkingInternet/Reference/QLPreviewController_Class/Reference/Reference.html#//apple_ref/occ/cl/QLPreviewController
with swift 2.2
For showing PDF and DOC file I have use following code snip
//Document file url
var docUrl = NSURL(string: "https://www.google.co.in/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&ved=0ahUKEwjjwPSnoKfNAhXFRo8KHf6ACGYQFggbMAA&
url=http%3A%2F%2Fwww.snee.com%2Fxml%2Fxslt%2Fsample.doc&usg=AFQjCNGG4FxPqcT8RXiIRHcLTu0yYDErdQ&sig2=ejeAlBgIZG5B6W-tS1VrQA&bvm=bv.124272578,d.c2I&cad=rja")
let req = NSURLRequest(URL: docUrl!)
webView.delegate = self
//here is the sole part
webView.scalesPageToFit = true
webView.contentMode = .ScaleAspectFit
webView.loadRequest(req)
Note: make sure on backed side (for api) we have to define content-type as document in headers.
Try this:
[self.m_webView loadRequest:[NSMutableURLRequest requestWithURL:[NSURL fileURLWithPath:_m_filePath]cachePolicy:NSURLCacheStorageAllowedInMemoryOnly timeoutInterval:3.0]];
You can change timeoutInterval and cachePolicy, in sometime you have to wait more time to loading.

Resources