【问题标题】:Xamarin iOS share buttonXamarin iOS 共享按钮
【发布时间】:2016-06-10 23:41:44
【问题描述】:

如何像原生开发一样在 Xamarin.iOS 中实现社交分享?

【问题讨论】:

    标签: c# xamarin xamarin.ios social


    【解决方案1】:

    可以这样做:

     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);
            }
    

    您共享的项目必须从 NSObjects 派生。

    您可以通过在UIActivityViewController 上设置ExcludedActivityTypes 来排除活动

    通过上面的代码,我在模拟器中得到了这个:

    【讨论】:

    • 是的,已经实现了这个进行测试,但问题是与附加图片上的共享视图相同,不仅仅是社交网络列表,它看起来很糟糕
    • 只有邮件、Twitter、阅读列表和复制?
    • 我附上了我在运行模拟器时看到的内容,它们看起来和我一样,但也许我错过了一些东西。您正在构建哪个 iOS 版本?你能发布你的测试实现代码吗?
    • 嗨!非常感谢您的回答,是的,现在它确实看起来像我需要的,只是被那个 applicationActivities 数组弄糊涂了。但是我没有看到我的任何社交网络(twitter 或 fb),我什至在设置中登录了 facebook,但仍然没有
    【解决方案2】:

    现在我在项目中使用这个组件:

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

    在按钮的 TouchUpInside 中,我调用了这样的方法:

        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();
        }
    

    其中 FacebookShareDialog 是派生类

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

    它比仅添加 FacebookShareButton 效果更好,因为您可以选择它的共享方式 - 原生或 ShareSheet 对话框模式确实比仅在浏览器中打开共享对话框更有用。

    【讨论】:

    • 嗯,我不仅需要登录facebook,还需要twitter和vk.com,希望有更简单的解决方案
    • 好吧,对于 Twitter,我们正在编写自己的 TwitterKit 绑定。幸运的是,我们现在不需要 Vkontakte。但总的来说,这是一个更难但更正确的流程,因为许多用户不会在浏览器中登录社交网络,而只是在应用程序中。
    【解决方案3】:

    【讨论】:

    • 它已经非常过时了,因为 facebook 改变了他们的 api
    • 抱歉,我的错
    【解决方案4】:

    最好的方法是使用

    https://github.com/jguertl/SharePlugin

    然后在安装包之后你可以像这样调用命令或事件:

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

    【讨论】:

    • 什么都不做:/我有这个内部按钮触摸:CrossShare.Current.Share("message","title");
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-15
    • 1970-01-01
    • 1970-01-01
    • 2015-10-14
    • 1970-01-01
    • 2017-03-16
    • 2018-05-04
    相关资源
    最近更新 更多