How to Launching Other Apps within an iPhone Application?


Here I am going to explain you few key feature using NSURL and UIApplication.

Examples of some of the key applications that you can launch via URL are:

Launch Google Maps


Launch Apple Mail


Dial a Phone Number


Launch the SMS Application


Launch the Browser


Launch the AppStore

Launching the Browser from within an iPhone application

 

 

Launch Google Maps

The URL string for launching Google Maps with a particular keyword follows this structure:
http://maps.google.com/maps?q=${QUERY_STRING}
The only trick to this is to ensure that the value for the ${QUERY_STRING} is properly URL encoded. Here is a quick example of how you would launch Google Maps for a specific address:
// Create your query …
NSString* searchQuery = @”1 Infinite Loop, Cupertino, CA 95014″;

// Be careful to always URL encode things like spaces and other symbols that aren’t URL friendly

searchQuery =  [addressText stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];

// Now create the URL string …

NSString* urlString = [NSString stringWithFormat:@”http://maps.google.com/maps?q=%@”, searchQuery];

// An the final magic … openURL!
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlText]];

Launch Apple Mail
Also very useful, is the ability to enable a user to quickly send an email by launching the email client in compose mode and the address already filled out. The format of this URI should be familiar to anyone that has done any work with HTML and looks like this:

mailto://${EMAIL_ADDRESS}

For example, here we are opening the email application and filling the “to:” address with info@iosdevelopertips.com :
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@”mailto://info@iosdevelopertips.com”]];

Dial a Phone Number (iPhone Only)


You can use openURL: to dial a phone number. One advantage this has over other URLs that launch applications, is that the dialer will return control back to the application when the user hits the “End Call” button.
Anyone familiar with J2ME or WML will find this URL scheme familiar:

tel://${PHONE_NUMBER}

Here is an example of how we would dial the number (900) 867-9999:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@”tel://9008679999″]];
NOTE When providing an international number you will need to include the country code.

Launch the SMS Application

Also not supported by the iPod Touch, is the ability to quickly setup the SMS client so that your users can quickly send a text message. It is also possible to provide the body of the text message.
The format looks like this:

sms:${PHONENUMBER_OR_SHORTCODE}

NOTE: Unlike other URLs, an SMS url doesn’t use the “//” syntax. If you add these it will assume it is part of the phone number which is not.

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@”sms:55555″]];

NOTE: According to the official SMS specification, you should be able to send a body as well as the phone number by including “?body=” parameter on the end of the URL … unfortunately Apple doesn’t seem to support this standard.

Launching the AppStore

Finally, it is worth noting that you can launch the AppStore and have the “buy” page of a specific application appear. To do this, there is no special URL scheme. All you need to do is open up iTunes to the application you want to launch; right-click on the application icon at the top left of the page; and select Copy iTunes Store URL .
The URL will look something like this:

http://itunes.apple.com/in/app/angry-birds-space-hd-free/id526337775?mt=8

Launching the AppStore URL is exactly the same as you would launch the browser. Using the link above, here is an example of how we would launch the AppStore:

NSURL *appStoreUrl = [NSURL URLWithString:@”http://itunes.apple.com/in/app/angry-birds-space-hd-free/id526337775?mt=8″%5D;
%5B%5BUIApplication sharedApplication] openURL:appStoreUrl];

 

Launching the Browser from within an iPhone application

It is sometimes nice to be able to launch a browser from within your applications. Though not as elegant as the use of a UIWebView, it is much easier.
NOTE Because it is possible for an application to bind itself to a URL (like Google Maps), this technique can also be used to launch other applications on the device.
Here is a simple example of how to open safari with a specific URL:

