Forum Xamarin.Forms
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.

I can't get the url of the shared page

danielparedes97danielparedes97 Member ✭✭✭
edited March 9 in Xamarin.Forms

For Android, everything works good (https://forums.xamarin.com/discussion/176844/how-to-share-an-url-to-my-app#latest)

For iOS I'm using SharerExtension to pass the url of the page, to my app.


I do that, but then nothing happens... I need to extract the url of the page, but I can't. I found this code...

var urlstr = string.Empty;
            var item = ExtensionContext.InputItems[0];
            NSItemProvider prov = null;

            prov.LoadItem(UTType.URL, null, (NSObject url, NSError error) =>
            {
                if (url == null)
                    return;

                NSUrl newUrl = (NSUrl)url;
                urlstr = newUrl.ToString();

                UIAlertController alerti = UIAlertController.Create("Share extension", urlstr, UIAlertControllerStyle.Alert);

                PresentViewController(alerti, true, () =>
                {
                    DispatchQueue.MainQueue.DispatchAfter(new DispatchTime(DispatchTime.Now, 5000000000), () =>
                    {
                        ExtensionContext.CompleteRequest(null, null);
                    });
                });
            });

From here: https://stackoverflow.com/questions/37355551/extract-url-from-extension-context

But It doesn't do anything...

Can someone help me???

Best Answer

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai

    You haven't set any value to the prov in your statement. Try to modify it like:

    public override void DidSelectPost()
    {
        var urlstr = string.Empty;
        var item = ExtensionContext.InputItems[0];
        NSItemProvider prov = null;
    
        if (item != null)
            prov = item.Attachments[0];
    
        if (prov.HasItemConformingTo("public.url"))
        {
            prov.LoadItem("public.url", null, (NSObject url, NSError error) =>
            {
                if (url == null)
                    return;
    
                NSUrl newUrl = (NSUrl)url;
    
                UIAlertController alerti = UIAlertController.Create("Share extension", newUrl.AbsoluteString, UIAlertControllerStyle.Alert);
    
                PresentViewController(alerti, true, () =>
                {
                    DispatchQueue.MainQueue.DispatchAfter(new DispatchTime(DispatchTime.Now, 5000000000), () =>
                    {
                        ExtensionContext.CompleteRequest(new NSExtensionItem[0], null);
                    });
                });
            });
        }
    }
    
  • danielparedes97danielparedes97 Member ✭✭✭

    @LandLu I need to do the tests with a physical iPhone or I can use a simulator/emulator of iPhone ?

    By the moment, it doesn't appear any popup/alert.

    info.plist of the extension...

    <key>NSExtension</key>
        <dict>
            <key>NSExtensionAttributes</key>
            <dict>
                <key>NSExtensionActivationRule</key>
                <string>TRUEPREDICATE</string>
                <key>NSExtensionPointName</key>
                <string>com.apple.share-services</string>
                <key>NSExtensionPointVersion</key>
                <string>1.0</string>
                <key>NSExtensionActivationSupportsText</key>
                <false/>
          <key>NSExtensionActivationSupportsWebURLWithMaxCount</key>
          <integer>1</integer>
          <key>NSExtensionActivationSupportsWebPageWithMaxCount</key>
          <integer>1</integer>
            </dict>
            <key>NSExtensionMainStoryboard</key>
            <string>MainInterface</string>
            <key>NSExtensionPointIdentifier</key>
            <string>com.apple.share-services</string>
        </dict>
    

    And info.plist of the .iOS project

    <key>NSExtensionAttributes</key> 
      <dict>
        <key>NSExtensionActivationRule</key>
        <dict>
          <key>NSExtensionActivationRule</key>
          <string>TRUEPREDICATE</string>
          <key>NSExtensionActivationSupportsWebURLWithMaxCount</key>
          <integer>1</integer>
          <key>NSExtensionActivationSupportsWebPageWithMaxCount</key>
          <integer>1</integer>
        </dict>
      </dict>
    
  • LandLuLandLu Member, Xamarin Team Xamurai

    I tested it with simulators. Have you rebuilt all the assemblies and deleted the application on the simulator?
    I didn't include the NSExtensionAttributes in my container application's info.plist.
    BTW, you could try it on VS for Mac.

  • danielparedes97danielparedes97 Member ✭✭✭

    @LandLu I have tested with simulators, and after to run the test, I delete the application on the simulator and clean the solution on VS for Windows.

    Could it be that the "public.url" is empty?

  • LandLuLandLu Member, Xamarin Team Xamurai

    You can test the sample on VS for Mac.
    We can debug the extension project directly there。
    Firstly, change the startup project to the extension project.
    Then we could hit the breakpoint in extension like:

    There will be an alert window on VS for Mac when you debug the extension application. We could leave it there and move to the next steps on simulators.

  • danielparedes97danielparedes97 Member ✭✭✭
    edited March 12

    @LandLu The problem is in the Alert line, because the newUrl has the correct site url.

  • LandLuLandLu Member, Xamarin Team Xamurai

    If you have got the correct address you could retrieve its scheme as you want, like newUrl.Scheme instead of its absolute address.
    And we could store this kind of string to the shared sandbox so that it could be accessed by the contain application.

  • danielparedes97danielparedes97 Member ✭✭✭
    edited March 18

    @LandLu I'm trying to do that...

    NSUrl newUrl = (NSUrl)url;
    string urlStr = newUrl.ToString();
    var path = saveText(urlStr);
    CrossLocalNotifications.Current.Show("Xamarin", "Se ha rescatado la url correctamente", 0, DateTime.Now.AddSeconds(7));
    

    --

    public string saveText(string txt)
            {
                var documents = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                var filename = Path.Combine(documents, "URL.txt");
                File.WriteAllText(filename, txt);
    
                return filename;
            }
    

    I want to save the text into URL.txt and with a local notifications, I'm trying that the user click on it, and open the app. At this moment, I want to read this file with the url that I need.

    public string ShowFileText()
            {
                var documents = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                var filename = Path.Combine(documents, "URL.txt");
                var text = File.ReadAllText(filename);
    
                return text;
            }
    

    The problem comes exactly when I try to read the file. It crashes because the file doesn't exist. I also have tried to put a button, to do the read action, but nothing, the file doesn't exist.

Sign In or Register to comment.