Sharing a Url to the Browser with UIActivityViewController

I am trying to share a Url (NSUrl) with UIActivityViewController.

I would like to open that url in the iOS browser app.

I have the following code:

var activityViewController = new UIActivityViewController(new NSObject[] { _web.Request.Url }, null) { ExcludedActivityTypes = new NSString[] { UIActivityType.AssignToContact, UIActivityType.SaveToCameraRoll } };
if (DeviceHelper.IsPhoneDevice()) { PresentViewController(activityViewController, true, null); } else { _popup = new UIPopoverController(activityViewController); _popup.PresentFromBarButtonItem(_shareButton, UIPopoverArrowDirection.Down, true); }

The code works fine, but I do not get the option to share to the built in browser app (Safari), why ?

Is it really nessesary to create my own UIActivity for Safari browser sharing of Url's ?

Posts

  • PerHungebergMllerPerHungebergMller DKMember ✭✭✭

    I found out that you need to roll your own activity for browser sharing.

    Here's the code for others to use as inspiration:

    ` public class BrowserActivity : UIActivity
    {
    private NSUrl _url;

        public override string Title
        {
            get
            {
                return "Safari";
            }
        }
    
        public override NSString Type
        {
            get
            {
                return (NSString) "com.millersystems.browserSharing";
            }
        }
    
        public override UIImage Image
        {
            get
            {
                return Resources.ShareButton;
            }
        }
    
        public override bool CanPerform(NSObject[] activityItems)
        {
            return true;
        }
    
        public override void Prepare(NSObject[] activityItems)
        {
            foreach (var item in activityItems)
            {
                if (item is NSUrl)
                {
                    _url = item as NSUrl;
                }
            }
        }
    
        public override void Perform()
        {
            if (_url == null) return;
    
            UIApplication.SharedApplication.OpenUrl(_url);
        }
    }
    

    `
    And the UIActivityViewController implementation to use it (implemented as a nice static helper method):

    ` public static class SharingHelper
    {

        public static void ShareUrl(NSUrl url, 
                                    UIViewController controller, 
                                    UIBarButtonItem shareButton)
        {
            if (VersionHelper.IsIos6OrBetter)
            {
                var activityViewController = 
                    new UIActivityViewController(new NSObject[] {url},
                                                 new UIActivity[] {new BrowserActivity()})
                    {
                        ExcludedActivityTypes = new NSString[]
                            {
                                UIActivityType.AssignToContact,
                                UIActivityType.SaveToCameraRoll
                            }
                    };
    
                if (DeviceHelper.IsPhoneDevice())
                {
                    controller.PresentViewController(activityViewController, true, null);
                }
                else
                {
                    var popup = new UIPopoverController(activityViewController);
                    popup.PresentFromBarButtonItem(shareButton, 
                                                   UIPopoverArrowDirection.Down, true);
                }
            }
            else
            {
                var actionSheet = new UIActionSheet(null, 
                                                    null, 
                                                    "Close".Translate(), 
                                                    null, 
                                                    new string[] 
                                                        { 
                                                            "Open in Browser".Translate() 
                                                        });
    
                actionSheet.Clicked += (o, eventArgs) =>
                    {
                        if (eventArgs.ButtonIndex >= 1) return; // ignore Cancel button
    
                        UIApplication.SharedApplication.OpenUrl(url);
                    };
    
                actionSheet.ShowFrom(shareButton, true);
            }
        }
    }
    

    `

  • VijayPatidarVijayPatidar USMember

    I just want to know How we can show our custom activity in upper row of UIActivityViewController.Here is my custom activity code

    using System;
    using MonoTouch.UIKit;
    using MonoTouch.Foundation;

    namespace CustomActivity
    {
    public class CustomActivityV:UIActivity
    {
    private NSUrl _url;
    private int flag;

        public int Flag {
            get {
                return flag;
            }
            set {
                flag = value;
            }
        }
    
    
        public static UIActivityCategory Category {
    
    
            get {
    
                return UIActivityCategory.Share;
    
            }
    
        }
        public CustomActivityV(int flag)
        {
            this.Flag = flag;
        }
        public override string Title
        {
            get
            {
                if (Flag == 0) {
                    return "Facebook";
    
                } else if (Flag == 1) {
                    return "ABCDD";
    
                } else if (Flag == 2) {
                    return "Open In Safari";
                }
    
    
                return "Safari";
            }
        }
    
    
        public override NSString Type
        {
            get
            {
                return (NSString) "com.abc.xyz";
            }
        }
    
        public override UIImage Image
        {
            get
            {
                return UIImage.FromBundle("Screenshot-iOS6.png");
            }
        }
    
        public override bool CanPerform(NSObject[] activityItems)
        {
            return true;
        }
    
        public override void Prepare(NSObject[] activityItems)
        {
            foreach (var item in activityItems)
            {
                if (item is NSUrl)
                {
                    _url = item as NSUrl;
                }
            }
        }
    
        public override void Perform()
        {
            if (_url == null) return;
    
            UIApplication.SharedApplication.OpenUrl(_url);
        }
    
    }
    

    }

  • PerHungebergMllerPerHungebergMller DKMember ✭✭✭

    @VijayPatidar‌, I am not sure what it is you want to happen with you custom activity ?

    Have you implemented the a UIActivityViewController ?

    Please note that any Custom UIActivity always gets added to the lowest row of the Sharing UIActivityViewController. This is by Apple design I think.

  • VijayPatidarVijayPatidar USMember

    @PerHungebergMøller I got the solution you have to override the

    [Export("activityCategory")] public new static UIActivityCategory Category {
    get{
    return UIActivityCategory.Share;
    }

    }
    
  • PerHungebergMllerPerHungebergMller DKMember ✭✭✭

    @VijayPatidar‌, cool Thank you for the tip :-)

Sign In or Register to comment.