Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 89864

Open file using UIDocumentInteractionController

$
0
0

Hi all,

I am working on Xamarin Forms application. The app currently downloads files from URL and save it in Documents directory of the phone. What I intend to achieve now is to view those downloaded files on Button Click. The app popups the default programs available to open the file.

I've done so in android. But I am having problems in iOS. I am using "UIDocumentInteractionController" to view the files. But the button click event doesn't display any options.

Following is my code sample. Please help.

`
public partial class ViewController : UIViewController
{
string filePath;
string fileName;

public override void ViewDidLoad()
{
    base.ViewDidLoad();

    filePath = "/Users/user/Library/Developer/CoreSimulator/Devices/6A3459AC-A7BC-40CE-A3F9-6581215E3421/data/Containers/Data/Application/BC5B26E9-ABD9-4310-96F0-57194837A73E/Documents/test1.pptx";

    var buttonO = new UIButton(UIButtonType.RoundedRect) {
        Frame = UIScreen.MainScreen.Bounds,
        BackgroundColor = UIColor.Red
    };

    NSString urlString = new NSString(filePath);
    NSUrl myFileUrl = new NSUrl(urlString);
    Console.WriteLine(myFileUrl.AbsoluteString);
    string absPath = myFileUrl.Path;

    buttonO.TouchUpInside += (sender1, e1) => {
        System.Diagnostics.Debug.WriteLine("File Path: " + filePath);

        // CALL FUNCTION
        OpenFileFunc();
    };

    Add(buttonO);
}

public void OpenFileFunc()
{
    var viewer = UIDocumentInteractionController.FromUrl(NSUrl.FromFilename(filePath));
    var controller = GetVisibleViewController();
    viewer.PresentOpenInMenu(controller.View.Frame, controller.View, true);

}

private UIViewController GetVisibleViewController(UIViewController controller = null)
{
    controller = controller ?? UIApplication.SharedApplication.KeyWindow.RootViewController;

    if (controller.PresentedViewController == null)
        return controller;

    if (controller.PresentedViewController is UINavigationController) {
        return ((UINavigationController)controller.PresentedViewController).VisibleViewController;
    }

    if (controller.PresentedViewController is UITabBarController) {
        return ((UITabBarController)controller.PresentedViewController).SelectedViewController;
    }

    return GetVisibleViewController(controller.PresentedViewController);
}

}
`


Viewing all articles
Browse latest Browse all 89864

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>