NSURL *url = [NSURL URLWithString:@”https://iosrider.wordpress.com”%5D;
[[UIApplication sharedApplication] openURL:url];

FacebookAgent and iPhone/iPad integration


Today most of the applications we build have a common feature: Socialize! One common way to achieve this my integrating Facebook within the app.For iPhone applications FacebookConnect is the only way to integrate facebook social features. Facebook team has developed a sdk for iPhone. A sample code showing the use and integration of Facebook Connect in your application. This is quite simple and also tons of tutorials, forums, e-books and videos are out here on the web.

Objective: Create a simple view based application and publish a feed using FacebookAgent.

1. Download this folder FacebookAgent.zip.

It also includes facebook connect sdk. So you don’t need to add this sdk elsewhere or even update any project settings for this purpose.

2. Create a new project, name it FacebookAgentTest. Righ click on the project in the XCode and select Add existing files. Add the FacebookAgent Folder to your classes. Check copy the source files.

Add FacebookAgent folder to your project

3.  In the FacebookAgentTestViewController.h, import FacebookAgent.h and implement protocol FacebookAgentDelegate.

@interface FacebookAgentTestViewController : UIViewController {
...

4. Declare a member variable of type FacebookAgent.

FacebookAgent* fbAgent;

5. Open FacebookAgentTest.m and initialize fbAgent in viewDidLoad method.

fbAgent = [[FacebookAgent alloc] initWithApiKey:@"PLACE YOUR FACEBOOK APPLICATION API KEY"
										  ApiSecret:@"PLACE YOUR FACEBOOK APPLICATION API SECRET"
										   ApiProxy:nil];
	fbAgent.delegate = self;

6. Declare an IBAction method in the FacebookAgentTest.h file.

-(IBAction)updateStatus:(id)sender;

7. Define the above method in FacebookAgentTest.m file.

-(IBAction)updateStatus:(id)sender{
}

8. Open FacebookAgentTestViewController.xib and add a button in the view and connect the above method on its touchesUpInside signal.

9. Now just add the following lines in the updateStatus method to publish a feed!

-(IBAction)updateStatus:(id)sender{
[fbAgent publishFeedWithName:@"Hellow world"
					 captionText:@"how are you?"
						imageurl:@"http://amanpages.com/wordpress/wp-content/uploads/2009/12/logo2.png"
						 linkurl:@"http://amanpages.com/"
			   userMessagePrompt:@"What do you think:"];
}

This code will first check if the user is logged in. If logged in it will show the feed dialog.
If the user is not logged in already, first the login dialog will be shown, after logging in, the feed dialog will be shown automatically!

But one minute, there is one require method in the FacebookAgentDelegate protocol. So, you need to define this:

- (void) facebookAgent:(FacebookAgent*)agent loginStatus:(BOOL) loggedIn{
}

Above method is called when the user logs in or logs out of the facebook. If you use any login button which is very likely, you may change the button title here.

10. Try updating your status. For this use this line:

[fbAgent setStatus:@"status from iPhone 1"];

This will first check if the user is logged in. if not logged in, then it will show the login prompt first.
After log in it will check if extended permission is enabled for this app. if not it will show the permission dialog.
Having given the permission, it will change the user status.

BUT.

11. You need to two more FacebookAgentDelegate method if you want to change status:

- (void) facebookAgent:(FacebookAgent*)agent requestFaild:(NSString*) message{
}
- (void) facebookAgent:(FacebookAgent*)agent statusChanged:(BOOL) success{
}

12. Thats all :) Run the app!

You can look into the FacebookAgent.h for more detail. It is well documented. If you still have some question shoot here or drop me mail.
Feel free to use this classes without any restriction but I will appreciate if you let me know in which app you are using it :)

What can be done now using FacebookAgent:
1. Fetch user name:
For this after initialization, set shouldFetchUsernameAfterLogin = YES.

fbAgent.shouldFetchUsernameAfterLogin = YES

and also define the corresponding delegate method:

- (void) facebookAgent:(FacebookAgent*)agent didLoadName:(NSString*) name{
    //use the name
}

2. Make your own attachment and publish a feed.

// this method has some over loaded versions too
- (void) publishFeed:(NSString*)attachement;

3. Publish feed by passing, name, caption, image and link url.

// this method has some over loaded versions too
/**
 * Let the agent make attachement for you. You just pass the information
 *
 */
- (void) publishFeedWithName:(NSString*)name
			 captionText:(NSString*)caption
					   imageurl:(NSString*)url
						linkurl:(NSString*)href
			  userMessagePrompt:(NSString*)prompt;

4. upload a photo

- (void) uploadPhoto:(NSString*)imageurl;

6. ask for extended permission

- (void) askPermission;

7. login and logout

- (void) login;
- (void) logout;

The delegates also offers some handy callback options like:

/**
 * Must define this method if setStatus is called
 *
 * This method is called when user status is changed either successfully or not
 */
