Friday 29 January 2016

Push notification

Push notifications:
There are two types of notifications in ios, local and remote notification. Remote notifications are also called push notification. Notifications enable an app that isn’t running in the foreground to let its users know it has information for them. The information could be a message, an impending calendar event, or new data on a remote server. When presented by the operating system, local and remote user notifications look and sound the same. They can display an alert message or they can badge the app icon. They can also play a sound when the alert or badge number is shown.
1) An app enables push notifications. The user has to confirm that he wishes to receive these notifications.
2) The app receives a “device token”. You can think of the device token as the address that push notifications will be sent to.
3) The app sends the device token to your server.
4) When something of interest to your app happens, the server sends a push notification to the Apple Push Notification Service, or APNS for short.
5) APNS sends the push notification to the user’s device.
6) When the user’s device receives the push notification, it shows an alert, plays a sound and/or updates the app’s icon. The user can launch the app from the alert. The app is given the contents of the push notification and can handle it as it sees fit. 
If you want to notify the users of your app about external events while the app is closed, you still need push notifications.
To enable push notification in an app you need some dependencies:
1)To enable push notifications in your app, it needs to be signed with a provisioning profile that is configured for push.
2)your server needs to sign its communications to APNS with an SSL certificate.
3)Each push app needs its own unique ID because push notifications are sent to a specific application. (You cannot use a wildcard ID.)
Register for remote notification in app delegate.m-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
 // Let the device know we want to receive push notifications

 [[UIApplication sharedApplication] registerForRemoteNotificationTypes:

(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];

    return YES;
}

The new call to registerForRemoteNotificationTypes: tells the OS that this app wants to receive push notifications.
There is one more thing you need to add in order to be able to receive push notifications. Add the following to AppDelegate.m:
To obtain deviceToken:- 
(void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
 NSLog(@"My token is: %@", deviceToken);
}
- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error
{
NSLog(@"Failed to get token, error: %@", error);
}
When your app registers for remote (push) notifications, it tries to obtain a “device token”. This is a 32-byte number that uniquely identifies your device. 
The didRegisterForRemoteNotificationsWithDeviceToken method is usually called right away after you’ve registered for push notifications. Usually, but not always. 

Receiving Push Messages in the App
So what happens when your iPhone receives a push notification? There are three possible situations:
The app is currently running in the foreground.
The app is closed, either the iPhone’s home screen is active or some other app is running.
The iPhone is locked.
The final modifications to our app all take place in AppDelegate.m, as the application delegate is the one who receives the push notifications. There are two places for this:
application:didFinishLaunchingWithOptions:. If your app wasn’t running when the notification came in, it is launched and the notification is passed as part of the launchOptions dictionary.
application:didReceiveRemoteNotification:. This method is invoked if your app is active when a notification comes in. On iOS 4.0 or later, if your app was suspended in the background it is woken up and this method is also called. You can use UIApplication’s applicationState property to find out whether your app was suspended or not.
Add Observer to register for events in order to call back when notification recieves :
[[NSNotificationCenter defaultCenter]  addObserver:menuVCDriver selector:@selector(showBookViewController:) name:@"bookingDetails" object:nil];
post the notification as app receives it :
[[NSNotificationCenter defaultCenter] postNotificationName:@"bookingDetails" object:self userInfo:dictBookingDetails];

For more information you can check this tutorial:

http://www.raywenderlich.com/32960/apple-push-notification-services-in-ios-6-tutorial-part-1

Friday 15 January 2016

Searching the table view content

Hello... friends, I am here with a new blog. Today I am discussing how to search for specific content in a UITableView when it has a lot of data .

When we have a large number of data to display in UITableView, then looking for a particular  data is very tedious task. To overcome this problem we can enable a search functionality on the tableview. As I used this in one of my app.

UISearchBar provide a functionality to search the content we look for from the specific view (here it is table view).It is very simple to use UISearchBar in an app. Steps are as follows:

1) Add UISearchBar property in your view controller's interface file  and set its delegate

                     self.searchBar = [[UISearchBar alloc]init];
        self.searchBar.showsCancelButton = YES;
        self.searchBar.delegate = self;
        [self.searchBar becomeFirstResponder];

2)I am displaying the search bar in navigation bar as:
        self.navigationItem.titleView = self.searchBar;


3) Implement the delegate method of the  UISearchBar 

-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
    
    //hide the searchBar, and reload the tableview to show original data
    isSearching = NO;    
}
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    //start the search
    NSLog(@"search btn cliked");
    [searchBar resignFirstResponder];
    [self searhTableList];
    
}
-(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{
    NSLog(@"search should begin editing");
    isSearching = YES;
    return YES;
}

Here when user type some text to search and tap the search button, we are searching the table list and then if any matches occur we add the specific row's data to a mutable array and we reload the table view to show the filtered result.

To store the filtered result, declare a mutable array in interface file , here it is filteredArrData,


-(void)searhTableList{
    NSString *searchString = self.searchBar.text;
   
    for (NSString *data in self.arr_tableData) {
         
        NSRange range = [data rangeOfString:searchString options:NSCaseInsensitiveSearch];
         
        NSComparisonResult result = [data compare:searchString options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:range];
        
        if (result == NSOrderedSame) {
            [self.filteredArrData addObject:data];
        }
    }

    if (self.filteredArrData.count == 0) {
         //show an alert, no results found
        
    }else{
         //reload the tableview
          
     }

That 's it. Here I am showing how to implement search on table's data.I am not showing how to use same table view to show the search result and original data(full data to show in table view). It's up to you  how to use it .
        




Friday 8 January 2016

Enumeration in Objective C

Enum (enumerated type) is an user-defined type that is used in Objective-C to represent the data that can be stored in one of several predefined values (all of them are int).


Let’s imagine you have to implement a simple UIView that has three different visual modes: default (grey), green and red. Each mode is represented to user by three different pictures: my_mode_grey.pngmy_mode_green.png and my_mode_red.png. All of three final views would have some common behavior. 

To understand it , lets take an example:

1) First create an enum

enum myMode {
         
         myViewModeDefault,  //== 0(by default)
         myViewModeGreen,   //  == 1 (incremented by one from previous)
         myViewModeBlue    // ==2 

};


It means that every “mode” is simply described by the unique int value (unique inside current enum). Each next enum mode is incremented by 1 by default. You can also set the enum mode values by yourself (if you really need it):


enum myMode {
         
         myViewModeDefault = 5,  //== 5 (redefined)
         myViewModeGreen,   //  == 6 (incremented by one from previous)
         myViewModeBlue  = 9   // ==2 

};

2) Now lets create a class:

#import <UIKit/UIKit.h>
@interface MyViews:UIView{

    enum myMode mode ;

}
-(id)initWithMode:(enum myMode)amode;

@end


3) implement our custom initializer:

@implementation MyViews

-(id)initWithMode:(enum myMode)amode{
      
             self = [super init];
            
             if(self){
                   mode = amode;
             

       //to store different images

      NSString  *imageName;
    
         switch(amode){
             
       case myViewModeDefault:
                               imageName = @"tom.png"
                                break; 
       case myViewModeDefault:
                               imageName = @"jery.png"
                                break; 

     case myViewModeDefault:
                               imageName = @"draculla.png"
                                break; 

     default:
                  imageName = @"sorry.png"
                                break; 
}

      UIImageView *imageView = [UIImageView alloc]initWithImage:[UIImage imageNamed: imageName]];

     [self addSubview:imageView];
}

return self;

}

@end



Thats it, how easy it is.