Thursday, May 5, 2011

Playing Video file in Iphone


Let’s start by looking at a short video of the application. The basic idea to make this work is simple, your video content viewable in your simulator.
Step 1: Create a new project from Xcode using View-base application.
Step 2: We need to add some code in the header file.
#import <MediaPlayer/MediaPlayer.h>

#import <UIKit/UIKit.h>
@interface Tabbar1ViewController : UIViewController
{
MPMoviePlayerController *moviePlayer;
NSURL *movieURL;
}
Step 3: Now you add MediaPlayer.framework in your framework folder.Only shows up in the iPhone SDK frameworks folder, at  /Developer/platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS2.0.sdk/System/Library/Frameworks.


Step 4: In the controller file, we create the path to the movie, setup the movie player, register to receive a notification when the movie has finished playing, start the movie.

- (void) moviePlayBackDidFinish:(NSNotification*)notification 
{       
[[NSNotificationCenter defaultCenter] removeObserver:self
name:NULL
object:nil];
[movieURL release];
[moviePlayer release];
}

-(void)viewWillAppear:(BOOL)animated
{
NSString *path = [[NSBundle mainBundle] pathForResource:@"video" 
    ofType:@"mov"];      
movieURL = [NSURL fileURLWithPath:path];

moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
[moviePlayer setMovieControlMode:MPMovieControlModeHidden];  

[[NSNotificationCenter defaultCenter] addObserver:self 
selector:@selector(moviePlayBackDidFinish:) 
name:MPMoviePlayerPlaybackDidFinishNotification 
object:nil];

[moviePlayer play];
}
 
Step 5: Now compile and run your application on the simulator

iPhone SDK Tip: Firing custom events when a link is clicked in a UIWebView

Well I have been asked this a few times and I can see how it would be a very helpful addition to theUIWebView’s feature set. What I am talking about is adding the ability to intercept the event generated when a user selects a link in a webpage being displayed with a UIWebView, this will then allow you to perform any action that you want.
When would you use this I hear you say? Well one person asked me if I knew how to append the users current location onto all HTTP requests? and another just wanted to know how to perform custom actions from an embedded UIWebView.

Once again I will be extending the “Build your very own Web Browser!”, if you haven’t already completed it then I suggest that you head over there now and spend 5 minutes reading it and then downloading the project files at the end.
Start by opening up the WebBrowserTutorialAppDelegate.h file and editing the @interface line to read:

@interface WebBrowserTutorialAppDelegate : NSObject  {
 
What we have done is to make the main AppDelegate a delegate for the UIWebView as well.
Now we need to set our webView to have the main AppDelegate as its delegate, you can do this by opening up WebBrowserTutorialAppDelegate.m and putting the following line just inside theapplicationDidFinishLaunching function:

webView.delegate = self;



That is all pretty self explanatory, it just sets the delegate of our webView to self, which in this case is our main application delegate.
Now we are pretty much done, we just need to add the function to catch the link clicks. To do this we need to add a new function, copy the content below to the WebBrowserTutorialAppDelegate.m file:

- (BOOL)webView:(UIWebView*)webView 
shouldStartLoadWithRequest:(NSURLRequest*)request 
navigationType:(UIWebViewNavigationType)navigationType {
NSURL *url = request.URL;
NSString *urlString = url.absoluteString;
NSLog(urlString);
return YES;
}
 
This function will catch all requests and allow you to either manipulate them and pass them on or to perform your own custom action and stop the event from bubbling.
The first line gets the URL of the request, this is the contents inside the href attribute in the anchor tag.
The next line converts the URL to a string so we can log it out. You can access many parts of the NSURL, here are some of them and brief description of what they do.

* absoluteString – An absolute string for the URL. Creating by resolving the receiver’s string against its base.
* absoluteURL – An absolute URL that refers to the same resource as the receiver. If the receiver is already absolute, returns self.
* baseURL – The base URL of the receiver. If the receiver is an absolute URL, returns nil.
* host – The host of the URL.
* parameterString – The parameter string of the URL.
* password – The password of the URL (i.e. http://user:pass@www.test.com would return pass)
* path – Returns the path of a URL.
* port – The port number of the URL.
* query – The query string of the URL.
* relativePath – The relative path of the URL without resolving against the base URL. If the receiver is an absolute URL, this method returns the same value as path.
* relativeString – string representation of the relative portion of the URL. If the receiver is an absolute URL this method returns the same value as absoluteString.
* scheme – The resource specifier of the URL (i.e. http, https, file, ftp, etc).
* user – The user portion of the URL.

Then the third line simply logs the URL to the console, so you will new to open up the console while you run this in the simulator to see the results.
Finally the forth line returns YES, this will allow the UIWebView to follow the link, if you would just like to catch a link and stop the UIWebView from following it then simply return NO.

Installing GIT on OS-X In 3 Minutes

How to install GIT on OS-X in under 3 minutes:

curl http://kernel.org/pub/software/scm/git/git-1.7.1.tar.gz -O
tar xzvf git-1.7.1.tar.gz
cd git-1.7.1
make configure
./configure --prefix=/usr/local
NO_MSGFMT=yes make prefix=/usr/local all
sudo make install
 
Git should be installed on your OS-X now, which you can verify by issuing: git --version command in Terminal. However, it’s a good idea to also pre-configure the default username and e-mail for your Git installation:

git config --global user.name "Your Firstname Lastname"
git config --global user.email "username@example.com"
git config --global --list
 
I personally, also like to set:

git config --global color.ui "auto"

How to Setting up the GIT

Setting Up

Now that you have the pre-reqs all ready, you can proceed with the install.  Open a terminal and cd into the directory where you want to install things.  Then type the following (or just copy and paste the items below):

git clone git://github.com/sproutit/sproutcore-abbot.git abbot
cd abbot
rake init
cd ..
git clone git://github.com/sproutit/sproutcore-samples.git samples
cd samples
mkdir -p frameworks
cd frameworks
git clone git://github.com/sproutit/sproutcore.git sproutcore
cd ../..

Add the path to abbot/bin to the beginning of your PATH to be able run the abbot command-line tools (sc-init, sc-server, sc-build, sc-gen, sc-manifest, sc-docs, and more) without having to enter the full path:
export PATH=`pwd`/abbot/bin:$PATH
Make sure you have configured your github account with SSH Keys and Username/Email (Without setting up the SSH key for your machine, you will get a permission denied error when trying to initialize the Git repository)
You should now have the full set of SproutCore tools running the latest Abbot-compatible code.

How to Dismiss the Keyboard when using a UITextView

The short answer is to send the UITextViewController the “resignFirstResponder” message … the trick however is when to send the message. In my case, and I assume it would be the same for others, is to listen for any changes to the text in the UITextView and if the carridge return character ‘\n’ is detected then send the “resignFirstResponder” message to the UITextView.
Step 1. The first step is to make sure that you declare support for the UITextViewDelegate protocol. This is done in your header file, as example here is the header called EditorController.h:
@interface EditorController : UIViewController  {
UITextView *messageTextView;
}
 
@property (nonatomic, retain) UITextView *messageTextView;
 
@end
Step 2. Next you will need to register the controller as the UITextView’s delegate. Continuing from the example above, here is how I have initialize the UITextView with EditorController as the delegate …
- (id) init {
if (self = [super init]) {
// define the area and location for the UITextView
CGRect tfFrame = CGRectMake(10, 10, 300, 100);
messageTextView = [[UITextView alloc] initWithFrame:tfFrame];
// make sure that it is editable
messageTextView.editable = YES;
 
// add the controller as the delegate
messageTextView.delegate = self;
}
Step 3. And now the final piece of the puzzle is to take action in response to the “shouldCahngeTextInRange” message as follows:
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range
replacementText:(NSString *)text
{
// Any new character added is passed in as the "text" parameter
if ([text isEqualToString:@"\n"]) {
// Be sure to test for equality using the "isEqualToString" message
[textView resignFirstResponder];
 
// Return FALSE so that the final '\n' character doesn't get added
return FALSE;
}
// For any other character return TRUE so that the text gets added to the view
return TRUE;
}

How to take Screenshot in Mac


In the article you’ll learn how to take screenshot in mac os. Although it’s very easy and quite interesting work but for the new users it’s bit confusing.
But anyways…to take the screenshots in mac os press “Command button +Shift+3″ and it’ll capture & save the image on desktop.
Apart from it there are many more interesting commands to take the screenshot, let’s see below…
1. To select the area and then take the screenshot give following command
“Command button +Shift+4″
2. Press “Command+Shift+4+space” if you wanted to take the screenshot of complete window.
Foregoing commands will save the screenshot at desktop. You can also save the screenshots in to clipboard. To do that you have to press one more button “Control” with the given commands. for example …
Use “Command+ control+ Shift+ 3″ instead of simple
“Command button +Shift+3″ command.
Apart from it mac os provide a software called “Grab” to capture the images in different styles. You can find this software in : Applications->utilities->Grab

iPhone On-Screen Keyboard Rundown

As I started building the latest Consumetrics iPhone app over the weekend, I had to choose which keyboard style to use for some input fields like name, email, and so on. These fields live in a “settings table” (really just a grouped table with input fields on it), so picking keyboards in Interface Builder is out of the question, unfortunately, since I require some pretty customized cells. Naturally, I checked the documentation first, where I was confronted by this plethora of options:
typedef enum {
UIKeyboardTypeDefault,
UIKeyboardTypeASCIICapable,
UIKeyboardTypeNumbersAndPunctuation,
UIKeyboardTypeURL,
UIKeyboardTypeNumberPad,
UIKeyboardTypePhonePad,
UIKeyboardTypeNamePhonePad,
UIKeyboardTypeEmailAddress,
UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable
} UIKeyboardType;
Good grief. Seven distinct keyboard types? Bah. (Yes, 9 appear in the list, but UIKeyboardTypeDefault and UIKeyboardTypeAlphabet are just aliases for other types. Quiet you.) Some choices were obvious — my email address field will probably use UIKeyboardTypeEmailAddress, for instance — but some other choices are kind of head-scratchers. One of the fields wants a quantity, for example. Which keyboard should I use for that bad boy? UIKeyboardTypeNumberPad? But what if I need units, like “cups”? Can I get an alphabetic keyboard from UIKeyboardTypeNumberPad? I didn’t know, and tragically neither does Google; there aren’t any good breakdowns of iPhone keyboard types on the first few pages. Hitting up some blogs finally pointed me to some more Apple documentation, but even that still didn’t answer all my questions.
After not finding what I need, I did what any good developer would do: put down what I was supposed to be doing, and pick up something more interesting. I slapped together a quick iPhone app to display each keyboard type through a table view, the source for which is attached, and used it to scribble down a quick reference, which follows forthwith:
Overview



This rundown contains images of all keyboard views for all keyboard types. Each keyboard is configured exactly the same with respect to things like which “Return” button they use, so any difference you see is due purely to differences in the keyboards themselves, and not their configuration. Bearing this in mind, it turns out there are only two real keyboard layout types: the “normal” alphabet keyboard layout, and the number pad layout:
normal alphabet keyboard layout number pad keyboard layout
The first thing to realize is that even though it looks like there are seven different keyboards, there really aren’t. There are really just 2 different keyboard layouts with different keys stuck on them, and the layouts are strung together in different ways. (The alphabet keyboard does have a couple varieties with more or fewer keys, but they’re really just variations on a theme.) The particularly observant reader (or maybe just the ones who are breathing, it is blue after all) will notice one particular difference between these keyboard layouts: the “Done” button. We’ll chat more about that in a bit.
UIKeyboardTypeASCIICapable and Friend
This keyboard has three different views: alphabet, punctuation and numbers, and punctuation and symbols.
normal alphabet keyboard layout alphabet keyboard layout with numbers and punctuation alphabet keyboard layout with symbols
You alternate between the three views by pushing the “ABC,” “123,” and “#+=” buttons on each view, which is as intuitive as we’ve come to expect from Apple. This keyboard has a total of 98 characters (26 lowercase + 26 uppercase + 1 spacebar + 10 numbers + 35 punctuation), so “ASCII capable” is a great name for this keyboard, and it should serve almost any enter-text-onto-your-iPhone need that you have. (And between you and me, if you push and hold some characters, you get a list of other characters to enter, so you can actually type a lot more than 98 characters on this keyboard, just less conveniently. I should note that this tap-and-hold input method works on all keyboards, not just UIKeyboardTypeASCIICapable.)
tap-and-hold diacritic example
The next keyboard type in the list, UIKeyboardTypeNumbersAndPunctuation, is exactly the same as this keyboard, it just opens on the second (numbers) view instead of the first (alphabet) view.
As a rule, this keyboard type should be your bread-and-butter keyboard. If you don’t know which keyboard to use, use this guy; he won’t let you down. There’s nothing text-wise you can do with an on-screen iPhone keyboard you can’t do with UIKeyboardTypeASCIICapable. (If your input is number-oriented as opposed to alphabet-oriented, though, you should probably use UIKeyboardTypeNumbersAndPunctuation.)
The only real drawback to this keyboard is that it’s clunky, with three screens to navigate, doesn’t provide any really input sanitization, so your user can enter almost any character he can think of, even if it doesn’t make sense in the context of the input. What keyboard should you use to provide input-level sanitization? Like sanitization for a URL, for example?
UIKeyboardTypeURL
I’m glad you asked! UIKeyboardTypeURL is designed specifically for the purpose of inputting the most common URLs quickly and efficiently:
URL alphabet keyboard layout URL number and punctuation keyboard layout URL number and symbol keyboard layout
This keyboard is basically the UIKeyboardTypeASCIICapable keyboard with the non-URL keys removed, and the spacebar replaced with some URL-handy keys. This keyboard is ideal for entering web addresses, like http://www.sigpwned.com/. Otherwise, not much else to say here. The take-home message is just that input sanitization is A Good Thing, and you should think about it, and this keyboard offers great sanitization for URLs. (But you probably knew that from the name.)
UIKeyboardTypeEmailAddress
If an email address and the UIKeyboardTypeURL somehow got together, the UIKeyboardTypeEmailAddress would be the love child from that unholy union:
email alphabet keyboard layout email number and punctuation keyboard layout email number and symbol keyboard layout
This has exactly the same layout as UIKeyboardTypeURL, but has a few keys swapped around to suit email addresses. The only surprise here is the “space” key; evidently spaces are allowed in email addresses, but you have to quote them. Who know? Otherwise, nothing to see here. Move along.
UIKeyboardTypeNumberPad
Here’s where things start to shake up a little bit. Behold our first number pad keyboard:
numberpad keyboard
As you can see, this layout is quite different from the others. The most important difference, though, lies in this keyboard’s lack of a Done key. The Done key is the key you use to dismiss the keyboard when you’re done editing, so knowing its location is kind of important. (Without a Done key, there’s no way for the user to dismiss the keyboard, so it just sits there.) This is one of the few places where Apple has violated the “Principle of Least Surprise” by not putting the Done key in the same place it appears on other keyboards. On “doneless” keyboards like these, most developers put a Done key on the navigation bar at the top, as I did:
numberpad done key example
As I’m sure you surmised from the title, this keyboard is specifically for numbers-only input, which is perhaps less useful than you might expect. That’s why UIKeyboardTypeNumberPad brought some friends along.
UIKeyboardTypePhonePad
This keyboard is just an on-screen implementation of a conventional touch-tone phone pad. You’ll have to use the same Done key trick from above.
phone pad number layout phone pad symbol layout
This keyboard brings along the symbol characters you need for some phone numbers, which is important. Unfortunately, however, I’ve now officially run out of witty transitions. So it’s a good thing you won’t remember, because these are not the droids you’re looking for. :: waves hand ::
UIKeyboardTypeNamePhonePad
Ah, UIKeyboardTypeNamePhonePad. Of all the many, many keyboards the iPhone supports, I find this one the most confusing. It supports numbers, letters, and spaces only, with no punctuation or symbols whatsoever. (And lowercase letters only at that because the shift key is disabled!) Very strange:
name phone pad alphabet layout name phone pad numbers layout
But mine is not to question why. Mine is just… no, actually mine is to question why. WTF Apple? Seriously, I can’t even get uppercase letters on this keyboard? Odd. There is one bit of good news about this particular keyboard, though: it has both a number pad and a Done button, so you can have a number pad without having to do a bunch of navigation-bar-done-button gymnastics. At least as long as you don’t mind using a kind of gimpy keyboard, anyway.
Keyboard App Source Code
Attached to this post you’ll find a file called “iPhone Keyboard App.src.zip”. Inside you’ll find all the NIBs, source code, graphic resources, etc. for the iPhone app I used to build this post. I made it pretty quick, but I think it’s pretty clean, so hopefully it’ll be useful to someone somewhere. At the very least it should serve as a good example of how to work with the Done key on the physical keyboard via UITextFieldDelegate and textFieldShouldReturn: versus on the Navigation bar using self.navigationItem.rightBarButtonItem and UIBarButtonItem. If anyone really wants to see it, I’d be happy to write a post about the architecture of the program and how it works at a high level.
Underview
And that’s that! You’re now an expert on iPhone keyboards. Or at least what they look like. Hopefully this will help people understand the core iPhone input method better and plan around what keyboard is best to use. Remember, the most restrictive keyboard you can use is the one you should use.