Xamarin iOS share button

独自空忆成欢 提交于 2019-12-10 22:47:07

问题


How to implement in Xamarin.iOS social sharing like in native development?


回答1:


Could do something like this:

 public override void ViewDidLoad ()
        {
            base.ViewDidLoad ();
            // Perform any additional setup after loading the view, typically from a nib.
            var button = new UIButton (UIButtonType.RoundedRect) {
                Frame = UIScreen.MainScreen.Bounds,
                BackgroundColor = UIColor.Red
            };

            button.TouchUpInside += (sender, e) => {
                var item = NSObject.FromObject ("HI");
                var activityItems = new NSObject[] { item };
                UIActivity[] applicationActivities = null;

                var activityController = new UIActivityViewController (activityItems, applicationActivities);

                PresentViewController (activityController, true, null);
            };

            Add (button);
        }

The items you share have to be derived from NSObjects.

And you can exclude activities by setting ExcludedActivityTypes on the UIActivityViewController

With the code above I get this in the simulator:




回答2:


Now in projects I use this component:

https://components.xamarin.com/view/facebook-sdk

and in button's TouchUpInside I invoke a method like this:

    void Share(string sharingUrl)
    {
        var content = new ShareLinkContent();
        content.SetContentUrl(new NSUrl(sharingUrl));

        var shareDialog = new FacebookShareDialog
                              {
                                  FromViewController = UIApplication.SharedApplication.KeyWindow.RootViewController,
                                  Mode = ShareDialogMode.Native
                              };
        shareDialog.SetShareContent(content);
        shareDialog.Show();
    }

where FacebookShareDialog is a derived class

public class FacebookShareDialog : ShareDialog
{
    public FacebookShareDialog()
        : base(NSObjectFlag.Empty)
    {
    }
}

It works better than just add a FacebookShareButton due to the fact you can choose how it shares - Native or ShareSheet dialogs modes are really more useful than just opening share dialog in browser.




回答3:


You can try with this component: https://components.xamarin.com/view/xamarin.social




回答4:


the best way is to use

https://github.com/jguertl/SharePlugin

then after installing the package you can call the command or event just like that :

public Command Share {
            get { 
                return new Command ((parameter) => {
                    CrossShare.Current.Share(((Joke)parameter).JokeTxt,"title");
                });
            }
        }


来源:https://stackoverflow.com/questions/32781742/xamarin-ios-share-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!