0

I am setting up my app with a few UIActivity Classes. The flow is that the user views an article, and clicks action button to pull up an UIActivityViewController.

- (void)prepareWithActivityItems:(NSArray *)activityItems
{
    UIAlertView* dialog = [[UIAlertView alloc] init];
    [dialog setDelegate:self];
    [dialog setTitle:@"Enter Password"];
    [dialog setMessage:@" "];
    [dialog addButtonWithTitle:@"Cancel"];
    [dialog addButtonWithTitle:@"OK"];
    dialog.tag = 5;

    dialog.alertViewStyle = UIAlertViewStylePlainTextInput;
    [dialog textFieldAtIndex:0].keyboardType = UIKeyboardTypeAlphabet;
    [dialog textFieldAtIndex:0].secureTextEntry = YES;

    CGAffineTransform moveUp = CGAffineTransformMakeTranslation(0.0, 0.0);
    [dialog setTransform: moveUp];
    [dialog show];
    [dialog release];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {

    if (buttonIndex == 0) {
    }

    if (buttonIndex == 1) {
        UITextField *passwordField = [alertView textFieldAtIndex:0];
        if ([passwordField.text isEqualToString:@"password"]) {
            [self composetheemail];
        }
        else
        {
            UIAlertView *oops = [[UIAlertView alloc]initWithTitle:@"Whoops" message:@"That password is incorrect.  Please try again." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
            [oops show];
            [oops release];
        }
    }
}

-(void)composetheemail {

    self.picker = [[MFMailComposeViewController alloc] init];
    self.picker.mailComposeDelegate = self;

    [self.picker setSubject:@"App Test"];

    // Set up recipients
    NSArray *churchemail = [NSArray arrayWithObject:@"email@gmail.com"];
    [self.picker setToRecipients:churchemail];
    // Fill out the email body text
    NSString *emailBody = @"Sent from the App";
    [self.picker setMessageBody:emailBody isHTML:NO];

    [picker release];
}

However, the method to compose the email never gets called. What am I doing wrong here?

modusCell
  • 13,151
  • 9
  • 53
  • 80
user717452
  • 33
  • 14
  • 73
  • 149
  • @mehulpatel I have done that. I put in NSLogs in the `clickedButtonAtIndex` and also the `composetheemail` method. It displays the log in console, but doesn't actually pull up the composeviewcontroller. I believe this is due to the UIActivity and how it is setup with the `- (UIViewController *)activityViewController { NSLog(@"TEST3"); return self.picker; }` part – user717452 Aug 13 '14 at 20:34
  • Remember @mehulpatel this is a UIActivity class not a UIViewController. – user717452 Aug 13 '14 at 20:48

1 Answers1

0

Your view controller needs to explicitly conform to the UIAlertViewDelegate protocol to have - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex be called.

In your view controller's @interface (usually in the .h file), you need to include the delegate like so:

@interface MyViewController : UIViewController <UIAlertViewDelegate>
sgonzalez
  • 1,086
  • 9
  • 24
  • I have done that. I put in NSLogs in the `clickedButtonAtIndex` and also the `composetheemail` method. It displays the log in console, but doesn't actually pull up the composeviewcontroller. I believe this is due to the UIActivity and how it is setup with the `- (UIViewController *)activityViewController { NSLog(@"TEST3"); return self.picker; } ` part – user717452 Aug 13 '14 at 20:33
  • Remember this is a UIActivity class, not a UIViewController. – user717452 Aug 13 '14 at 20:56
  • I imagine that you have a ```UIActivityViewController``` with your ```UIActivity```. You could subclass ```UIActivityViewController``` or make a category on it that conforms to the alert view delegate protocol. – sgonzalez Aug 14 '14 at 14:08