Hello World with Navigation Base application in iPhone

This is the very simple example. In this example we will see how to Hello iPhone display using navigation base application.

Step 1: Create a Navigation Base application using template. Give the application name “HelloWorld_NavigationBase”.

Step 2: Xcode automatically creates the directory structure and adds essential frameworks to it. You can explore the directory structure to check out the content of the directory.

Step 3: Xpand classes and notice Interface Builder created the RootViewController class for you (See the figure below). Expand Resources and notice the template generated a separate nib,RootViewController.xib, for the “HelloWorld_NavigationBase”.

Step 4: Open the RootViewController.m file, for this example we need to add only two lines of code in this file . So make the changes.

(void)viewDidLoad {
    [super viewDidLoad];

// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
     self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

  (UITableViewCell *)tableView:(UITableView *)tableView   cellForRowAtIndexPath:(NSIndexPath *)indexPath {

   static NSString *CellIdentifier = @"Cell";

   UITableViewCell *cell = [tableView  dequeueReusableCellWithIdentifier:CellIdentifier];
  if (cell == nil) {
   cell = [[[UITableViewCell alloc]   initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]    autorelease];
}

  // Configure the cell. // added below two line
    [[cell textLabel] setTextAlignment:UITextAlignmentCenter];
    [cell.textLabel setText:@"Hello iPhone!"];

   return cell;
}

Step 5: Now compile and run the application in the Simulator.

You can Download SourceCode from here HelloWorld_NavigationBase

ViewBase application in iPad

This is the simple View Base application. In this application we will display image,  button and label.

Step 1: Create a View base application using template. Give the application name “ViewBase_iPad”.

Step 2: Xcode automatically creates the directory structure and adds essential frameworks to it. You can explore the directory structure to check out the content of the directory.

Step 3: Xpand classes and notice Interface Builder created the ViewBase_iPadViewController class for you. Expand Resources and notice the template generated a separate nib,ViewBase_iPadViewController.xib, for the “ViewBase_iPad”.

Step 4: In the ViewBase_iPadViewController.h file, we have created instance of UILabel and UIImage class. Define one IBAction method. So make the following changes.

#import <UIKit/UIKit.h>

@interface ViewBase_iPadViewController : UIViewController {
       
 IBOutlet UILabel *myLabel;
 IBOutlet UIImage *myImage;

}

@property(nonatomic,retain) IBOutlet UILabel *myLabel;
@property(nonatomic,retain) IBOutlet UIImage *myImage;

(IBAction)ButtonPressed:(id)sender;

Step 5: Double click the ViewBase_iPadViewController.xib file and open it to the Interface Builder.First drag the Image View from the library and place it to the View window. Next drag label and Round Rect from the library and place it to the view window. Select the Round Rect from the view window and bring up Connection iNspector. Drag from the Touch Up Inside to the File’s Owner icon and select ButtonPressed: method. Connect File’s Owner icon to the View icon and select view and connect File’s Owner icon to the label select myLabel. Now save the ViewBase_iPadViewController.xib file, close it and go back to the Xcode.

Step 6: Open the ViewBase_iPadViewController.m file and make the following changes in the file.

(IBAction)ButtonPressed:(id)sender{

myLabel.text = @"Welocome to the Real World!!!";
}

Step 7: Now compile and run the application in the Simulator.

You can Download SourceCode from here ViewBase_iPad

Download Apple WWDC 2010 Session Videos and Source Code

This might be a bit late, but just in case you missed this one and did not get the email from Apple – please do have a look! You don’t need to purchase the DVDs if you missed this years Apple WWDC session. To access them, all you need is a registered Apple developer account from – http://developer.apple.com/videos/wwdc/2010/. Grab them in both low and high definition, and if you don’t have an account ask your friendly neighbor or get on Youtube or some other video sharing sites to see what is uploaded there :)

Here is the link – Must Watch WWDC 2010 Keynotes!

Mobile Report by Distimo, June 2010 – A Must Read for Business Intelligence Data

This report covers the Apple App Store for iPad, the Apple App Store for iPhone, BlackBerry App World (Worldwide), Google Android Market, Nokia Ovi Store, Palm App Catalog and Windows Marketplace for Mobile for May 2010 in the United States.

The major findings are:

More than 50% of applications are priced below or equal to $2.00 in all stores, with the exception of BlackBerry App World and Windows Marketplace for Mobile.

The average price of all paid applications and the 100 most popular paid applications in the Apple App Store for iPad ($4.65) is higher than in the Apple App Store for iPhone ($4.01). However, the average price of the 100 top grossing applications is higher on the Apple App Store for iPhone.

Google Android Market has the largest share of free applications (57%) and Windows Marketplace for Mobile has the smallest (22%).

Twitter, Inc. published their native application in the Apple App Store and Google Android Market, becoming the monthly number 10 free application in the Apple App Store for iPhone, and the number 6 free application in Google Android Market.

Nine out of the ten most popular free applications and eight out of ten most popular paid applications in the Apple App Store for iPhone are games.

In the competitive e-reader market, both the iBooks application by Apple, Inc., and the Kindle application by Amazon.com are ranked among the 10 most popular free applications in the Apple App Store for iPad. Apple’s application is ranked number one however, and Amazon’s application is ranked number ten.

Five out of the ten most popular free applications in Windows Marketplace for Mobile are published by Microsoft Corporation.

You may download the entire report for free here!

Display WebPage and AudioFile play in TabBar Application

This is the TabBar application. In this application we will see how to display Web page and play audio file in the iPad. We will create this application using TabBar Application Template.

Step 1: Create a TabBar application using template. Give the application name “Tabbar_Videoplay_iPad”.

Step 2: code automatically creates the directory structure and adds essential frameworks to it. You can explore the directory structure to check out the content of the directory.

Step 3: Xpand classes and notice Interface Builder created the TabBarApplication_iPadViewController class for you. Expand Resources and notice the template generated a separate nib,TabBarApplication_iPadViewController.xib, for the “TabBarApplication_iPad”.

Step 4: We need to add UIViewController class in the project . Select classes -> Add -> New Files -> Select UIViewController class and give the class name “AudioPlayViewController”. Select corresponding .xib file and targeted for iPad.

Step 5: We have added AudioToolbox and AVFoundation framework in the Frameworks folder.

Step 6: We need to add two music file in the resource folder. Give the name of the music file “sound.aif”,”music.mp3″.

Step 7: In the FirstViewController.h file we have created instance of UIWebView class . So make the following changes in the file.

#import <UIKit/UIKit.h>

@interface FirstViewController : UIViewController {
  IBOutlet UIWebView *webDisplay;
}
@property(nonatomic,retain) UIWebView *webDisplay;

Step 8: Double click the MainWindow.xib file and open it to the Interface Builder. Select the FirstViewController from the TabBar Controller and bring up Attribute Inspector and delete the NIB name . Now drag the WebView from the library and place it to the view window. Select the first tab from the view window and bring up Connection Inspector and connect webDisplay to the Web View. Save the MainWindow.xib file and close it and go back to the Xcode.

Step 9: Open the FirstViewController.m file and make the following changes in the file.

(void)viewDidLoad {
       
        NSString *urlAddress = @"http://www.google.com";
        NSURL *url = [NSURL URLWithString:urlAddress];
        NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
        [webDisplay loadRequest:requestObj];
       
    [super viewDidLoad];
}

Step 10: In the AudioViewController.h file , we have import AudioToolbox and AVFoundation framework. Create an instance of  AVAudioPlayer and UIButton class. Define two IBAction method. So make the following changes in the file.

#import <UIKit/UIKit.h>
#import <AudioToolbox/AudioToolbox.h>
#import <AVFoundation/AVFoundation.h>

@interface AudioPlayViewController : UIViewController  <AVAudioPlayerDelegate> {

        SystemSoundID systemSoundID;
        AVAudioPlayer *player;
        UIButton *StartStopSound;
       
}

@property (nonatomic, retain) IBOutlet AVAudioPlayer *player;
@property (nonatomic, retain) IBOutlet UIButton *StartStopSound;

(IBAction) playSound: (id) sender;
(IBAction) playSong: (id) sender;

Step 11: Double click the AudioPlayViewController.xib file and open it to the view window. First drag the two Round Rect from the library and place it to the view window and give the name “Play Sound” , “Play Song”. Select the  ”Play Sound” button and bring up connection Inspector and drag Touch Up Inside to the File Owner icon select playSound: action. Do the same thing for the “Play Song”  button and select the playSong: action. Connect File’s Owner icon to the “Play Song” button and select StartStopSound. Now save the AudioPlayViewController.xib file, close it and go back to the Xcode.

Step 12: Open the AudioPlayViewController.m file and make the following changes:

(void)viewDidLoad {
        NSLog(@"InView did load");
        AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:[[NSBundle mainBundle]
                                                                                                                                           pathForResource: @"sound" ofType:@"aif"]],
                                                                         &systemSoundID);
   
       
        player = [[AVAudioPlayer alloc]
                          initWithContentsOfURL:[NSURL fileURLWithPath:
                                                                         [[NSBundle mainBundle] pathForResource:@"music" ofType:@"mp3"]]
                          error:nil];
       
       
        [player prepareToPlay];
       
       
       
}

