ios 10 Open file in default app

MehmetSeyirciMehmetSeyirci USMember
edited October 2016 in Xamarin.Forms

I have previously used this way to open documents in the default app, but starting from iOS 10 it does not work on iPhone (the app crashes).

Code running in ios9;

public async void ImageSave(byte[] imageBytes, string fileName)
{
var documentsDirUrl = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
string localPath = System.IO.Path.Combine(documentsDirUrl, fileName);

        var b = NSUrl.FromFilename(localPath);
        //Save the Image using writeAsync
        try
        {
            FileStream fs = new FileStream(localPath, FileMode.OpenOrCreate);
            await fs.WriteAsync(imageBytes, 0, imageBytes.Length);
            fs.Close();
        }
        catch (Exception e)
        {

        }

        if (File.Exists(localPath))
        {
            OpenDocument(localPath, fileName);
        }

    }

    private bool OpenDocument(String pathfilename, String FileName)
    {
        var url = "file:" + Uri.EscapeUriString(pathfilename);
        UIDocumentInteractionController _interationController = UIDocumentInteractionController.FromUrl(new NSUrl(url));
        _interationController.Name = FileName;

        var _navigationController = FindNavigationController();
        _navigationController.NavigationBar.Translucent = false;

        _interationController.ViewControllerForPreview = (controller) => _navigationController;
        _interationController.DidEndPreview += (sender, e) => { _interationController.Dispose(); _interationController = null; };
        _interationController.PresentPreview(true);

        return true;
    }

Answers

  • [https://forums.xamarin.com/discussion/comment/225628/#Comment_227550]

    [assembly: Xamarin.Forms.Dependency(typeof(iOS.NativeCodeDependencies.FileImplementation))]
    namespaceiOS.NativeCodeDependencies
    {

    class FileImplementation : IFile
    {
    
        public void OpenFileExternally(Stream fileStream, string fileName)
        {
            try
            {
    
                var documentsDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
    
                string fullPath = System.IO.Path.Combine (documentsDirectory, fileName); 
    
                if(!NSFileManager.DefaultManager.FileExists(fullPath)) {
    
                    NSData imgData = NSData.FromStream(fileStream);
    
                    NSError err;
    
                    imgData.Save(fullPath, false, out err);
    
                }
    
                //
                // This operation must be done in UIThread
                //
    
                Xamarin.Forms.Device.BeginInvokeOnMainThread(() => {
    
                    //
                    // This is a trick. Here we are trying to get the navigation renderer to get the navigationcontroller from it.
                    //
    
                    var firstController = UIApplication.SharedApplication.KeyWindow.RootViewController.ChildViewControllers.First().ChildViewControllers.Last().ChildViewControllers.First();
    

    var navcontroller = firstController as UINavigationController;
    var viewController = navcontroller.ViewControllers.Last();

                    var uidic = UIDocumentInteractionController.FromUrl(new NSUrl(fullPath, true));
    
                    uidic.Delegate = new DocInteractionC(viewController); 
    
                    uidic.PresentPreview(true);
    
                });
    
            }
            catch (Exception e)
            {
    
                throw new BaseFileException(e.Message, e);
    
            }
            finally
            {
    
                //
                // Close the original stream.
                //
    
                fileStream.Close();
    
            }
    
        }
    
        public class DocInteractionC : UIDocumentInteractionControllerDelegate
        {
            readonly UIViewController _viewController;
    
            public DocInteractionC(UIViewController controller)
            {
                _viewController = controller;
            }
    
            public override UIViewController ViewControllerForPreview(UIDocumentInteractionController controller)
            {
    
                return _viewController;
    
            }
    
            public override UIView ViewForPreview(UIDocumentInteractionController controller)
            {
    
                return _viewController.View;
    
            }
    
        }
    
    }
    

    }

Sign In or Register to comment.