Thursday, 25 August 2011

World of ios4.3

Welcome to world of ios4.3

Feature Enhancements in ios4.3:-

• AirPlay Enhancements
AirPlay is a technology that lets your application stream audio and now Video to Apple TV and to third-party AirPlay speakers and receivers. Once the user chooses to play your audio and now Video using AirPlay, it is routed automatically by the system.

• Safari Performance
Now surf the web as fast as in iOS (4.2)2

• iTunes Home Sharing
• iPad Side Switch
• Personal Hotspot for iPhone 4
Now Enable Personal Hotspot and share your cellular data connection with your Mac, PC, iPad, or other Wi-Fi-capable device. You can share your connection with up to five devices at once over Wi-Fi, Bluetooth, and USB .Every connection is password protected and secure. And it’s power friendly, too.

• iPad Features(Multitasking):-

1. You pinch to get to the Home screen (instead of pressing the home button)
2. Swipe up to reveal the multitasking tray (instead of double-clicking the home button)
3. Swipe left and right to switch apps

Crack Nutshells of ios4.3:-
Time for the ios developers to take a sound breath because this section describes the developer-related features introduced in ios 4.3

AirPlay Video Support:-

Support for playing video using AirPlay is included in the MPMoviePlayerController class. This support allows you to play video-based content on AirPlay–enabled hardware such as Apple TV. When the allowsAirPlay property of an active MPMoviePlayerController object is set to YES and the device is in range of AirPlay–enabled hardware, the movie player presents the user with a control for sending the video to that hardware. (That property is set to NO by default.)

allowsAirPlay=YES;

For web-based content, you can enable AirPlay Video in the QuickTime Plug-in or HTML5 video element as follows:

QTPlug-in:
● airplay="allow"
● airplay="deny"(Default)

For example: <embed src="movie.mov" width="320" height="240" airplay="allow">

HTML5 video element:

● x-webkit-airplay="allow"
● x-webkit-airplay="deny"(Default)

For example: <video controls width="640" height="368" x-webkit-airplay="allow" src="content/side_with_the_seeds.mov"> </video>

Framework Enhancements:-

The following sections highlight the significant changes to frameworks and technologies in iOS 4.3

AV Foundation Framework:-
The AV Foundation framework includes the following enhancements

1.)The AV Foundation framework added the following classes for tracking network playback statistics.

• AVPlayerItem
• AVPlayerItemAccessLogEvent
• AVPlayerItemErrorLogEvent

2.)The AVMetadataItem class added support for loading key data asynchronously.

iAd Framework:-
The ADInterstitialView class is a view that you can use to embed full-screen banners into your content. You can present these banners modally or as part of a transition from one page of content to another.

UIKit Framework:-
The UIViewController class added the disablesAutomaticKeyboardDismissal method, which you can use to override the default input view dismissal behavior.

Media Player Framework:-
The Media Player framework includes the following enhancements:

1.)The MPMoviePlayerController class supports playback of video content using AirPlay.

2.)The Media Player framework added the following classes for tracking network playback statistics.

1. MPMovieAccessLog,
2. MPMovieErrorLog,
3. MPMovieAccessLogEvent
4. MPMovieErrorLogEvent

3.)The MPMoviePlayerController class now includes properties for accessing log information.


Core Audio Framework:-
The Audio Unit and Audio Toolbox frameworks include the following enhancements

1.)The AudioUnitParameterHistoryInfo struct (in the Audio Unit framework) along with supporting audio unit properties adds the ability to track and use parameter automation history.

2.) The ExtendedAudioFormatInfo struct (in the Audio Toolbox framework) lets you specify which codec to use when accessing the kAudioFormatProperty_FormatList property.

3.)The kAFInfoDictionary_SourceBitDepth dictionary key and the kAudioFilePropertySourceBitDepth property (in the Audio Toolbox framework) provide access to the bit depth of an audio stream.

4.)The kAudioConverterErr_NoHardwarePermission result code (in the Audio Toolbox framework) indicates that a request to create a new audio converter object cannot be satisfied because the application does not have permission to use the requested hardware codec.

I hope this document will help you in understanding and developing applications in ios4.3.
Happy Coding!!!!:)

Adding To Favorites Functionality

In most of the apps of iphone there is an option of favorites .You can add items to favorite list,remove items and view items marked as your favorites ones.

This wiki includes the complete implementation of favorites functionality

In your AppDelegate.h file create a favorite array.

NSMutableArray *favoritesArray;
make the property and synthesize it.

In your AppDelegate.m file