(IBAction) playSound:(id) sender {
       
        NSLog(@"In Sample");
        AudioServicesPlaySystemSound(systemSoundID);
}

(IBAction) playSong:(id) sender {
       
        if ([[NSString stringWithFormat:@"%@", [StartStopSound titleForState:UIControlStateNormal]] isEqualToString:@"Play Song"]) {   
                [player play]
                [StartStopSound setTitle:@"Stop Song" forState:UIControlStateNormal];   }
        else { 
                [player stop]
                [StartStopSound setTitle:@"Play Song" forState:UIControlStateNormal];  
        }      
}

Step 13: Double click the MainWindow.xib file and open it to the Interface Builder. Select the ViewController from the TabBar Controller  in MainWindow and bring up Identity Inspector and change the class name into AudioPlayViewController and bring up Attribute Inspector, set the NIB name into “AudioPlayViewController”. Now save the MainWindow.xib file, close it and go back to the Xcode.

Step 14: Now Compile the application and run it in the Simulator.

You can Download SourceCode from here Tabbar_Videoplay_iPad

Mail Send from iPad

This is the very simple application. In this application we will see how to mail send from the iPad.

Step 1: Create a View base application using template. Give the application name “MailComposer”.

Step 2: Xcode automatically creates the directory structure and adds essential frameworks to it. You can explore the directory structure to check out the content of the directory.

