Forum Xamarin.iOS

Send data from Share Extension to Xamatin.Forms app

pelinalpppelinalpp Member ✭✭✭

I work on Share Extension. And it only has a sample project (following link) and a general extension documentation.
Basic project: https://docs.microsoft.com/en-us/samples/xamarin/ios-samples/ios8-share/
Extension documentation: https://docs.microsoft.com/tr-tr/xamarin/ios/platform/extensions

When I click share with my app on iphone (for example on photos), I want to send this file to my app and upload it remote server. But I couldn't find anyway to send object from share extension. I looked up and probably I need to use "NSUserDefaults" and I tried to use it. I followed this documentation:
https://docs.microsoft.com/en-us/xamarin/ios/app-fundamentals/user-defaults

I did these:
1)I did necessary settings (creating group id, app id etc) on my apple developer account.
2)I did necessary settings on my project Entitlements.plist
3)I try to send on ShareViewController - DidSelectPost method in the my project's share extension as below:

var default = new NSUserDefaults ("group.com.myapp.AppName", NSUserDefaultsType.SuiteName);
default.SetString ("Test content", "TestKey");
default.Synchronize ();

4)I try to receive string data on my main project's AppDelegate as below:

var default = new NSUserDefaults ("group.com.myapp.AppName", NSUserDefaultsType.SuiteName);
var key = default.StringForKey ("TestKey");
string value = key;

But I couldn't do it. And I saw someone did this with OpenUrl function. I tried this and this didn't work.

I started mobile programming with xamarin forms and I don't know enough native ios programming (I only learn with xamarin). I think share extension has insufficient documentation. So can someone help me please?

Best Answers

Answers

  • ColeXColeX Member, Xamarin Team Xamurai

    Hi , what is your exact problem ?

    • DidSelectPost method not called ?

    • Image not received in DidSelectPost method?

    • NSUserDefaults not work ?

  • pelinalpppelinalpp Member ✭✭✭
    edited February 12

    Hi,
    No, DidSelectPost method is called. But I don't know how to pass file/files to Xamarin.Forms app. I don't know Image is recieved or is not received. Because I couldn't debug shared extension project. I tried to debug with Console.WriteLine but I didn't see anythings. And NSUserDefaults doesn't work. I guess I'm making a mistake somewhere, Because firstly I try to send a string data from share extension project to xamarin.ios project. But I couldn't send. If I send a string data from share extension project to xamarin.ios project, I will try to send "NSExtensionItem". But I couldn't use NSUserDefaults yet.
    And I tried to use NSUserDefaults as above steps which I said. I don't know where I am wrong.

  • pelinalpppelinalpp Member ✭✭✭

    Actually my problem is while the app is in the background, I can't open the app and can't get data with NSUserDefaults Because the process I want to do is to open the photos and to click "share with my application". After that I want to open my app and get this data (with NSUserDefaults or anythings) and upload file/files to the remote server. But I can't open my app with share extension. How to get data with NSUserDefaults when app is in background?

  • ColeXColeX Member, Xamarin Team Xamurai
    edited February 13

    Set Extension Projects as startup project , so that you can debug on the project , add breakpoint in method DidSelectPost to see what happened.

    Check https://github.com/xamarin/release-notes-archive/blob/master/release-notes/studio/xamarin.studio_6.2/xamarin.studio_6.2.md#debugging-ios-app-extension-projects

  • pelinalpppelinalpp Member ✭✭✭

    Thank you I will look it

  • ColeXColeX Member, Xamarin Team Xamurai

    Any update ?

  • pelinalpppelinalpp Member ✭✭✭

    I tried, but I couldn't. I recorded a video what I do. Could you check it please? Where I do missing?
    Video link: https://drive.google.com/open?id=1vof-YAqRUFqutqPa1Hm-g15BR0pNMvL3

  • pelinalpppelinalpp Member ✭✭✭

    @ColeX And I tried clean and rebuild projects

  • MarcoSalvatoriMarcoSalvatori USMember ✭✭✭

    I'm working on the same problem. I read a lot of documentation and many forum posts. There doesn't seem to be a clear way to send data from the extension to the mother APP.
    Valuable information can be retrieved here https://stackoverflow.com/questions/27506413/share-extension-to-open-containing-app where it is also said that:

    "Technically you can't open containing app from share extension, but you can schedule local notification, and that's what I end up doing. Just before I call super.didSelectPost, I schedule local notification with some text, and if user wants to open containing app, they can, and if not - they can continue with their workflow. I even think its a better approach than automatically opening containing app and disrupting what they are doing."

  • pelinalpppelinalpp Member ✭✭✭

    Yes I saw this. But some app can do this for example Box. Do they do it with local notifications? How much size data can send with local notification? Is local notification convenient this process? I don't know. It must be another way. But I will try to use it with local notification.

  • pelinalpppelinalpp Member ✭✭✭
    edited February 17

    Yes, I did it. But url scheme works only for one file. It doesn't support multiple file share, that doesn't work in each location(for example photos). :(

  • pelinalpppelinalpp Member ✭✭✭

    And I can debug on physical device when I set extension projects as startup project. Maybe it helps another one.

  • pelinalpppelinalpp Member ✭✭✭
    edited February 19 Accepted Answer

    Hey, @MarcoSalvatori you are right. Firstly when you said URL scheme, I thought you mention from "Exported UTIs, Imported UTIs". After I understood I must use url scheme for open url. I tried lots of things so I thought missing firstly.

    I could do finally. I used NSUserDefaults and OpenUrl. But I didn't do in the my job projects. I did same things for my business project but it doesn't work. According to my guess provisioning profile aren't updated in my business project.

    I did sample project for share extension. Maybe it can help another one. It isn't best syntax. It can be improved but it works basically.
    Sample project: https://github.com/Pelinalpp/XamarinShareExtension

  • MarcoSalvatoriMarcoSalvatori USMember ✭✭✭

    I left the project on standby. As soon as possible I will take it back and do my tests also based on your code. Thank you.

Sign In or Register to comment.