how to use UIActivityViewController with MonoTouch

2019-04-10 00:13发布

I am trying to access UIActivityViewController from MonoTouch with the following code:

var textToShare = new NSString("hallo there");
var activityItems = new NSObject[] {textToShare};
var excludedActivityTypes = new NSString[] { new NSString("UIActivityTypePostToWeibo"),       new NSString("UIActivityTypeMessage") };
var activityViewController = new UIActivityViewController(activityItems, null);
activityViewController.ExcludedActivityTypes = excludedActivityTypes;
this.PresentViewController(activityViewController, true, null)

The iOS action sheet is displayed but the specified activities are not excluded. Any suggestions what the problem is? Are there any MonoTouch samples for this?

1条回答
【Aperson】
2楼-- · 2019-04-10 00:51

You were close. Change this:

var excludedActivityTypes = new NSString[] { 
    new NSString("UIActivityTypePostToWeibo"),
    new NSString("UIActivityTypeMessage") 
};

into:

var excludedActivityTypes = new NSString[] {
    UIActivityType.PostToWeibo,
    UIActivityType.Message
};

Note: The name of the constant is not always identical to it's value. Also it's common (even if bad practice) for ObjC to compare the NSString pointers (not their values) to detect constant equality - creating your own NSString instance won't work in such cases (anyway the exposed const are much better looking and work better with code completion from the IDE).

查看更多
登录 后发表回答