Accessing file stored in "On My iPhone" in iOS Xamarin Application

Te 1 Reputation point
2021-03-24T15:00:40.697+00:00

I need my application to be able to read a file in the "On My iPhone" folder in the files application of an iphone. The file would be downloaded from an email and into that directory. Would it be possible for an iOS application to access the file downloaded from email and into that directory? And if so, I would I go about doing that?

Thank you in advance!

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,288 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 67,781 Reputation points Microsoft Vendor
    2021-03-25T08:09:55.51+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    Yes, you can read it by UIDocumentPickerViewController. Add add following two keys to your Info.plist

       <key>UIFileSharingEnabled</key>  
           <true/>  
           <key>LSSupportsOpeningDocumentsInPlace</key>  
           <true/>  
    

    Here is demo code. I picker the text file, then return the text file's NSUrl

       public partial class ViewController : UIViewController  
           {  
               public ViewController(IntPtr handle) : base(handle)  
               {  
               }  
         
               public override void ViewDidLoad()  
               {  
                   base.ViewDidLoad();  
         
                   UIButton button = new UIButton() {  
         
                       Frame = new CoreGraphics.CGRect(10, 100, 200, 80),  
                       BackgroundColor = UIColor.LightGray,  
         
                 };  
                   button.SetTitle("Click", UIControlState.Normal);  
                   button.TouchUpInside += Button_TouchUpInside;  
         
                   View.AddSubview(button);  
         
                   // Perform any additional setup after loading the view, typically from a nib.  
               }  
         
               private void Button_TouchUpInside(object sender, EventArgs e)  
               {  
                   string[] strs = {"public.image","public.text", };  
         
                   //NSArray arr = NSArray.FromStrings(strs);  
         
                   UIDocumentPickerViewController controller = new UIDocumentPickerViewController(strs,UIDocumentPickerMode.Open);  
         
         
                   controller.Delegate = new PickerClass();  
         
                   UIApplication.SharedApplication.Windows[0].RootViewController.PresentViewController(controller,true,null);  
         
               }  
         
               public override void DidReceiveMemoryWarning()  
               {  
                   base.DidReceiveMemoryWarning();  
                   // Release any cached data, images, etc that aren't in use.  
               }  
           }  
         
         
           public class PickerClass : UIDocumentPickerDelegate, IUIDocumentInteractionControllerDelegate  
           {  
               public override void DidPickDocument(UIDocumentPickerViewController controller, NSUrl url)  
               {  
         
                   var path = url.AbsoluteString;  
         
               }  
           }  
    

    Best Regards,

    Leon Lu


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.