- (void)applicationDidFinishLaunching:(UIApplication *)application {

//Create an instance ofNSUserDefaults

NSUserDefaults *stdDefaults=[NSUserDefaults standardUserDefaults];
if([stdDefaults valueForKey:@"favorites"] == nil)
[self setFavoritesArray:[[NSMutableArray alloc]init]];
else
[self setFavoritesArray:[NSMutableArray arrayWithArray:[stdDefaults valueForKey:@"favorites"]]];

}

- (void)applicationWillResignActive:(UIApplication *)application{

if(updated == NO){
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setObject:favoritesArray forKey:@"favorites"];
[NSUserDefaults resetStandardUserDefaults];
updated = YES;
}
}

- (void)applicationWillTerminate:(UIApplication *)application {
// Save data if appropriate

if(updated == NO){
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setObject:favoritesArray forKey:@"favorites"];
[NSUserDefaults resetStandardUserDefaults];
updated = YES;
}
}


Before adding the item in the favorites we check if it already exists or not.If it does not exist it gets added else the popup appears.




-(IBAction)addToFavorites
{
once = NO;

for(int i=0;i<[[appDelegate favoritesArray] count];i++)
{
NSDictionary *tempFavDict = [[appDelegate favoritesArray] objectAtIndex:i];
NSArray *key=[tempFavDict allKeys];
NSDictionary *dkeydict=[tempFavDict objectForKey:[key objectAtIndex:0]];
NSString *businessName = [dkeydict valueForKey:@"name"];

if([name isEqualToString:businessName]){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Already added" message:nil
delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil ,nil];
[alert show];
alert.tag=1;
[alert release];
once=YES;
break;
}
}
if(once==NO)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message: @"Do you want to add this business to your Favorites ?"
delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
[alert show];
alert.tag=2;
[alert release];

}
}







Now on the button click or wherever you want to add items in your favorite list add code:-

In the following code I have created a favoriteDict dictionary which contains all the values and adding that dictionary into favorites array.


- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
{
if(alertView.tag==2)
{
if(buttonIndex==0)
{
return;
}
if(buttonIndex==1)
{
NSMutableDictionary *favoriteDict=[[NSMutableDictionary alloc] init];
NSDate *dat=[NSDate date];
NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
[formatter setDateFormat:@"yyyy-MM-dd"];
NSString *favDate=[formatter stringFromDate:dat];
[formatter release];
[favoriteDict setObject:favDate forKey:@"date"];
[favoriteDict setObject:self.name forKey:@"name"];
[favoriteDict setObject:self.address forKey:@"address"];
[favoriteDict setObject:self.description forKey:@"description"];
[favoriteDict setObject:self.contact forKey:@"contact"];
[favoriteDict setObject:self.phone forKey:@"phone"];
[favoriteDict setObject:self.email forKey:@"email"];
[favoriteDict setObject:self.website forKey:@"website"];

//[favoriteDict setObject:self.code forKey:@"businesscode"];
[busniessCodeFavorite setObject:favoriteDict forKey:self.code];
[appDelegate.favoritesArray addObject:busniessCodeFavorite];

//Resetting favorites in NSUserDefaults

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setObject:appDelegate.favoritesArray forKey:@"favorites"];
[NSUserDefaults resetStandardUserDefaults];
[favoriteDict release];

//NSLog(@"appdelegate favories array%@",appDelegate.favoritesArray);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message: @"Added To Favorites"
delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
}
}




The selected item is added in favorites Array now create a link where your favorite item list will be displayed




Tuesday, 23 August 2011

UIPageControl Tutorial


You have to make two view Controllers
First :- That will contain your scrolling and deegate implementation.
Second:-That will describe what you want to show in your view.

the first file should be the same and you might do changes in your second file depending on your layout requirements.

First:-
In your .h file (MyCardsViewController.h)

#import <UIKit/UIKit.h>


@interface MyCardsViewController : UIViewController <UIScrollViewDelegate>{

UIScrollView *scrollView;
IBOutlet UIPageControl *pageControl;
NSMutableArray *viewControllers;

// To be used when scrolls originate from the UIPageControl
BOOL pageControlUsed;
}

@property (nonatomic, retain) IBOutlet UIScrollView *scrollView;
@property (nonatomic, retain) IBOutlet UIPageControl *pageControl;
@property (nonatomic, retain) NSMutableArray *viewControllers;

- (IBAction)changePage:(id)sender;

@end

In your .m file (MyCardsViewController.m)

#import "MyCardsViewController.h"
#import "MyViewController.h"

static NSUInteger kNumberOfPages = 3;

@interface MyCardsViewController (PrivateMethods)

- (void)loadScrollViewWithPage:(int)page;
- (void)scrollViewDidScroll:(UIScrollView *)sender;

@end


@implementation MyCardsViewController
@synthesize scrollView, viewControllers,pageControl;


