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.

Share Extension Crashes on try to get stream of an .mov file

JanSchraderJanSchrader Member ✭✭

We currently are workiong on an share extension for mediafiles. Every thing working fine expecpt .mov files.

Every kind of attempt to load them in an stream or get any bit of the shared .mov files failed with an app crash.

Could anyone say why or has an solution for this kind of poblem?

We tried:
NSData().AsSteam/.ToArray and foreach
File.Load()
FileInfo.openRead()
AVAssets

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai

    Are you trying to read the stream in the extension?
    I suggest storing them directly in the shared container and then analyze them in your container application.
    We need to enable the App Group before achieving it:
    https://docs.microsoft.com/en-us/xamarin/ios/deploy-test/provisioning/capabilities/app-groups-capabilities

  • JanSchraderJanSchrader Member ✭✭

    Storring in the shared Container it self is not possible because that this operation cause the same result that the app crashes without exception. Every attemt of an file oparation isn't posible for.mov files in the extension and app.

  • LandLuLandLu Member, Xamarin Team Xamurai

    How did you manipulate the files in your share extension? Could you please share some code here?
    We could try to retrieve the URL of the videos like:

    NSExtensionItem extensionItem = ExtensionContext.InputItems.FirstOrDefault();
    NSItemProvider itemProvider = extensionItem.Attachments.FirstOrDefault();
    if (itemProvider.HasItemConformingTo("public.movie"))
    {
        var item = await itemProvider.LoadItemAsync("public.movie", null);
        if (item is NSUrl)
        {
            NSUrl movieUrl = (NSUrl)item;
        }
    }
    

    Copy the file to the storage through this path or pass it directly to the container app.

  • JanSchraderJanSchrader Member ✭✭

    To get the NsUrl its the same code as you postet, and from there on if tryed to get the file as stream with NSData.FromURL(movieUrl ).AsStream() or .ToArry(); File.load(movieUrl .Path); FileInfo.OpenRead(movieUrl .Path);

    To Copy File.Copy(movieUrl.Path, Path to App Group Share); FileInfo.CopyTo(Path to App Group Share);

    and i try to get the byts of NSData from an For/Foreach loop manuely its all the same result.

  • LandLuLandLu Member, Xamarin Team Xamurai

    Try to copy it to the container directly instead of reading the data:

    NSUrl movieUrl = (NSUrl)item;
    
    NSFileManager fileManager = NSFileManager.DefaultManager;
    
    fileManager.Copy(movieUrl, destinationUrl, out error);
    
  • JanSchraderJanSchrader Member ✭✭

    copy work but the file couldn't be load by the extension and app

  • LandLuLandLu Member, Xamarin Team Xamurai

    What do you mean it couldn't be loaded?
    Can you retrieve the video from shared container in the container app?

  • JanSchraderJanSchrader Member ✭✭

    No, loading the video from the shared container results in the same crash of the app and extension, then loading it from the location that's be provided by IOS for the share extension

  • LandLuLandLu Member, Xamarin Team Xamurai

    Do you mean even though you retrieve data in the main app it will still crash?
    Could you please post the exception here?
    Make sure the file has been stored successfully at the destination path.
    And how did you get the data from a specific path?

  • JanSchraderJanSchrader Member ✭✭

    I dont get any Usefull Exception, Dot.Net shown nothing try catch did not working. And the ios device logs shows only an invalid json (it stop writing the log after property"memoryPages") file with some device date.

  • LandLuLandLu Member, Xamarin Team Xamurai

    Where did you retrieve the file main app or extension app?
    Try to get the file on the main app.
    How did you read the data post some code here.

  • JanSchraderJanSchrader Member ✭✭

    I try to get it in te main app wit all kind off possible Stream Provider i can use for ios Apps. the same i already listet yesterday

    the nsUrl for the ons that has to use it i created by NSUrl.FromFilename(Path I Saved the File)

  • LandLuLandLu Member, Xamarin Team Xamurai

    I can't figure out what happened on your side without your code.

  • JanSchraderJanSchrader Member ✭✭

    as example: NSData.FromURL( NSUrl.FromFilename("Path I Saved the File")).AsStream()
    thear is no more code

  • LandLuLandLu Member, Xamarin Team Xamurai

    If you want to create a URL from path use this:

    new NSUrl(path, false);
    

    How did you get a shared container path? Make sure your file has existed on that path using:

    File.Exists(path);
    
  • JanSchraderJanSchrader Member ✭✭

    already tryed new NSUrl(path, false); dose change nothing.

    File.Exists(path); ist true

  • JanSchraderJanSchrader Member ✭✭

    Also
    NSFileManager fileManager = NSFileManager.DefaultManager;
    fileManager.Contents("Path to Saved the File").AsStream();

    Did not work either

  • LandLuLandLu Member, Xamarin Team Xamurai

    Try to get NSData first.
    Can you get the expected value through NSData.FromUrl(new NSUrl(path, false));?

  • JanSchraderJanSchrader Member ✭✭

    I could Create the NSData but every attampt to read any bit from it after createing fails in the same crash result.

    Example:
    NSData data = NSData.FromUrl(new NSUrl(path, false));
    data.AsStream();

        NSData data = NSData.FromUrl(new NSUrl(path, false));
        var firstByte = data[0];
    
  • LandLuLandLu Member, Xamarin Team Xamurai

    NSData is not a collection we can't access it through data[0]. Convert it to byte array first and then access its first byte:

    NSData data = NSData.FromUrl(new NSUrl(filePath, false));
    var streams = data.AsStream();
    var bytes = data.ToArray();
    var firstByte = bytes[0];
    
  • JanSchraderJanSchrader Member ✭✭
    edited April 1

    in the same post i write that data.AsStream(); did not work, how should your example work for me? And NSData is an colection data[0]; returns an byte i use this on other applications for some reasons.

  • LandLuLandLu Member, Xamarin Team Xamurai

    Try to provide a sample here to help me reproduce this issue.
    This code worked on my side.

  • JanSchraderJanSchrader Member ✭✭

    I only use the demo code for Share Extensions with

    if (itemProvider.HasItemConformingTo("public.movie"))
    {
        var item = await itemProvider.LoadItemAsync("public.movie", null);
        if (item is NSUrl)
        {
            NSUrl movieUrl = (NSUrl)item;
        NSError error = new NSError();
    
        NSFileManager fileManager = NSFileManager.DefaultManager;
    
        var AppGroupContainer = fileManager.GetContainer("AppGroupID");
        string AppGoupPath = AppGroupContainer.Path;
        string NewFile = Path.Combine(AppGoupPath, "Temp.mov");
    
        if(fileManager.FileExists(NewFile))
           fileManager.Remove(NewFile);
    
        fileManager.Copy(movieUrl, new NSUrl(NewFile , false), out error);
    
        if(fileManager.FileExists(NewFile))
        {
            NSData data = NSData.FromUrl(new NSUrl((NewFile, false));
            var streams = data.AsStream();
            // App Crash
        }
    
        }
    }
    

    To try loading in the extension.

    To try loading in the app itself i drag the part

    if(fileManager.FileExists(NewFile))
    {
    NSData data = NSData.FromUrl(new NSUrl((NewFile, false));
    var streams = data.AsStream();
    // App Crash
    }

    To an simply button click in the app.

  • LandLuLandLu Member, Xamarin Team Xamurai

    I can't figure out what will cause an exception if the file does exist on that specific path so we need your sample to help us reproduce this issue.
    If you feel difficult to share it on this public forum, try to open a free support ticket here:
    https://support.microsoft.com/en-us/supportforbusiness/productselection?sapId=211dd84f-3474-c3c5-79bf-66db630c92a6

  • JanSchraderJanSchrader Member ✭✭

    https://1drv.ms/u/s!AlyHXQLoRDospvYnrXzUsY2fNVL6rA?e=235nTv

    Example app with same crash on try get stream of file

  • LandLuLandLu Member, Xamarin Team Xamurai

    Try to check your group settings here:

    var AppGroupContainer = fileManager.GetContainerUrl("group.MyAppGroups.share");
    

    Will you get a valid container?
    If I comment out the shared part and retrieve the data directly it succeed:

    Streams:

  • JanSchraderJanSchrader Member ✭✭

    I get an valid container and coud not get an stream if i try to get it directly.

  • JanSchraderJanSchrader Member ✭✭

    As info, the problem only exists on hardware devices (tested on iphone 11, 7 and 8), in an virtual devies it works

  • LandLuLandLu Member, Xamarin Team Xamurai

    It worked as expected as I showed above on my iPhone 7 Plus using your project.

Sign In or Register to comment.