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!

Sunday, May 29, 2011

What it seems to be, but it wasn't

Some time ago, I read somewhere that Microsoft will help iOS developers to migrate their apps/games to Microsfot Windows Phone market, and for that objective Microsoft launches iOS to WP7 API Mapping Tool. I was surprised, I thought Microsoft released a tool that save us a lot of time translating our classes from Objective-C, and maybe also xib files will be translated to XAML files with only a mouse click. So a few days ago I decided to download this tool to migrate some of my apps (Tickeating and Valladolid Wifi) to WP7 in a few time but... Oh wait! There was not a "magical tool" that transalate all the code for me...

So, did Microsoft lie us??? I don't think that at all, I want to think that there was a confusion with the name, and the word "tool" will not be there. But wait a moment, Microsoft had the detail for iOS developer to map the libraries from one framework to another. There isn't a "magical tool" that saves us a lot of time, but there is a good tool that saves us some time, be positive my friends.

With this aptitude I think that Microsoft wants to attract iOS apps and game developers to migrate its applications to Windows Phone. Develop for Windows Phone with Visual Studio 2010 its easy for people who develops for iOS so its a great idea release this kind of tools, in an attemp to gain Android apps market share and obviously sell more devices.

This is the URL of iOS to WP7 API Mapping Tool: http://wp7mapping.interoperabilitybridges.com/

[iPhone-app] Valladolid WiFi

Valladolid WiFi is a smart utility application which helps the user to look for public WiFi access points located in Valladolid. From tourist to Valladolid citizens they could use the applications for find a public wifi in a pub, hotel or university and get connected.

At the moment there is only Valladolid WiFi at app store, but we are looking for people who wants to bring this application to their city all over the world. We only need that people send us the name, address and location of their business whith public WiFi and we'll put it into the application database (we need a minimum of 15 WiFi point to launch a city personalized app) and it's for FREE.

If you are interested, please contact me: fiveflamesmobile@gmail.com

Name: Valladolid WiFi
Language: Spanish
Category: Utilities
Cost: Free

Valladolid WiFi AppStore Link

Wednesday, May 25, 2011

[Code Snippet] Custom UINavigationItem title

Last week I was working in an iPhone navigation based project, the customer wanted to have its own corporative colors in the application, and for the UINavigationBar too.

There was no problem with whole color for buttons and bar, using the UINavigationBar tintColor proprerty I set the desired color, but when the UINavigationItem title time becomes it was more difficult. We have to forget the UINavigationItem title proprerty and work with titleView one. This proprerty can be fulfilled with any UIView object, in my case I used a UILabel, but I have to set a different titleView for my different UIViewControllers so I encapsulated this in the function that I make available for you.

+ (UILabel*) labelForNavigationItem:(NSString*)text
{
   
   
    UILabel *titleView = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 120, 30)] autorelease];
    [titleView setFont:[UIFont boldSystemFontOfSize:16.0]];
    titleView.backgroundColor = [UIColor clearColor];
    titleView.textAlignment = UITextAlignmentCenter;
    titleView.textColor  = [UIColor colorWithRed:.80784314 green:.77647079 blue:.50980392 alpha:1.0];
    titleView.textColor = [UIColor whiteColor];
    titleView.text = text;
   
    return titleView;
}
And assign the corresponding proprerty on viewDidLoad function

self.navigationItem.titleView = labelForNavigationItem:@"Menu";
That's all.

But at last I found a problem that I wasn't able to accomplish. I want to keep the standard UINavigationController backButton, but how could I change its font color? I think its no possible (at least without using an image), but if someone has any answer I would be grateful.

Enjoy it!