/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
*/
- (void)viewDidLoad {
self.navigationController.navigationBar.hidden=YES;
[super viewDidLoad];

// view controllers are created lazily
// in the meantime, load the array with placeholders which will be replaced on demand
NSMutableArray *controllers = [[NSMutableArray alloc] init];
for (unsigned i = 0; i < kNumberOfPages; i++) {
[controllers addObject:[NSNull null]];
}
self.viewControllers = controllers;
[controllers release];

// a page is the width of the scroll view
scrollView.pagingEnabled = YES;
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * kNumberOfPages, scrollView.frame.size.height);
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.scrollsToTop = NO;
scrollView.delegate = self;

pageControl.numberOfPages = kNumberOfPages;
pageControl.currentPage = 0;

// pages are created on demand
// load the visible page
// load the page on either side to avoid flashes when the user starts scrolling
[self loadScrollViewWithPage:0];
[self loadScrollViewWithPage:1];
}

- (void)loadScrollViewWithPage:(int)page {
if (page < 0) return;
if (page >= kNumberOfPages) return;

// replace the placeholder if necessary
MyViewController *controller = [viewControllers objectAtIndex:page];
if ((NSNull *)controller == [NSNull null]) {
controller = [[MyViewController alloc] initWithPageNumber:page];
[viewControllers replaceObjectAtIndex:page withObject:controller];
[controller release];
}

// add the controller's view to the scroll view
if (nil == controller.view.superview) {
CGRect frame = scrollView.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
controller.view.frame = frame;
[scrollView addSubview:controller.view];
}
}

- (void)scrollViewDidScroll:(UIScrollView *)sender {
// We don't want a "feedback loop" between the UIPageControl and the scroll delegate in
// which a scroll event generated from the user hitting the page control triggers updates from
// the delegate method. We use a boolean to disable the delegate logic when the page control is used.
if (pageControlUsed) {
// do nothing - the scroll was initiated from the page control, not the user dragging
return;
}

// Switch the indicator when more than 50% of the previous/next page is visible
CGFloat pageWidth = scrollView.frame.size.width;
int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
pageControl.currentPage = page;

// load the visible page and the page on either side of it (to avoid flashes when the user starts scrolling)
[self loadScrollViewWithPage:page - 1];
[self loadScrollViewWithPage:page];
[self loadScrollViewWithPage:page + 1];

// A possible optimization would be to unload the views+controllers which are no longer visible
}

// At the begin of scroll dragging, reset the boolean used when scrolls originate from the UIPageControl
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
pageControlUsed = NO;
}

// At the end of scroll animation, reset the boolean used when scrolls originate from the UIPageControl
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
pageControlUsed = NO;
}

- (IBAction)changePage:(id)sender {
int page = pageControl.currentPage;

// load the visible page and the page on either side of it (to avoid flashes when the user starts scrolling)
[self loadScrollViewWithPage:page - 1];
[self loadScrollViewWithPage:page];
[self loadScrollViewWithPage:page + 1];

// update the scroll view to the appropriate page
CGRect frame = scrollView.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
[scrollView scrollRectToVisible:frame animated:YES];

// Set the boolean used when scrolls originate from the UIPageControl. See scrollViewDidScroll: above.
pageControlUsed = YES;
}


- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];

// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
self.pageControl=nil;
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}


- (void)dealloc {
[pageControl release];
[viewControllers release];
[scrollView release];
[pageControl release];
[super dealloc];
}

@end

in your this viewController xib add a scrollview and a page control and bind it.

Start up with secondViewController

in .h file:-

int pageNumber;
UIImageView *imageView;
}


@property(nonatomic,retain) IBOutlet UIImageView *imageView;

- (id)initWithPageNumber:(int)page;

in .m file

+ (UIColor *)pageControlColorWithIndex:(NSUInteger)index {
if (__pageControlColorList == nil) {
__pageControlColorList = [[NSArray alloc] initWithObjects:[UIColor redColor], [UIColor greenColor], [UIColor magentaColor],
[UIColor blueColor], [UIColor orangeColor], [UIColor brownColor], [UIColor grayColor], nil];
}

// Mod the index by the list length to ensure access remains in bounds.
return [__pageControlColorList objectAtIndex:index % [__pageControlColorList count]];
}

// Load the view nib and initialize the pageNumber ivar.
- (id)initWithPageNumber:(int)page {
if (self = [super initWithNibName:@"MyViewController" bundle:nil]) {
pageNumber = page;
}
return self;
}

- (void)dealloc {
[imageView release];
[super dealloc];
}