Step 3: Expand classes and notice Interface Builder created the MailComposerViewController class for you. Expand Resources and notice the template generated a separate nib, MailComposerViewController.xib, for the “MailComposer”.

Step 4: We need to add MessageUI Framework. Select Frameworks folder ->Add -> Existing Framework -> Add MessageUI Framework.

Step 5: In the MailComposerViewController.h file , we have import MessageUI framework. Create an instance of UIButton class and add one buttonPressed method. So make the following changes in the file.

#import <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h>

@interface MailComposerViewController : UIViewController
<MFMailComposeViewControllerDelegate>
{
        IBOutlet UIButton *button;
}

(IBAction)buttonPressed;

Step 6: Double click MailComposerViewController.xib file and open it to the Interface Builder. First drag the Round Rect button from the library and place it to the view window. Connect File’s Owner icon to the View icon and select view. Drag File’s Owner icon to the round Rect button and select button and select the Round Rect button and bring up Connection Inspector next drag Touch Up Inside to the File’s Owner icon and select buttonPressed: action. Now save the MailComposerViewController.xib file, close it and go back to the Xcode.

Step 7: Open the MailComposerViewController.m file and make the following changes in the file.

(void)viewDidLoad {
        if ([MFMailComposeViewController canSendMail])
                button.enabled = YES;
}

(IBAction)buttonPressed {
        MFMailComposeViewController *mailController = [[MFMailComposeViewController alloc] init];
        mailController.mailComposeDelegate = self;
        [mailController setSubject:@"Hello World"];
        [mailController setMessageBody:@"This is the MailSend Application…." isHTML:NO];
        [self presentModalViewController:mailController animated:YES];
        [mailController release];
}

(void)mailComposeController:(MFMailComposeViewController*)mailController didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
        [self becomeFirstResponder];
        [self dismissModalViewControllerAnimated:YES];
}

Step 8: Now Compile and run the application in the Simulator.

You can Download SourceCode from here MailComposer

Image change using touch function in iPhone

In this application we will see how to images change using touch function.

Step 1: Create a Window base application using template. Give the application name “MovingBall12”.

Step 2 : Xcode automatically creates the directory structure and adds essential frameworks to it. You can explore the directory structure to check out the content of the directory.

Step 3: We need to add UIView class i the project. Select Classes -> Add -> New File -> Cocoa Touch Class -> Objective C class -> select UIView from the Subclass of. Give the file name “BallView”.

Step 4: We have added two resources in the Resources folder. Give the name of the resource “1.png”,”2.png”.

Step 5: In the MovingBall12AppDeleagte.h file, we have added BallView class, NSTimer , so make the following changes in the file.

#import <UIKit/UIKit.h>

@class BallView;

@interface MovingBall12AppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow *window;
        BallView *ballView;
        int count;
        NSTimer *ViewTimer;
       
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet BallView *ballView;
@property (nonatomic, retain) IBOutlet NSTimer *ViewTimer;

Step 6: Open the BallView.h file, we have added UIImageView, UILabel, NSTimer, so make the following chnages:

#import <UIKit/UIKit.h>

@interface BallView : UIView {
       
        NSTimer *ViewTimer;
        UIImageView *theBall;
        UILabel *touchMe;
        CGRect touchMeFrame;
        int count,switcher;
        int directionX, directionY;
        BOOL gameRunning;
        NSArray *images;
}

@property(nonatomic,retain) NSTimer *ViewTimer;
@property(nonatomic,retain) IBOutlet UIImageView *theBall;
@property(nonatomic,retain) IBOutlet UILabel *touchMe;
@property(nonatomic,retain) NSArray *images;

@property CGRect touchMeFrame;
@property int directionX, directionY;
@property BOOL gameRunning;

