how to use UIActivityViewController with MonoTouch

2019-04-09 23:58发布

问题:

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:

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).