Pages

Wednesday, June 22, 2011

[Code Snippet] Playing video streaming in our iPhone App

Hi there. Today's code snippet is another easy one, just for playing video streaming embedded in our app using Media Player Framework.

First thing to do is to import the headers that we need

#import <MediaPlayer/MediaPlayer.h>

After that we'll have to instantiate the MPMoviePlayerController and add it as a subview to our actual view. We could set the MPMoviePlayer controller frame, to get a full screen player or not.


MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:
                                            [NSURL URLWithString:@"http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8"]];
   
    [player.view setFrame: self.view.bounds];  // player's frame must match parent's
    [self.view addSubview: player.view];
   
    [player prepareToPlay];
    [player play];

Two things to take care:

First one: Take care about the supported stream formats for iOS, if the video streaming is not played maybe because the streaming is not transmitted in the correct formats. Review the stream formats and the best practices for iOS streaming here

Second one: Video player is another control yet, so you can add controls onto it like UILabels, UIButtons and make your streaming a bit interactive for the user. Just add the control with addView method and you can use it.

I hope this code snippet could be useful for you. Now I'm inmersed into interoperability between Microsoft Azure cloud computing framework and iPhone so it's possible that in a few weeks there are some articles about that.

Wednesday, June 15, 2011

[Code Snippet] Implementing UIActionSheets

This week code snippet is about how to implement UIActionSheet in our UIViewController to present actions like a context menu. Like my previous post, this is so easy but have it previously implemented could save us some valious time.

To show the UIActionSheet, instance UIActionSheet and show it with this piece of code.

UIActionSheet *popupQuery = [[UIActionSheet alloc] initWithTitle:@"Share" delegate:self cancelButtonTitle:@"Cancelar" destructiveButtonTitle:nil otherButtonTitles:@"Facebook", @"Twiter", @"E-mail", nil];
        popupQuery.actionSheetStyle = UIActionSheetStyleBlackOpaque;
   
    [popupQuery showInView:self.view];
    [popupQuery release];

Cancel button will hide our action sheet when is pushed, destructive button will appear in red background and any other button will appears as normal. Both, destructive and the other buttons behaviour has to be declared implementing UIActionSheetDelegate clickedButtonAtIndex.


-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
     if (buttonIndex == 0) {
       
         NSLog(@"Button Facebook clicked");

    } else if (buttonIndex == 1) {
       
        NSLog(@"Button Twitter clicked");;

    } else if (buttonIndex == 2) {

        NSLog(@"Button Email clicked");
       
    }
}

With this piece of code you can implement a context menu like behaviour in your view. Take note that you have not to implement cancel button index click behaviour.





I hope this code snippet helps you, enjoy it!!!

Wednesday, June 8, 2011

[Code Snippet] Sending emails from our app with MessageUI framework

Hello everybody, I've been very busy last days, too much work, so this week I'll bring you an easy code snippet that everybody should know but could be useful to have it here for cut & past simply.

When we want to use the iOS' email composer from our app, first thing to do is to link MessageUI framework with our binaries, and obviously import it in our header file.

#import <MessageUI/MessageUI.h>

For using it, you have to implement in your class the MFMailComposeViewControllerDelegate

@interface MyViewController : UIViewController <MFMailComposeViewControllerDelegate>

After that, to invoke the MFMailComposer is so easy, just instance it and preload all the email fields that you want: To, cc, bcc, subject... and present it as a modal view controller. Don't forget setting the mailComposeDelegate property to the class which implements MFMailComposeViewControllerDelegate, usually the same class that invokes MFMailComposeViewController.:

    MFMailComposeViewController *mailView = [[MFMailComposeViewController alloc] init];
    mailView.mailComposeDelegate = self;
    mailView.navigationBar.tintColor = [UIColor colorWithRed:0.36078431 green:.015686275 blue:.0078431373 alpha:1];
    [mailView setToRecipients:[NSArray arrayWithObject:@"fiveflamesmobile.blogspot.com"]];
   
    [self presentModalViewController:mailView animated:YES];
   
    [mailView release];

After those steps we have to implement de MFMailComposrDelegateProtocol as this:

#pragma mark -
#pragma mark MFMailComposeViewControllerDelegate

- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
    [self dismissModalViewControllerAnimated:YES];
   
    UIAlertView *alert = nil;
   
    if (result == MFMailComposeResultSent)
    {
        alert = [[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"EmailEnviado", "") message:NSLocalizedString(@"EmailExito", "") delegate:self cancelButtonTitle:NSLocalizedString(@"Aceptar", "") otherButtonTitles:nil] autorelease];
       
    } else if (result == MFMailComposeResultSaved) {
       
        alert = [[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"EmailGuardado", "") message:NSLocalizedString(@"EmailBorradores", "") delegate:self cancelButtonTitle:NSLocalizedString(@"Aceptar", "") otherButtonTitles:nil] autorelease];
       
    } else if (result == MFMailComposeResultFailed) {
       
        alert = [[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"EmailNoEnviado", "") message:NSLocalizedString(@"EmailFallo", "") delegate:self cancelButtonTitle:NSLocalizedString(@"Aceptar", "") otherButtonTitles:nil] autorelease];
    }
   
    [alert show];
}

Be careful, I've user localizated string to show the message in the alert view, but maybe I'll write about that in next code snippets. And with this few lines of code you will be able to send emails with the iOS mail client.

So that's all for today. More and better (I'll try it) the next week.

Wednesday, June 1, 2011

[Code Snippet] When keyboard shows...

Sometimes we have a lot of UITextField controls in our UIView, but what happends when the keyboard is shown??? The UITextFields that are at the bottom of the UIView are hidden behind the keyboard and users don't know what they're writing.

Today's code snipett tries to help the developer to avoid this issue. What we shall do is to move the UIView up when keyboard shows and move it down again when keyboards hides. To accomplish that we have tu use observers,  and implement the two methods that will be called when keyboards shows and hides.

First, we have to add the observers to NSNotificationCenter default center for the events of showing and hiding the keyboard Implements this in viewDidLoad method.

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
 After that we implement both methods. We have to calculate the height at our hidden UITextField is, for adding or substract it from view's frame origin in order to move the UIView up and down. If we have more than one UITextField controls we shall to detect which is the first responder and sets the position accord to it.

- (void) keyboardWillShow: (NSNotification*) aNotification;
{   
    int position = 0;
   
   
    if ([textField1 isFirstResponder])
        position = 100;
    else if ([textField2 isFirstResponder])
        position = 150;
           
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];
    CGRect rect = [[self view] frame];
    rect.origin.y -= position;
    [[self view] setFrame: rect];
    [UIView commitAnimations];
   
}

- (void) keyboardWillHide: (NSNotification*) aNotification;
{
    int position = 0;
   
   
    if ([textField1 isFirstResponder])
        position = 100;
    else if ([textField2 isFirstResponder])
        position = 150;
   
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];
    CGRect rect = [[self view] frame];
    rect.origin.y += position;
    [[self view] setFrame: rect];
    [UIView commitAnimations];
}

And with this code snippet you will have a professional application that provides a good user experience and an animated interface when editing.

Enjoy it!