Import and Export

I’m sure this has been discussed a lot, but I can’t find anything here.

My app is creating files (basically xml) that I need to be able to “share” or save for use in other apps. I prefer not to require users do deal with iTunes to get the files.

Likewise I need to be able to import the files to process.

I know Apple likes to keep things in the sandbox but I own a lot of apps that can save and import from the files app.

Is this possible from Xojo?
What do you recommend?

if you are sending XML to your ios app, you only have iTunes if using Windows.
You can share the documents folder with Finder on a Mac.
There is no (easily understandable?) way to get such documents from iCloud. (prove me wrong someone)

To get files OUT of your app, you can share the file with a sharing panel.
Options for sharing are to send to files, send to email, message

I don’t want to just share the XML text, I need to share it as a file. Can you do this with the sharing panel?

How do apps like LightRoom import and export from/to the files on the device?

Ok, I figured out how to share a file.

Now just importing!

There is a way to get access to files, I believe I wrote some code for that a while ago.

Will try to find the code and share it here or on Github

Following!

I think you may have.
I’m looking through some of the code you shared (I believe it’s yours), and there is a module called iCloudStuff.

Maybe that’s not it though.

iCloudStuff is part of iOSKit, but I’m not sure that it works in current versions of iOS. I experienced a lot of pain with iCloud and declares so good luck getting it working. If Christian has something that works in MBS plugins you should just use than and move on.

Sorry you are of course right that it’s in iOSKit.
I’m writing from the passenger seat on a cross country drive so I wasn’t fact checking.

I don’t know that the iCloud API are even the right ones for me to be looking at.

I got it to work on a real device, with one minor modification.

Add this method to UIDocumentPickerViewController

Public Sub PresentInView(mView As MobileScreen, completion As iOSBlock = nil)
  declare sub presentViewController lib UIKitLib selector "presentViewController:animated:completion:" _
  (obj_id as ptr, controller as ptr, animated as Boolean, completion as ptr)
  
  
  
  if completion <> nil then
    presentViewController(mView.ViewControllerHandle, self, True, completion.Handle)
  else
    presentViewController(mView.ViewControllerHandle, self, True, nil)
  end if
  
End Sub

In a MobileScreen add a browser as UIDocumentPickerViewController property.

Add the following method to the screen:

Private Sub DidPickDocument(picker As UIDocumentPickerViewController, url As NSURL)
  
  
  
  if url <> nil then
    
    Dim shouldBeAbleToOpen As Boolean
    
    Select case picker.documentPickerMode
    Case UIDocumentPickerViewController.UIDocumentPickerMode.Open 
      if url.StartAccessingSecurityScopedResource then
        shouldBeAbleToOpen = True
      else
        break
        //handle the error
      end if
    Case UIDocumentPickerViewController.UIDocumentPickerMode.Import 
      shouldBeAbleToOpen = True
    End Select
    
    
    If shouldBeAbleToOpen then
      
      Dim f As new FolderItem(url.path)
      try
        Dim pic As Picture = Picture.Open(f)
        ImageViewer1.Image = pic
      Catch
        MsgBox("Image error")
      end try
      
      url.StopAccessingSecurityScopedResource
      
    Else
      MsgBox("can't access image")
      
    end if
  end if
  
  
End Sub

In a Button add the following code:

//Allowed UTIs
Dim UTIs() As Text = array("public.jpeg")

//Creating the NSArray
Dim strings() As Foundation.NSString
For each u as String in UTIs
  strings.Append new Foundation.NSString(u)
Next

Dim nsArr As Foundation.NSArray = Foundation.NSArray.CreateWithObjects(strings)

browser = new UIDocumentPickerViewController(nsArr, _
UIDocumentPickerViewController.UIDocumentPickerMode.Import) //.Import will tell iOS to create a copy of the file in your sandboxed app container/temp folder
//Use .Open to open the file directly


AddHandler browser.DidPickDocument, AddressOf DidPickDocument


browser.PresentInView(self)

It is not necessary to add an Entitlement for iCloud.

EDIT: Updated DidPickDocument to handle PickerMode.Open and PickerMode.Import
Both have been tested on a real device to open a JPG picture located on my iCloud Drive.
.Import mode was required to open a JPG picture located on Google Drive.

Do you ever sleep?!

This is fantastic.
I’ll be implementing it early tomorrow.

Thank you for taking the time to share. It’s very appreciated.

1 Like

Of course I do :sleeping::sleeping:

Does this need iOSKit bundled first?

Success.

Thank you,l!

1 Like

Yes

I just want to say that, Jeremie, people like you and @Jason_King and others on the forum who spend their time helping the rest of us bring non-builtin functionality to our apps are so very much appreciated! My apps simply could not do what I would want them too without you sharing your deep insight and code. I’ve already incorporated this into the app I am currently working on. So, Thank you!

5 Likes

I second that.
I hope some of us can return the favor sometime.

1 Like

I will take payment in beverages at XDC London :wink:

Anyway, I am happy to help :slightly_smiling_face:

4 Likes
Forum for Xojo Programming Language and IDE. Copyright © 2021 Xojo, Inc.