如何使用Xamarin ios在instagram中共享文本和链接?

时间:2019-05-13 04:58:25

标签: xamarin.forms xamarin.ios instagram

我想共享文本并从我的应用程序链接到Instagram。我尝试使用此链接来实现此目的:-https://xamarinhelp.com/share-dialog-xamarin-forms/。但是在共享选项中,instagram没有显示。

请帮助我。

  public class Share : IShare
   {     
       string _path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
       string _fileName = "image.png";
        // MUST BE CALLED FROM THE UI THREAD
   public async Task Show(string title, string message, string filePath)
    {
var items = new NSObject[] { NSObject.FromObject(title), NSUrl.FromFilename(Path.Combine(_path , _fileName )) };
var activityController = new UIActivityViewController(items, null);
var vc = GetVisibleViewController();

NSString[] excludedActivityTypes = null;

if (excludedActivityTypes != null && excludedActivityTypes.Length > 0)
    activityController.ExcludedActivityTypes = excludedActivityTypes;

if (UIDevice.CurrentDevice.CheckSystemVersion(8, 0))
{
    if (activityController.PopoverPresentationController != null)
    {
        activityController.PopoverPresentationController.SourceView = vc.View;
    }
}
await vc.PresentViewControllerAsync(activityController, true);      
}

UIViewController GetVisibleViewController()
{
    var rootController = UIApplication.SharedApplication.KeyWindow.RootViewController;

if (rootController.PresentedViewController == null)
    return rootController;

if (rootController.PresentedViewController is UINavigationController)
{
    return ((UINavigationController)rootController.PresentedViewController).TopViewController;
}

if (rootController.PresentedViewController is UITabBarController)
{
        return ((UITabBarController)rootController.PresentedViewController).SelectedViewController;
}

return rootController.PresentedViewController;
}

}

1 个答案:

答案 0 :(得分:0)

请注意filePath是嵌入在您的iOS项目中的图像名称,并确保其构建动作为BundleResource

然后,当您单击共享按钮以打开UIActivityViewController窗口时。通过单击“更多”按钮找到Instagram应用:

enter image description here

打开Instagram的开关。您也可以调整顺序。最后,您可以成功地将项目共享到Instagram。