Forum Xamarin.iOS
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Accessing data files from another application (Xamarin Forms - iOS)

SrbaSrba Member ✭✭
edited June 27 in Xamarin.iOS

Hi all!
We have a need to communicate with an app that is installed on the phone (it's a security app).
We managed to do this on Android with Intents and StartActivity(Intent) or with StartActivityForResault, but we have no idea if it's even possible to do this on iOS with whole 'sandbox data' thing. I know about, but don't completely understand, the concept of 'sandbox data' in iOS.
Can we do something similar to Android?
I can share the source code that we used to make this work in Android if it would help. :)

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai

    the concept of 'sandbox data' in iOS.

    The storage belongs to the application can only be accessed by itself. Other applications have no right to access this scope.
    If you want to open another application on iOS you could try URL scheme:
    https://stackoverflow.com/a/37297811/8354952
    Add a custom URL scheme in the second application's info.plist and then we could open it through:

    UIApplication.SharedApplication.OpenUrl(new NSUrl("scheme://"), new NSDictionary(), (isSuccess) =>
    {
    });
    

    If you want to share data with other applications. You could try shared group container:
    https://docs.microsoft.com/en-us/xamarin/ios/deploy-test/provisioning/capabilities/app-groups-capabilities

  • SrbaSrba Member ✭✭
    edited June 29

    If you want to share data with other applications. You could try shared group container:
    https://docs.microsoft.com/en-us/xamarin/ios/deploy-test/provisioning/capabilities/app-groups-capabilities

    I need to 'access' data from another app (not mine) for it to be uploaded through my apps REST POST method.
    Can that be done via a group container?

  • LandLuLandLu Member, Xamarin Team Xamurai

    Applications could read data from the shared storage:
    https://docs.microsoft.com/en-us/xamarin/ios/watchos/app-fundamentals/parent-app#files
    after setting up the app group.
    And then when the second application opens, it could upload data to your server from the shared storage.

  • SrbaSrba Member ✭✭

    Is this type of sharing possible since we are accessing data for upload through Xamarin CrossFilePicker (this works for random docs/pdfs/pictures on iPhone currently) or should we use something different for picking files?
    This is the message we are getting when we try to access data from another app through the picker.

  • LandLuLandLu Member, Xamarin Team Xamurai

    This is iCloudKit which allows you to store files to iCloud.
    This is another option. But the user needs to sign in the same iCloud to share data.
    Group requires the same device to share data. They have different scenarios.

  • SrbaSrba Member ✭✭

    This is another option. But the user needs to sign in the same iCloud to share data.

    While using CrossFilePicker customer has access to iCloud and its data, that is fine, this data is successfully sent via REST methods to the server.

    But when trying to access data that is stored in another app, in this case, [email protected], the customer gets
    "App is not authorized to access [email protected]".

    Everything I've read online has told me that this is 'sandbox' that Apple has and that me accessing data that is contained in another application folder is not allowed.

    I've read into about using URL Schemes, but that is just about opening certain files with different applications through my application.

  • LandLuLandLu Member, Xamarin Team Xamurai

    I've read into about using URL Schemes, but that is just about opening certain files with different applications through my application.

    If we could access this file, we could definitely manipulate it. Such as send it to the backend service through rest api.
    What is the original intent?

  • SrbaSrba Member ✭✭

    @LandLu said:

    I've read into about using URL Schemes, but that is just about opening certain files with different applications through my application.

    If we could access this file, we could definitely manipulate it. Such as send it to the backend service through rest api.
    What is the original intent?

    Correct me if I'm wrong, but, for URL Schemes I already need a path to file for it to be opened?
    That's why I thought that if I manage to simply (no simple way to do it, it seems) get file path I'll be able to upload it via REST API.
    The way it's currently done and working is through CrossFilePicker, for picking a file, and iCloud or LocalApplicationData as repositories from which the customer picks a file.
    With CrossFilePicker I get FileData type back when a file has been selected and through it, I get File Path from which to upload.

    // getting FileData from picked file
    FileData fileData = await CrossFilePicker.Current.PickFile();
    
    //Reading from file and filling a buffer to be sent over REST API
    try
    
                    {
                        fileStream = new FileStream(fileData.FilePath, FileMode.Open, FileAccess.Read);
    
                        byte[] buffer = new byte[1024];
    
                        for (int length = 0; (length = fileStream.Read(buffer, 0, buffer.Length)) > 0;)
                        {
                            outputStream.Write(buffer, 0, length);
                        }
                        outputStream.Flush();
                        // writer.Flush();
                    }
                    finally
                    {
                        if (fileStream != null)
                            try
                            {
                                fileStream.Close();
    
                            }
                            catch (IOException logOrIgnore)
                            {
                                AppConnectLog.LogAtLevel(ACLogLevel.Status, "UploadAttachment LogOrIgnore Trace: " + logOrIgnore.StackTrace);
                            }
                    }
    
  • LandLuLandLu Member, Xamarin Team Xamurai

    Store the file in the shared storage so that the apps in the same group could access it.
    And then use a URL scheme to open the destination application.
    Finally, upload the file using the shared storage path to your backend service.

Sign In or Register to comment.