I'm planning an iOS application which will allow browsing documents on my server using a WebService. The user will have the option to download documents (PDF and MS Office) to have them availab开发者_如何学Pythonle offline. Now I know that iPhone/iPad is capable of viewing all kinds of document formats, including PDF, DOC(X), XLS(X) if they are received through email for instance. I assume they are all handled by the same "preview module".
The question is: if I have the documents in my app's storage, how can I use the same functionality?
Solution in ObjC or MONOTOUCH is fine, or just pointing my into the right direction in general.
René
You can use the UIDocumentInteractionController
which will present the user with a menu offering them "Quick Look" (which will open the file in the app) or the option to open the file in another app depending on what the user has installed.
Same sample code I've used looks like this (where e.Result is the result of a call to my web service).
string path = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
string filePath = Path.Combine(path, e.Result.Filename);
if (!File.Exists(filePath))
{
File.WriteAllBytes(filePath, e.Result.DocumentData);
}
UIDocumentInteractionController docControl = UIDocumentInteractionController.FromUrl(NSUrl.FromFilename(filePath));
docControl.Delegate = new UIDocumentInteractionControllerDelegateClass(this);
InvokeOnMainThread(delegate {
docControl.PresentOptionsMenu(new RectangleF(0,-260,320,320), tbnv.View, true);
});
The delegate class contains overrides for the view where the preview view will appear (so you want to pass the current view to this and ensure that this is the view the preview will be presented in).
In my app, I allow users to view PDF's in-app by using a UIWebView control, and just loading the url of the PDF in it. That should work for internal PDFs also. I've never tried it with other types of docs, but I imagine the same approach would work.
精彩评论