Question

i have no idea why the uiactivityviewcontroller crash when i use the prensentViewController method.

it is weird, anyone has any clue?

the program is running fine not untile when i use the presenviewcontroller.

#import "ActivityViewController.h"

@interface ActivityViewController ()

@end

@implementation ActivityViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    [self createTextField];
    [self createButton];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void) createTextField
{
    self.myTextField = [[UITextField alloc] initWithFrame:CGRectMake(20.0f, 35.0f, 280.0f, 30.0f)];
    self.myTextField.translatesAutoresizingMaskIntoConstraints = NO;
    self.myTextField.borderStyle = UITextBorderStyleRoundedRect;
    self.myTextField.placeholder =@"Enter text to Share";
    self.myTextField.delegate = self;
    [self.view addSubview:self.myTextField];
}

- (void) createButton
{
    self.myButtonShare = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    self.myButtonShare.frame = CGRectMake(20.0f, 80.0f, 280.0f, 40.0f);
    self.myButtonShare.translatesAutoresizingMaskIntoConstraints = NO;


    [self.myButtonShare setTitle:@"Share" forState:UIControlStateNormal];
    [self.myButtonShare addTarget:self action:@selector(handleShare:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:self.myButtonShare];



}

- (void) handleShare:(id)sender
{

    NSArray *activities = @[UIActivityTypeMail,UIActivityTypeMessage];

    self.myActivityViewController = [[UIActivityViewController alloc] initWithActivityItems:@[self.myTextField.text] applicationActivities:activities];

    [self presentViewController:self.myActivityViewController animated:YES completion:nil];

}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self.myButtonShare resignFirstResponder];
    return YES;
}
Was it helpful?

Solution

The documentation for UIActivityViewController's initWithActivityItems:applicationActivities: states that the NSArray passed as the second parameter (applicationActivities) should be an "array of UIActivity". You are passing in an array containing the two objects UIActivityTypeMail and UIActivityTypeMessage which are of type NSString *const. It seems that you were hoping that you could restrict the share sheet to only show the mail and messages activity, which is currently not possible.

To stop the crash from happening, change your code to:

<...>
self.myActivityViewController = [[UIActivityViewController alloc] initWithActivityItems:@[self.myTextField.text] applicationActivities:nil];
<...>

OTHER TIPS

What ccjensen said is all correct, except you can actually restrict your share sheet to just Mail & Message by excluding activities, this way for example:

activityController.excludedActivityTypes = @[ UIActivityTypeCopyToPasteboard, UIActivityTypeAddToReadingList ];

This would only show the email sharing by default.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top