Step 7: Double click the MainWindow,xib file and open it to the Interface Builder. Double click the window icon from the main window and open it , first drag the View from the library and place it to the window. Next drag label and ImageView from the library and place it to the view window. Select the View from the main window and bring up Identity Inspector and change the class name into BallView. Connect BallView to the label and select touchMe,and connect BallView to the ImageView and select the theBall (See the figure below). Now save MainWindow.xib file, close it and go back to the Xcode.

Step 8: Open the BallView.m fie make the following changes in the file.

(void)onTimer {
        CGPoint center = theBall.center;
       
        float newX = center.x + directionX;
        float newY = center.y + directionY;
       
        int rndX = rand() % 3 + 1;
        int rndY = rand() % 3 + 1;
       
        int screenX = self.frame.origin.x + self.frame.size.width;
        int screenY = self.frame.origin.y + self.frame.size.height;
       
        BOOL changed = NO;
        if (newX < 0) {
                directionX = rndX;
                newX = 0;
                changed = YES;
        } else if (newX > screenX) {
                directionX = rndX;
                newX = screenX;
                changed = YES;
        }
       
        if (newY < 0) {
                directionY = rndY;
                newY = 0;
                changed = YES;
        } else if (newY > screenY) {
                directionY = rndY;
                newY = screenY;
                changed = YES;
        }
       
        if ((rand() % 1000) > 997) {
                directionY *= 1;
                changed = YES;
        }
       
        if ((rand() % 1000) > 997) {
                directionX *= 1;
                changed = YES;
        }
       
        if (changed == YES) {
                int img = switcher++ % (int)[images count];
                theBall.image = [images objectAtIndex:img];
               
        }
       
        CGPoint newCenter = CGPointMake(newX, newY);
        [theBall setCenter:newCenter];
       
}

(void)toggleTimer:(int)withCount {
        if (withCount % 2 != 0) {
                [ViewTimer invalidate];
                touchMe.backgroundColor = [UIColor whiteColor];
                touchMe.textColor = [UIColor blackColor];
               
                               
                gameRunning = NO;
               
        } else {
                ViewTimer = [NSTimer scheduledTimerWithTimeInterval:0.0075 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];  
                touchMe.backgroundColor = [UIColor blackColor];
                touchMe.textColor = [UIColor whiteColor];
               
                gameRunning = YES;
        }
}

(void)doInitialSetup {
        sranddev();
       
        count = 0;
       
        int rndX = rand() % 3 + 1;
        int rndY = rand() % 3 + 1;
       
        directionX = rndX;
        directionY = rndY;
       
        UIImage *img1 = [UIImage imageNamed:@"1.png"];
        UIImage *img2 = [UIImage imageNamed:@"2.png"];
       
        self.images = [NSArray arrayWithObjects:img1, img2, nil];
       
        int img = (int)count % (int)[images count];
        theBall.image = [images objectAtIndex:img];
       
        gameRunning = NO;
}

(BOOL) isPointInTouchMe:(CGPoint)point {
        return (point.x >= touchMe.frame.origin.x
                        && point.x <= touchMe.frame.origin.x + touchMe.frame.size.width
                        && point.y >= touchMe.frame.origin.y
                        && point.y <= touchMe.frame.origin.y + touchMe.frame.size.height);
}

(BOOL) isPointInTheBall:(CGPoint)point {
        return (point.x >= theBall.frame.origin.x
                        && point.x <= theBall.frame.origin.x + theBall.frame.size.width
                        && point.y >= theBall.frame.origin.y
                        && point.y <= theBall.frame.origin.y + theBall.frame.size.height);
}

(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
        UITouch *touch = [touches anyObject];
        CGPoint point = [touch locationInView:self];
       
        if ([self isPointInTouchMe:point]) {
                [self toggleTimer:count++];
        }
       
}
(void) awakeFromNib {
        [self doInitialSetup];
}

(id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
                [self doInitialSetup];
    }
    return self;
}

(void)drawRect:(CGRect)rect {
        [super drawRect:rect];
}

Step 9: Now compile and run the application on the Simulator.

You can Download SourceCode from here MovingBall12

That Flash on the iPhone

I am not quite sure how often Flash has been discussed on the iPhone and how often we have seen prototypes running on it. Given the development of HTML5 as well as the memory and battery issues that come with Flash, Flash is turning out to be a nice-to-have feature and not so much a must have. But if you really must run Flash on an iPhone, now you can, thanks to Frash that runs on jailbroken iPhones. We haven’t tested it yet, but we thought you would like to know. If you run it, keep in mind that Comex says that Frash is not what you would describe as stable.

Of course, there is now an interesting legal situation that legalizes jailbreaking, even if such a move still voids the warranty of the iPhone. We doubt that Flash will become a major deal on the iPhoen, but it is worth watching whether this story develops.