- (void) facebookAgent:(FacebookAgent*)agent statusChanged:(BOOL) success;

/**
 * Must define this method if shouldFetchUsernameAfterLogin is set YES
 *
 * This method is called after the agent fetched facebook profile name
 */
- (void) facebookAgent:(FacebookAgent*)agent didLoadName:(NSString*) name;

/**
 * Must define this method if uploadPhoto is called
 *
 * This method is called after photo is uploaded
 */
- (void) facebookAgent:(FacebookAgent*)agent photoUploaded:(NSString*) pid;

/**
 * Must impement this method if any of the above method is defined
 *
 * This method is called if the agent fails to perform any of the above three actions
 */
- (void) facebookAgent:(FacebookAgent*)agent requestFaild:(NSString*) message;

@required

/**
 * This method is called if after login or logout
 */
- (void) facebookAgent:(FacebookAgent*)agent loginStatus:(BOOL) loggedIn;

Here is the demo project. DONT FORGET TO ADD YOUR key and secret!!

 

FacebookAgentTest

Cocoa: Convert number to comma-separated NSString


At some point in your Mac, iPhone, or iPad development you may find the need to express a long number (1000000000) as a comma-separated string (1,000,000,000) to make life easier on your users. The NSNumberFormatter class is a rich tool for converting numbers to strings supporting different types of currencies and localizations. It’s also the perfect class to leverage for our comma-ing task.

First of all, the NSNumberFormatter class works on NSNumber objects, so we need to convert our number to a NSNumber if it’s not there already.
NSNumber *number = [NSNumber numberWithInt:1000000000];

NSNumber also supports floating point values (numberWithFloat) and the regular gang of other number formats.

With our NSNumber in hand, we can get on with the good stuff. NSNumberFormatter supports grouping of numeric digits into arbitrary length groups (we want groups of three) and separating the groups with arbitrary strings (we want to use a comma (@”,”) but we could use any string). Here’s the code that makes our string:

NSNumberFormatter *frmtr = [[NSNumberFormatter alloc] init];
[frmtr setGroupingSize:3];
[frmtr setGroupingSeparator:@","];
[frmtr setUsesGroupingSeparator:YES];
NSString *commaString = [frmtr stringFromNumber:number];

 

How to Integrate iAd in your Application?


Hi,

Today I am going to discuss how apple iAd is integrated in our iPhone/iPad Native Application.It’s really a very simple task.I am dividing this task in 3 simple steps.

Step 1:

1.import iAd Framework to the Application.

2.Provide #import <iAd/iAd.h> in the particular controller where you want to show your Add.

3.Provide it’s delegate UIViewController <ADBannerViewDelegate>

4.Provide one  view to that particular ViewController.Assume I have taken

@property (weak, nonatomic) IBOutlet UIView *contentView;

Step 2:

Allocate it in ViewDidLoad method

– (void)viewDidLoad
{
_bannerView = [[ADBannerView alloc] init];
_bannerView.delegate = self;

[super viewDidLoad];
[self.view addSubview:_bannerView];
}

step 3:

Provides it’s delegate methods which i have mention bellowed.

– (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) {
_bannerView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
} else {
_bannerView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierLandscape;
}
[self layoutAnimated:duration > 0.0];
}

– (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
[self layoutAnimated:YES];
}

– (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
[self layoutAnimated:YES];
}

– (BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave
{

return YES;
}

– (void)bannerViewActionDidFinish:(ADBannerView *)banner
{

}
– (void)layoutAnimated:(BOOL)animated
{
if (UIInterfaceOrientationIsPortrait(self.interfaceOrientation)) {
_bannerView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
} else {
_bannerView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierLandscape;
}

CGRect contentFrame = self.view.bounds;
CGRect bannerFrame = _bannerView.frame;
if (_bannerView.bannerLoaded) {
contentFrame.size.height -= _bannerView.frame.size.height;
bannerFrame.origin.y = contentFrame.size.height;
} else {
bannerFrame.origin.y = contentFrame.size.height;
}

[UIView animateWithDuration:animated ? 0.25 : 0.0 animations:^{
self.contentView.frame = contentFrame;
[self.contentView layoutIfNeeded];
_bannerView.frame = bannerFrame;
}];
}

and your Advertisement is ready.

You can take the source code from Here.

(In File Option click on Download Original)