// Set the label and background color when the view has finished loading.
- (void)viewDidLoad {
[imageView setImage:[UIImage imageNamed:[NSString stringWithFormat:@"cardview%d",pageNumber + 1]]];
self.view.backgroundColor = [MyViewController pageControlColorWithIndex:pageNumber];
}

Adwhirl Integration in iphone apps

This Blog describe how to integrate Adwhirl in your project:-
Adwhirl supports iads in ios 3.0 also.
This blog assumes that you have your application key and you have activated your supported networks.

Getting Started :-
First download the Adwhirl sdk from http://code.google.com/p/adwhirl/downloads/list
You will get a folder which contains these folders:-


Now open your project and drag adwhirl and touchJson folder in your project.
After you drag this window will appear dont forget to check copy items options



Do the same thing for adding touchJson folder also 
Now your project will look like:-


Adwhirl contains a lot of adapters network you have to check only those that you want to use.In my case i am using admobs and iAds
Check only those that you want to use:-


After that Add following frameworks:-

UIKit.framework, Foundation.framework and CoreGraphics.framework should be included in your project by default.
● AddressBook.framework
● AudioToolbox.framework 
● AVFoundation.framework
● CoreLocation.framework 
● MapKit.framework
○ weak-linked for OS 2.X support 
● MediaPlayer.framework
● MessageUI.framework 
○ weak-linked for OS 2.X support
● QuartzCore.framework 
● SystemConfiguration.framework 
● iAd.framework
○ required for iAd, weak-linked for OS 3.X support
● libsqlite3.dylib 
● libz.dylib




Now you need to download the sdk for adapter network you are using.That you can download from that ads corresponding site.
I have used Admobs so for that I have downloaded abmobs sdk
Now add that downloaded folder in the same way as added adwhirl folder.



Now You are ready with your whole integration process and you have to add few lines of code now

In your viewController.h file write:-

#import "AdWhirlDelegateProtocol.h"

add AdwhirlDelegate like this @interface AdwhirlSampleViewController : UIViewController <AdWhirlDelegate>

In your viewController.m file write:-

#import "AdWhirlView.h"

these two delegate methods of adwhirl

-(NSString *)adWhirlApplicationKey
{
return @"a98aa6af9bc04aa59d551a862320d062";
}
- (UIViewController *)viewControllerForPresentingModalView
{
return [((AdwhirlSampleAppDelegate *)[[UIApplication sharedApplication] delegate]) viewController];
}

- (void)viewDidLoad {
[super viewDidLoad];
AdWhirlView *awView = [AdWhirlView requestAdWhirlViewWithDelegate:self]; 
[self.view addSubview:awView];

}

Making rounded corners of XCode Controls

This blog describe a very basic and essential feature that is used in iPhone/iPad Applications.When we normally use label,button etc the shapes are in square form.Of course you can convert it in a rectangle by varying the width and height but the problem occurs when you have to give a rounded shape.

This is what the blog will guide you :-

First you have to add QuartzCore Framework in your project.

Add #import <QuartzCore/QuartzCore.h> in your file.

Now add these lines of code where ever you want to show your control

CALayer *l = [btn layer];
[l setMasksToBounds:YES];
[l setCornerRadius:20.0];

btn is my UIButton .You can change the value of setCornerRadius to give your desired rounded shape.

Zooming the location on the mapView within the cretain radius


This blog describe how to zoom your current location or any another location on map in certain radius.

Write the following code when you have requirement to zoom your location within a certain radius.

CLLocation *zoomLocation= map.userLocation.location;
float latitude = zoomLocation.coordinate.latitude;
float longitude = zoomLocation.coordinate.longitude;
double miles = 3.0;
double scalingFactor = ABS( (cos(2 * M_PI * latitude / 360.0) ));
MKCoordinateSpan span;
span.latitudeDelta = miles/69.0;
span.longitudeDelta = miles/(scalingFactor * 69.0);
MKCoordinateRegion region;
region.span = span;
region.center = zoomLocation.coordinate;
[map setRegion:region animated:YES];
[map regionThatFits:region];

This code will make your current location zoom within 3 miles radius on your map.

Thursday, 18 August 2011

Sending Email Through iPhone Within the app



Hi All,

This Blog describes how to send the e-mail from the app.

Very First you have to attach "MessageUI" framework kit in your framework folder.

In your ViewController.h file import this class:-

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



In your ViewController.m file write the below code:-


if([MFMailComposeViewController canSendMail]){

MFMailComposeViewController *mail=[[MFMailComposeViewController alloc]init];
mail.mailComposeDelegate=self;
[mail setSubject:@"subject of the mail"];
[mail setMessageBody:@"Hello my first mail from iphone app" isHTML:NO];
[self presentModalViewController:mail animated:YES];
[mail release];
}

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

}