1

I try 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, 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?

1 Answer 1

0

One possible issue in your code is that you're using the presentOpenInMenuFromRect method of UIDocumentInteractionController to display the application picker. However, this method requires a valid rectangle to specify the anchor point from which the menu should be presented. In your code, you're passing an empty rectangle (CGRectFromRect(TRectF.Empty)) which might be causing the issue.

To fix this, you can try providing a valid rectangle for the anchor point. For example, you can pass the bounds of the view controller's view as the rectangle:

var
  anchorRect: CGRect;
  rootView: UIView;
begin
  // ...

  rootView := SharedApplication.keyWindow.rootViewController.view;
  anchorRect := rootView.bounds;

  interactionController.presentOpenInMenuFromRect(anchorRect, rootView, true);

By providing a valid rectangle, the application picker should be displayed correctly.

Additionally, make sure that you have set the appropriate document type (UTI) for the file you're trying to open. In your code, you're setting it to 'public.data', which is a generic type. You may need to use a specific UTI that corresponds to the file type you're working with. For example, if you're working with a PDF file, you can use 'com.adobe.pdf' as the UTI.

Ensure that you have imported the necessary frameworks in your project, such as MobileCoreServices.framework, to work with UTIs.

Lastly, double-check that you have the necessary permissions and entitlements in your app to open files and interact with other applications.

By addressing these issues and ensuring that you have the correct UTI and necessary permissions, you should be able to open the file in the desired application or display the application picker dialog.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.