Apple is unlikely to change its opinion and if you forget Mark Papermaster’s departure from the company, it is rather quiet about the phone lately. Despite the success of Android, Apple appears to be still selling every iPhone it can make and the Apple Store still says that there is a delay between your order and actual delivery. Sterne Agee analyst Vijay Rakesh this morning told investors that Apple is likely to address this situation, especially since the iPhone 4 is making its way to a total of 88 countries by the end of September. Q3 orders have been increased to more than 10 million, according to Rakesh, which would translate into a 20% production increase over Q2.

We should see data and app use still increase over the next months. For those of you who are keeping track of the numbers, Google recently said that Android is selling some 6 million phones every month now.

That Flash on the iPhone

I am not quite sure how often Flash has been discussed on the iPhone and how often we have seen prototypes running on it. Given the development of HTML5 as well as the memory and battery issues that come with Flash, Flash is turning out to be a nice-to-have feature and not so much a must have. But if you really must run Flash on an iPhone, now you can, thanks to Frash that runs on jailbroken iPhones. We haven’t tested it yet, but we thought you would like to know. If you run it, keep in mind that Comex says that Frash is not what you would describe as stable.

Of course, there is now an interesting legal situation that legalizes jailbreaking, even if such a move still voids the warranty of the iPhone. We doubt that Flash will become a major deal on the iPhoen, but it is worth watching whether this story develops.

Apple is unlikely to change its opinion and if you forget Mark Papermaster’s departure from the company, it is rather quiet about the phone lately. Despite the success of Android, Apple appears to be still selling every iPhone it can make and the Apple Store still says that there is a delay between your order and actual delivery. Sterne Agee analyst Vijay Rakesh this morning told investors that Apple is likely to address this situation, especially since the iPhone 4 is making its way to a total of 88 countries by the end of September. Q3 orders have been increased to more than 10 million, according to Rakesh, which would translate into a 20% production increase over Q2.

We should see data and app use still increase over the next months. For those of you who are keeping track of the numbers, Google recently said that Android is selling some 6 million phones every month now.

iPad App Store Gets Genius, Try Before You Buy

Demo versions have always been a big part of software download stores on the Internet – just not so much for the App Store, which in the end, created an environment of “lite” versions and, if we believe Mashable, also software piracy. Piracy is unlikely to go away entirely, but there is now a new try before you buy section in the App Store that may discourage piracy somewhat. And, Apple quietly introduced Genius as a new feature for the App Store.

Apple was never especially receptive for demo versions and that has not changed yet. However, lite versions are now specifically available in a try before you buy section. You can always argue what may have prompted Apple to introduce the feature. Perhaps it was just about time, considering the fact that there are more and more users and more and more apps – and sometimes you just don’t want to pay before you have not seen what an app can do. Mashable, however, believes that Apple may think that this category may prevent a scenario of would-be pirates – users who just don’t want to pay before they have tried the app.

The iPad App Store now has a Genius tab, enabling users to find new apps depending on the Apps they already have. While the feature was added quietly and there isn’t much information available, it appears that the Genius feature only recommends apps with a 2-star rating or higher. That is rather unfortunate for those apps that do not have a rating yet, but surely helps to get to the higher quality apps first. However, it means that good ratings will get even more important for apps and while the Genius feature is marketing help provided by Apple, it requires that you do your homework first – and get a good rating in the first place.

For those who keep count, there are more than 225,000 apps in Apple’s App Store.

Is it iPhone 5 time already?

This one seems to be a bit early. It isn’t quite Apple rumor time and it surely is not the time for a new iPhone to be discussed. Or is it?

The whole iPhone 4 antenna drama has spawned a new round of rumors, iPhone 5 rumors. One of those industry sources suggested that Apple is pushing up the release of the fifth generation iPhone up to early January 2011 to deal with the antenna issue. Sort of a redesigned iPhone 4. That is a tough one to believe, but then we never know. By the way, Apple just deleted the antenna performance comparison videos from its website. There was no information why that happened, but we think it is a good idea. A justification has never been really necessary anyway.

Besides the new iPhone 5, rumor has it that there will also be a smaller iPad with a screen size of seven inches. The source also talks about an iPod nano, a shuffle with a small 1.7” screen as well as the new iPod touch. There is one more product update cycle in September and there is no doubt in our mind that Apple will be updating its iPods then. It will be interesting to see how far Apple will take its iOS, whether it will make it into the nano or whether Apple will put a much stronger focus on the touch, which was highlighted by the company as the reason why the iPod line can still deliver somewhat stable revenues during Apple’s most recent earnings conference.

The iPhone 5 got us thinking though. Apple rumors have been very accurate lately, but it seems more likely that Apple will just redesign the iPhone 4, which may be feasible until January 2011 and a subsequent release. An entirely phone, which would also spark a discussion of a new iOS may be a bit optimistic.

iPhone Maintains the Edge in Web Usage

There are some now statistics out that indicate an extremely high product loyalty among iPhone users as well as impressive web usage that is most likely fueled by the iPhone 4.

