Snippet: Sending Mail for Your App Using MFMailComposeViewController

This code snippet is a bit longer than usual but it is really useful. It can be added to a view controller to allow sending of mail to a fixed address (although the user can edit it). It could be used for sending feedback from your app.

#import <MessageUI/MessageUI.h>
// Adopt protocol: MFMailComposeViewControllerDelegate

/**
 * @brief Sends mail to a hard-coded address.
 */
- (void)sendMail
{
    if([MFMailComposeViewController canSendMail])
    {
        MFMailComposeViewController* mailer = [[MFMailComposeViewController alloc] init];
        NSString* to = [NSString stringWithFormat:@"%@@%@", @"snippets", @"iosdeveloperzone.com"];
        [mailer setToRecipients:[NSArray arrayWithObject:to]];
        [mailer setSubject:@"Snippet Feedback"];
        [mailer setMessageBody:@"Nice snippet!" isHTML:NO];
        mailer.mailComposeDelegate = self; 
        [self presentModalViewController:mailer animated:YES];
        
    }  
    else
    {
        NSLog(@"Can't send mail");
    }
}

// MARK: -
// MARK: MFMailComposeViewControllerDelegate
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
    UIViewController* parent = controller.parentViewController;
    [parent dismissModalViewControllerAnimated:YES];
    NSString* message = nil;
    switch(result)
    {
        case MFMailComposeResultCancelled:
            message = @"Not sent at user request.";
            break;
        case MFMailComposeResultSaved:
            message = @"Saved";
            break;
        case MFMailComposeResultSent:
            message = @"Sent";
            break;
        case MFMailComposeResultFailed:
            message = @"Error";
    }
    NSLog(@"%s %@", __PRETTY_FUNCTION__, message);
}
Posted in Code Snippets | Leave a comment

Adding YouTube Videos to Tutorials

The steps in some of the tutorials are quite difficult to describe with words alone. Clearly it’s much easier to understand if you also have a video showing you how to complete the step. With this in mind I have started adding short YouTube videos to some of the tutorials. Watch out for a little YouTube icons in the tutorials, like the one below.

Example of YouTube icon.

Most of the videos will be simple screen captures with no audio, but hopefully they will make things a little easier to follow. In other words, Martin Scorsese has nothing to worry about!

Posted in News | Leave a comment

Snippet: Handling Key Clicks in a Custom Keyboard

In a custom keyboard or input accessory you may want to support audio feedback or, as they say on the street, key clicks.

The code snippet below summarizes how to do this:

// Adopt the UIInputViewAudioFeedback protocol
@interface CustomKeyboardAccessoryView : UIView< UIInputViewAudioFeedback> {

}
...
@end

// Implement protocol
- (BOOL)enableInputClicksWhenVisible
{ 
    return YES;
}

// When you should click call 
[[UIDevice currentDevice] playInputClick];

Reference
Text, Web and Editing Programming Guide for iOS: Playing Input Clicks

Posted in Code Snippets | Tagged , , | Leave a comment