Sunday, August 14, 2011

Push Notification iOS Side Programming

Push Notification (also called Remote Notification) is a powerful free function provided by Apple. App can use it to push promotions or other tailor-made messages to App users.

iOS side code is simple, most of time within 200 lines. However, the logic part is a little bit difficult if you want to write an "intelligent" handler when push message comes, taking consideration of the following three scenarios:
  1. App is not running. 
  2. App is running at the background.
  3. App is running at the forefront.
Here, I am going to share some of the iOS side code for "Register for Push Notification" and "Handling of Push Notification".

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
[[UIApplication sharedApplicationregisterForRemoteNotificationTypes: UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert];
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;

        //////////Your App launch Setups
        //////////Your App launch Setups
        //////////Your App launch Setups
        [self.window makeKeyAndVisible];
//for receiving push notification when the App is not running.
if (launchOptions!=nil) {
if ([launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]!=nil) {
NSDictionary * userInfo=[launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
//this userInfo actually contains information about the received Push notification, so write some code about it and do handling about it here. 
    return YES;


-(void) application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
//send the token to your server here
//send the token to your server here
//send the token to your server here

Please take a look at the above code.
First line is to Register for Push Notification, and second line is to clear BadgeNumber if there is any.

Let's go to next function first,
-(void) application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken.

This function is called when the App receive the "token" from Apple, so you'd write some code to send this code to your server (Better hash it first). With this token, your server will be able to send Push Notification to the user with this "token".

Move back to function (didFinishLaunchingWithOptions), near there end the few lines of code is actually handler for scenario 1: App is not running. 

Handler for scenario 2 & 3 will be handled by the following function, basically it is the place you can capture the message (userInfo) for the Push Notification. In this example, I use an AlertView to display the Push message for scenario 3, which looks intuitive. Then, any further handling with be written within the AlertViewDelegate methods.

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
NSLog(@"Got Notification%@",userInfo);
if ( application.applicationState == UIApplicationStateActive ){
// app was already at the forefront: Scenario 3
if ([[userInfo objectForKey:@"aps"] objectForKey:@"alert"]) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil
message:[NSString stringWithFormat:@"%@",[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]]  delegate:self
  otherButtonTitles:@"View", nil];
[alertView show];
[alertView release];
else// app was just brought from background to forefront: Scenario 2
//Write your code here to handle your Push Notification

Hope this helps some people. It is not very in detail, but the structure is there. Thank you.

Monday, August 8, 2011

My App (1): Vcard Backup

VCard Backup

Expert on iPhone's Address Book

Main functions of this App are as following:

1. Import VCF contacts from your old mobile device (e.g. Nokia, Android) to iPhone. Just a few button clicks to BULK_import all your contacts, as long as they are standard VCFs. So this app helps reduce a lot of hurdles for people just switching from other smartphone to iPhone.
2. Export your current iPhone Address Book contacts as VCFs, and save them in your computer for usage of other platform or just as a reliable way to backup your contacts. To most people, contact list within the phone is the most important information that they are afraid to lose. So this app will be their good choice: You Can See The VCards.
3. Backup and Restore Address book (2nd Tab). All the data produced within the 2nd Tab is stored within the App. User can use this as a convenient way of backup and restore, but be careful that the saved data will be lost when the App is uninstalled.

Here are some step guidelines and screen captures for this App.

A: Import

  1. Connect your iPhone with your computer.
  2. Add all the VCFs to the App "VCard Backup" via iTunes' File Sharing.
  3. Open the App in your iPhone, and hit Refresh button. You should see total number of VCFs detected by the App.
  4. Hit "Import to Address Book".
  5. That's it. All the contacts are added to your Address Book.

B: Export

  1. For iOS 6, please turn off Contacts to access your Facebook Contacts, as shown below.
  2. Connect your iPhone with your computer.
  3. Open the App "VCard Backup".
  4. Hit "Export All Contacts".
  5. Save the folder "Export" to your local directory via iTunes' File Sharing.

C:Backup & Restore

  1. Click "Backup Address Book".
  2. Click "Restore from Backup" to restore directly within the App. OR
  3. Click "Email".
  4. Open the Attachment (Backup.vcf) from Mail, you will be able to add all the contacts to address book of any iOS device.
Please leave me a comment here if you have any improvement suggestions or find any Bugs of the App. Thank you.

Tuesday, August 2, 2011

How to add UITextField to UIAlertView from XIB

Today, I am going to share how to add UITextFields to UIAlertView from XIB. 
UITextFields can be added through coding, but I prefer draw it in a small view from XIB because it is more convenient and visualizable. In the following example, I will add 3 UITextFields and 4 UILabels in the small view.

1. Create a view_controller and add the customized small view(named AlertXIB in the picture, while named "changePasswordView" in the code) parallel to the main view of the view_controller. Add certain number of UILabels and/or UITextfields in the small view. As shown in the picture:

2. Add the view into the view_controller' main view on a button click.

-(IBAction)buttonClicked:(UIButton*) button {
//If I don't add this _fakeTextField, my own textfields won't be able to detect inputs from keyboard, another good thing is that the alertview will be automatically moved up a bit.
UITextField * _fakeTextField=[[UITextField alloc] initWithFrame:CGRectMake(12, 3, 200, 30)];
_fakeTextField.backgroundColor=[UIColor clearColor];

UIAlertView *_myAlertView = [[UIAlertView alloc] initWithTitle:nil message:@"\n\n\n\n\n" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK",nil];
changePasswordView.frame=CGRectMake(12, 3, changePasswordView.frame.size.width, changePasswordView.frame.size.height);
[_myAlertView addSubview:_fakeTextField];
[_fakeTextField release];
[_myAlertView addSubview:changePasswordView];
[_myAlertView show];
[_myAlertView release];

A few things to explain here:
1). The message of the UIAlertView, I use several newline characters, because the message will be determine the size of the UIAlertView.

2). A "_fakeTextField" is also added, reason is what i explained in the comment. You can play around with it. For all the UITextfields "newPassword, oldPassword, confirmPassword", you can configure their UITextFieldDelegate via XIB or code. 

3). delegate of the UIAlertView is set to self (the view_controller), so you can handle further processing when user clicks button on the UIAlertView.

#pragma mark -
#pragma mark UIAlertViewDelegate

-(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex==0) {
NSLog(@"button at index 0, cancel and doing nothing");
} else {
//Change password code here
NSLog(@"button at index 1");

3. So when the button click method (buttonClicked:) is called, the view will look like this. The UILabel text in the UIAlertView is in black color is just for demo, while you can change it to any color or size you want.