Net Applications found that 0.70% of all web browsing now originates from iPhones. That may not sound much, but that is close to the web traffic posted by Linux systems, which is now at 0.93%, according to the market research firm. Mac OS X 10.5’s share, by the way, is at 1.82% and Mac OS X 10.6 stands at 2.48%, which shows that 0.70% from a smart phone is a big deal: The iPhone has gained 0.11 points in July alone, which was largest single-month usage gain ever. Android, by the way is estimated at a total web usage share of 0.18%.

Nielsen has released some charts that highlight the user loyalty of the iPhone. 89% of iPhone users intend to buy another iPhone, while 6% say they want to switch to Android and 2% say they are looking for a Blackberry. 71% of Android users say they want another Android phone, but 21% want an iPhone next. 29% of Blackberry owners are likely to switch to an iPhone and 21% to an Android phone.

Android seems to be gaining market momentum especially in the U.S. Nielsen said that 27% of new smartphone acquisitions went to Android in the second quarter of the year and only 23% to the iPhone.

The Black Sheep: Free iPhone Apps Snoop Contacts

Blackhat has started and not surprisingly, there is quite a bit of information coming out of Las Vegas that relates to the iPhone. Lookout revealed some results from its App Genome project, which analyzed about 300,000 apps that are available for the iPhone and Android. Sometimes you get what you pay for and that may be true in the case of iPhone apps as well, in a rather negative way.

Lookout said that one third of free applications for the iPhone can potentially access a user’s location. 14% can access a user’s contacts and 23 of iPhone apps contain third party code. According to the company, new vulnerabilities will be unveiled at Blackhat, including mobile data leakage, which, however, seems to affect Android more than the iPhone.

Lookout noted that App developers need to “be more aware of best practices for accessing, transmitting and storing users’ personal data. In addition, consumers need to be aware of the permissions that mobile applications request and how that personal data is being used in the application.“

Sounds reasonable to us. Given its exposure, the iPhone has become a very attractive target platform for malicious intent. And boy, opening up the platform to all apps, whether they are App Store certified or not, may create an entirely different dimension of software threats.

iPhone Programming Tutorial – Local Notifications

Way back when, when everyone was still complaining about Apple’s lack of support for (3rd party) multitasking, there was a simple solution put in place. This solution was known as push notifications.

Push notifications solved many of the issues associated with background processing.  For example, when quitting the AIM application, the server could keep you logged in and send you a push notification when a new message arrived.  You could then tap on a View button that would launch the app.

This solution is great and all, but it still requires that you have an active internet connection.  As of iOS4, Apple has introduced a new type of notification that can be scheduled to fire within the device itself.  It requires no complicated server programming, or additional configuration with iTunes.  I am talking about Local Notifications.

Local notifications can be scheduled on the user’s iDevice to fire at any given time; you can even set them to be recurring.  Today, we will explore these notifications and I will provide you with a simple example of how to schedule, view, and handle local notifications.  Here is a quick screenshot of the project that we are going to create (note I am using the iPhone 4  simulator).

The project will allow a user to schedule a location notification to fire off at a given date.  Using the text field, they are also able to specify some text for the notification.  The table view below displays a list of all of the currently scheduled location notifications within the application.

So, by now (if you are an avid iCodeBlog reader), you are already a killer iPhone dev and I can rush through the stuff that is not related to the notifications.  I will try to provide links to tutorials about sections that I rush through as well.

1. Create a View-Based Application

We will be using this as our starting point.  Check out this tutorial if you are unfamiliar with doing this.  Name the project Notifier.

2. Create All Properties and Dummy IBActions

This is usually a good first step when tackling an application like this.  Let’s get everything set up in the .h and .m files so we only have to visit Interface Builder Once.  Here is what our NotifierViewController.h file looks like.

@interface NotifierViewController : UIViewController&lt;UITableViewDelegate,UITableViewDataSource&gt; {
	IBOutlet UITableView *tableview;
	IBOutlet UIDatePicker *datePicker;
	IBOutlet UITextField *eventText;
}
 
@property (nonatomic, retain) IBOutlet UITableView *tableview;
@property (nonatomic, retain) IBOutlet UIDatePicker *datePicker;
@property (nonatomic, retain) IBOutlet UITextField *eventText;
 
- (IBAction) scheduleAlarm:(id) sender;
 
@end

Seems clear enough.  We have 3 UI elements that we care about and one action.  One thing to note is, your class should implement the UITableViewDelegate and UITableViewDataSource protocols.  This is because we will be displaying a tableview containing all of the scheduled alarms.

Now, do all of the necessary steps in your .m file.  This includes memory management for the IBOutlets and creating a dummy method for the scheduleAlarm IBAction.  Your .m file should look something like this. Note: I have omitted import statements because my syntax highlighter wasn’t digging them.

@implementation NotifierViewController
 
@synthesize datePicker,tableview, eventText;
 
- (IBAction) scheduleAlarm:(id) sender {
 
}
 
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
 
