iosfiledelphifiremonkeydelphi-11-alexandria

Delphi - Open a file with default Application in under iOS or show App picker


I am trying to open a file with my app. The File is transferred via a Web service and saved in a local Database, BASE64 encoded. When I show the file in my App, I want to use the default Application for that file type. Or at least I want to show an application picker dialog and let the user choose the application to open the File with. What I have so far is this:

procedure Base64StringToFileAndOpen(const base64String: string; Filename: string);
var
  decodedBytes: TBytes;
  filePath: string;
  fileStream: TFileStream;
  interactionController: UIDocumentInteractionController;
  NSU: NSURL;
begin
  // Decode BASE64 string into bytes
  decodedBytes := TNetEncoding.Base64.DecodeStringToBytes(base64String);

  // Determine the file path
  // NSStrToStr(TNSString.Wrap(NSTemporaryDirectory));
  filePath := System.IOUtils.TPath.Combine(System.IOUtils.TPath.GetTempPath, Filename);

  // Save decoded bytes into a file
  fileStream := TFileStream.Create(filePath, fmCreate);
  try
    fileStream.WriteBuffer(decodedBytes[0], length(decodedBytes));
  finally
    fileStream.Free;
  end;

  NSU := TNSURL.Wrap(TNSURL.OCClass.URLWithString(StrToNSStr(PChar(filePath))));
  interactionController := TUIDocumentInteractionController.Wrap(TUIDocumentInteractionController.alloc.init);
  interactionController.setURL(NSU);
  interactionController.setUTI(StrToNSStr('public.data'));
  interactionController.presentOpenInMenuFromRect(CGRectFromRect(TRectF.Empty), SharedApplication.keyWindow.rootViewController.view, true);
end;

When I run that code on the iPhone (iOS), a box like you would normally expect to show up is shown on the end of the Screen. But nothing is in that Screen. No Apps, no Text, nothing. I would expect Apps to appear here that let me open that File. Where is my mistake? I looked for Kastri framework if something useful is implemented there, but there is only a Share Component, but I do not want to share my File I only want to display it in another app.

Can anyone help me out?


Solution

  • Problem was not the Rect. Problem is because of the way iOS handles files and Apps in sandboxes.

    https://github.com/emozgun/delphi-ios-file-storage-sharing

    emozgun give a great explanation for that Problem and a Solution. Kastri and the ShareItems sample was my way to to go.