Forum Xamarin.iOS

iOS Share Extension

Hi I am working on a share extension for iOS using a custom view.

I cannot seem to figure out how to extract the URL for the page that was shared.

The Extension Context will give me the name of the Page, but I cannot get the URL. When using an SLComposeServiceViewController I can grab the url in Did Select Post. But there aren't any attachments on the Extension Context now? Any ideas?

//using System;
//using CoreFoundation;
//using Foundation;
//using MobileCoreServices;
//using Social;
//using UIKit;

//namespace Share
//{
//  public partial class ShareViewController : SLComposeServiceViewController
//  {
//      protected ShareViewController(IntPtr handle) : base(handle)
//      {
//          // Note: this .ctor should not contain any initialization logic.
//      }

//      public override void DidReceiveMemoryWarning()
//      {
//          // Releases the view if it doesn't have a superview.
//          base.DidReceiveMemoryWarning();
//          // Release any cached data, images, etc that aren't in use.
//      }

//      public override void ViewDidLoad()
//      {
//          base.ViewDidLoad();
//          // Do any additional setup after loading the view.vi
//      }

//      public override bool IsContentValid()
//      {
//          // Do validation of contentText and/or NSExtensionContext attachments here
//          return true;
//      }

//      public override void DidSelectPost()
//      {
//          // This is called after the user selects Post. Do the upload of contentText and/or NSExtensionContext attachments.

//          // Inform the host that we're done, so it un-blocks its UI. Note: Alternatively you could call super's -didSelectPost, which will similarly complete the extension context.
//          //ExtensionContext.CompleteRequest(null, null);
//          //UIAlertController alert = UIAlertController.Create("Share", "You share text \"" + ContentText + "\" to Share app.", UIAlertControllerStyle.Alert);
//          //PresentViewController(alert, true, () =>
//          //{
//          //  DispatchQueue.MainQueue.DispatchAfter(new DispatchTime(DispatchTime.Now, 5000000000), () =>
//          //  {
//          //      ExtensionContext.CompleteRequest(null, null);
//          //  });
//          //});
//          var urlstr = string.Empty;
//          var item = ExtensionContext.InputItems[0];
//          NSItemProvider prov = null;

//          if (item != null)
//              prov = item.Attachments[0];

//          if (prov != null)
//          {

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

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

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

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


//      public override SLComposeSheetConfigurationItem[] GetConfigurationItems()
//      {
//          // To add configuration options via table cells at the bottom of the sheet, return an array of SLComposeSheetConfigurationItem here.
//          return new SLComposeSheetConfigurationItem[0];
//      }
//  }
//}

using System;
using System.Threading.Tasks;
using CoreFoundation;
using Foundation;
using MobileCoreServices;
using Social;
using UIKit;

namespace Share
{
    public partial class ShareViewController : UIViewController
    {
        protected ShareViewController(IntPtr handle) : base(handle)
        {
            // Note: this .ctor should not contain any initialization logic.
        }

        public override void DidReceiveMemoryWarning()
        {
            // Releases the view if it doesn't have a superview.
            base.DidReceiveMemoryWarning();
            // Release any cached data, images, etc that aren't in use.
        }


        public override void ViewDidLoad()
        {
            base.ViewDidLoad();
            Xamarin.IQKeyboardManager.SharedManager.EnableAutoToolbar = true;
            Xamarin.IQKeyboardManager.SharedManager.ShouldResignOnTouchOutside = true;

            NSExtensionItem[] outputItem = this.ExtensionContext.InputItems;

            //this.ExtensionContext.CompleteRequest(outputItem, (obj) => {
            txtUrl.Text = outputItem[0].AttributedContentText.Value;



            NSExtensionItem item = ExtensionContext.InputItems[0];
            NSItemProvider provider = item.Attachments[0];  //provider is null, no attachments




        }
        partial void btnCloseClicked(Foundation.NSObject sender) {
            this.ExtensionContext.CompleteRequest(null, null);
        }

        partial void btnNextClicked(Foundation.NSObject sender) { 
            abcView.Hidden = false;
            navView.Hidden = true;
            txtUrl.Hidden = true;
            gotoNext();
        }

        public async void gotoNext()
        {
            await Task.Delay(TimeSpan.FromSeconds(2));
            this.ExtensionContext.CompleteRequest(null, null);
        }

    }
}

Answers

Sign In or Register to comment.