- (void)viewDidUnload {
	datePicker = nil;
	tableview = nil;
	eventText = nil;
}
 
- (void)dealloc {
    [super dealloc];
}
 
@end

Now it’s time to build our interface.  Open Interface builder and construct an interface like this.

If you want my super sweet green button image, here it is:

After creating the interface, make sure you hook up all of the UI components to their corresponding IBOutlets and hook up the touchUpInside: method of the button the your scheduleAlarm: IBAction.  For more info on hooking up IBOutlets, check out this tutorial.

3. Implement UITableViewDelegate and UITableViewDataSource Delegate methods to List Currently Scheduled Local Notifications

It may seem weird to implement the code to display the notifications before the code that creates them, however I like this approach better.  This way, once we schedule the notifications, they automagically appear in our table.  Add the following code to your .m file.

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // We only have one section
    return 1;
}
 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of notifications
    return [[[UIApplication sharedApplication] scheduledLocalNotifications] count];
}
 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
 
    static NSString *CellIdentifier = @"Cell";
 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }
 
    // Get list of local notifications
    NSArray *notificationArray = [[UIApplication sharedApplication] scheduledLocalNotifications];
    UILocalNotification *notif = [notificationArray objectAtIndex:indexPath.row];
 
    // Display notification info
    [cell.textLabel setText:notif.alertBody];
    [cell.detailTextLabel setText:[notif.fireDate description]];
 
    return cell;
}

OK, finally some “real” code.  Most of this code should seem pretty straight forward.  If not, check out this tutorial on UITableViews.

So, the new code here is dealing with retrieving a list of scheduled notifications.  Calling the scheduledLocalNotifications method of UIApplication will return an NSArray of all notifications scheduled by the current app.  We just index into this array and grab each notification.

Finally, we are displaying the alertBody (text that displays when the notification fires) and the fireDate (date and time when the notification will display) in the tableview cell.

4. Scheduling Notifications

And now for the moment you’ve been waiting for… OK, probably not, but definitely the most exciting (least boring) part of this tutorial.  Let’s implement that scheduleAlarm: IBAction that you framed out earlier.  Update your .m file to contain the following code.

- (IBAction) scheduleAlarm:(id) sender {
    [eventText resignFirstResponder];
 
    NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
 
    // Get the current date
    NSDate *pickerDate = [self.datePicker date];
 
    // Break the date up into components
    NSDateComponents *dateComponents = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit )
												   fromDate:pickerDate];
    NSDateComponents *timeComponents = [calendar components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit )
												   fromDate:pickerDate];
    // Set up the fire time
    NSDateComponents *dateComps = [[NSDateComponents alloc] init];
    [dateComps setDay:[dateComponents day]];
    [dateComps setMonth:[dateComponents month]];
    [dateComps setYear:[dateComponents year]];
    [dateComps setHour:[timeComponents hour]];
	// Notification will fire in one minute
    [dateComps setMinute:[timeComponents minute]];
	[dateComps setSecond:[timeComponents second]];
    NSDate *itemDate = [calendar dateFromComponents:dateComps];
    [dateComps release];
 
    UILocalNotification *localNotif = [[UILocalNotification alloc] init];
    if (localNotif == nil)
        return;
    localNotif.fireDate = itemDate;
    localNotif.timeZone = [NSTimeZone defaultTimeZone];
 
	// Notification details
    localNotif.alertBody = [eventText text];
	// Set the action button
    localNotif.alertAction = @"View";
 
    localNotif.soundName = UILocalNotificationDefaultSoundName;
    localNotif.applicationIconBadgeNumber = 1;
 
	// Specify custom data for the notification
    NSDictionary *infoDict = [NSDictionary dictionaryWithObject:@"someValue" forKey:@"someKey"];
    localNotif.userInfo = infoDict;
 
	// Schedule the notification
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
    [localNotif release];
 
	[self.tableview reloadData];
}

So, most of the explanation is in the comments.  I’ll talk you through some of the less obvious stuff.  The first tricky area is dealing with the NSCalendar.  We just use the NSCalendar object to break up the date into components.  Note: This demo does not require that we break the date up into components.  You could have just as easily fed the date from the date picker into the notification fireDate.  The reason that I’m showing you how to break it down is, you may have some sort of custom date logic to work with and this makes things much easier in the future.

Another important bit of code is where we set the alertBody or the notification.  In this example we set it to the text that the user entered into the text field.  You can set this to whatever you like.

The other thing I want to mention is the infoDict in the code.  This dictionary is your chance to associate some additional information with the alert.  For example, if you are using this alert in a game like We Rule to notify you when a crop is ready.  You might want to set a key and value that contains the id of the crop that has completed.  For now, we just set some arbitrary values and you can ignore them if you like.

After actually scheduling the notification, we just reload the tableview to get it to display immediately.

5. Handling Notifications After They Fire

The last piece of this puzzle is determining what to do when a notification fires.  Fortunately, this step is very easy and handled inside of the appDelegate.  When a notification fires, there are one of two situations. 1. The app is running and 2. The app is not running (or running in the “background”) .

Open up your app delegate .m file and add the following code.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
 
    // Override point for customization after application launch.
 
    // Add the view controller's view to the window and display.
    [window addSubview:viewController.view];
    [window makeKeyAndVisible];
 
    application.applicationIconBadgeNumber = 0;
 
    // Handle launching from a notification
    UILocalNotification *localNotif =
    [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
    if (localNotif) {
        NSLog(@"Recieved Notification %@",localNotif);
    }
 
    return YES;
}
 
- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif {
    // Handle the notificaton when the app is running
    NSLog(@"Recieved Notification %@",notif);
}

The first thing we see here is the application badge number is getting set to 0.  Whenever a notification fires, it will increase the badge count on the application.  Next, we handle the case when the application launches from a notification.   This happens when the user presses the view button on the notification.  For now, we just NSLog the data, but you should handle the notification how you see fit for your app.

Finally, we implement the didReceiveLocalNotification method.  This method is required if you want to handle notifications at all in your app.  You will see this method fire when the app is running and you receive a local notification.  When the app is running, you will not see the UIAlertView show up with the notification data.

And there you have it!  The complete lifecycle of a local notification.  You may download the source for this tutorial below.  If you have any questions, feel free to post them in the comments section or write them to me on Twitter.

Notifier Source Code

Jailbreaking, iAds, T-Mobile and You

It may be a bit tough to keep track with Apple today, even at the accelerated pace we are used to. However, there have been a few interesting bits of information that we think you should be aware of and that surely impact your apps today and in the future. T-Mobile may be announcing its iPhone this week, iAds may be the income source you should be relying on and then there is the fact the U.S. law has now legalized the jailbreaking of smartphones under certain circumstances.

The new T-Mobile rumor comes via Pocket-lint. Again, a reliable source disclosed that the T-Mobile iPhone will be a reality on July 30. Why July 30? Because that is the day Apple plans to expand the iPhone 4’s availability to 17 more countries and T-Mobile may just fit into that announcement. There was no information on pricing.

A change to the DMCA in the U.S. Code could get you thinking today. There are six scenarios in which consumers are now explicitly allowed to circumvent access controls to digital content, which is generally interpreted as the government’s ok to jailbreaking your iPhone and “liberating” other phones as well. Our conclusion is that the new addition basically opens the door to jailbreaking has many implications that may affect you even if you ignore this new law and stay with Apple’s guidelines.

Circumventing jailbreak blockers is now legally permitted through “Computer programs that enable wireless telephone handsets to execute software applications, where circumvention is accomplished for the sole purpose of enabling interoperability of such applications, when they have been lawfully obtained, with computer programs on the telephone handset.”

As in so many cases, this new law can be interpreted and most certainly will be bent left and right. It is important to see that jailbreaking is only legal if a user tries to run an application that is otherwise blocked – unauthorized apps that are not sold through the App Store. Breaking the software to run the phone on another network is not covered per se. That indicates that Apple will not change its warranty guidelines and they warranty will be void if jailbreaking occurs.

The law does not require Apple to explicitly allow jailbreaking and remove jailbreaking hurdles. Apple simply has to tolerate the ongoing jailbreaking sport and consumers who actually jailbreak their devices to run unauthorized devices. The code change makes it lawful to circumvent any controls that are designed to block jailbreaking. That’s it.

Apple’s major concern of such a law is that potentially unsafe applications can now be run on iPhones and affect other applications as well. The tight control Apple has over the app environment is somewhat impaired and developers will have to recognize that potentially malicious applications can affect all other applications on an iPhone as well. Protecting your app may be evolving as a new topic we should be thinking about more seriously.

On the other side, outside sales open new opportunities for those who believe it is economically beneficial to sell apps through other channels than the App Store. However, several sources have raised the general earnings potential through app stores anyway and other income sources may be something you really should be thinking about. Forrester analyst Thomas Husson wrote earlier today that Apple has made just $429 million in revenue from the App Store so far and that the bottom line isn’t exactly significant to Apple. Husson says that it is likely that “a significant number of independent developers have not recouped their investments via the current revenue-sharing model. “ Also, Husson says that there may be very limited interest from consumers to pay for apps.

The analyst believes that App Stores (other than Apple) will have to change to make their service more beneficial to all participants. He said that stores need to be more explicit about the addressable audience, create a viable business model for third parties, provide marketing and merchandising tools, offer a wide choice of payment and pricing options, show a local content catalog and look at other sources of inspiration – and not just Apple. So, if Apple is not the entire solution, there may be an opportunity now for other stores that carry “unauthorized” apps. If they are able to address current shortcomings, we may just see a new generation of App Stores developing in the very near future.