Powered By Blogger

Thursday, May 23, 2013

What should be the alternative of horizontal scrolling table in iOS?

Hi,
Dear readers,

Now a days, I came across a new requirement of horizontal scrolling table view. I made some research for it and I found a new library as a unique solution for this. The library is known as "iCarousel" library. You just need to add that 2 files into your project source and after then you can import ".h"(header) file in your class where-ever you want to make such view effect.

Download Link : https://github.com/nicklockwood/iCarousel



After downloading two source files "iCarousel.h" & "iCarousel.m". Follow the below steps to integrate it in your project.

*** Step 1: Create a view of iCarousel in header file and bind it with .XIB file.
    - For ex. : IBOutlet iCarousel *carousel;

*** Step 2: Then place its data-source and delegate methods in implementation section of a view controller.

*** Step 3: Set the delegate of a iCarousel.

*** Step 4: Set the type of a iCarousel in ViewDidLoad method.

- (void)viewDidLoad
{
       [super viewDidLoad];

        carousel.type = iCarouselTypeLinear;
        carousel.vertical = NO;
        carousel.scrollOffset = 1.0;
        carousel.centerItemWhenSelected = NO;
        carousel.stopAtItemBoundary = YES;
        carousel.scrollToItemBoundary = NO;
}

***Step 5: And finally put the below code into implementation section of a view controller.

#pragma mark -------------------
#pragma mark iCarousel Delegate methods
#pragma mark -------------------


- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
    //generate 100 buttons
    //normally we'd use a backing array
    //as shown in the basic iOS example
    //but for this example we haven't bothered
    return [businessArr count];
}

- (UIView *)carousel:(iCarousel *)carousel1 viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
        //create new view if no view is available for recycling
        CustomBusinessListingCell_iPad *businessListingView  = (CustomBusinessListingCell_iPad *)[[[NSBundle mainBundle]loadNibNamed:@"CustomBusinessListingCell_iPad" owner:self options:nil] objectAtIndex:0];
        businessListingView.frame =  CGRectMake(0, 0, LIST_VIEW_WIDTH, LIST_VIEW_HEIGHT);
        businessListingView.tag = index;
        businessListingView.lblBusinessName.verticalAlignment = BAVerticalAlignmentBottom;

        businessListingView.btnBook.tag = index;
        [businessListingView.btnBook addTarget:self action:@selector(btnBookClicked:) forControlEvents:UIControlEventTouchUpInside];
       
        businessListingView.btnBookmark.tag = index;
        [businessListingView.btnBookmark addTarget:self action:@selector(priceClicked:) forControlEvents:UIControlEventTouchUpInside];
       
         //NSLog(@"BusinessImage : %@",[[businessArr objectAtIndex:index] valueForKey:@"BusinessImage"]);
       
        [businessListingView setBusinessValue:[businessArr objectAtIndex:index]];
       
        if (index==[businessArr count]-1)
        {
            // ask next page only if we haven't reached last page
            if([businessArr count] < totalRecords)
            {
                if (!isLoadmore)
                {
                    isLoadmore = YES;
                    pageIndex = pageIndex+1;
                    [self loadNewData];
                }
            }
        }
        return businessListingView;
}

-(void)setCarouselPosition
{
    UIInterfaceOrientation orientation = [[UIApplication sharedApplication]statusBarOrientation];
    if ((orientation == UIInterfaceOrientationPortrait) || (orientation == UIInterfaceOrientationPortraitUpsideDown)) {
        [carousel scrollToOffset:0.6 duration:0.0];
    }
    else if((orientation == UIInterfaceOrientationLandscapeLeft) || (orientation == UIInterfaceOrientationLandscapeRight))
    {
        [carousel scrollToOffset:1.1 duration:0.0];
    }
}

- (CGFloat)carousel:(iCarousel *)_carousel valueForOption:(iCarouselOption)option withDefault:(CGFloat)value
{
    //customize carousel display
    switch (option)
    {
        case iCarouselOptionSpacing:
        {
            //add a bit of spacing between the item views
            return value * 1.08f;
        }
        case iCarouselOptionVisibleItems:
        {
            return 5;
        }
        default:
        {
            return value;
        }
    }
}

- (void)carouselDidEndScrollingAnimation:(iCarousel *)carousel1
{
    UIInterfaceOrientation orientation = [[UIApplication sharedApplication]statusBarOrientation];
    if ((orientation == UIInterfaceOrientationPortrait) || (orientation == UIInterfaceOrientationPortraitUpsideDown)) {
        if (carousel1.scrollOffset < 0.6) {
            [UIView beginAnimations:@"" context:nil];
            [UIView setAnimationDuration:0.7];
            [carousel1 scrollToOffset:0.6 duration:0.0];
            [UIView commitAnimations];
        }
    }
    else if ((orientation == UIInterfaceOrientationLandscapeLeft) || (orientation == UIInterfaceOrientationLandscapeRight))
    {
        if (carousel1.scrollOffset < 1.1) {
            [UIView beginAnimations:@"" context:nil];
            [UIView setAnimationDuration:0.7];
            [carousel1 scrollToOffset:1.1 duration:0.0];
            [UIView commitAnimations];
        }
    }
}

- (void)carouselDidEndDragging:(iCarousel *)carousel1 willDecelerate:(BOOL)decelerate
{
    UIInterfaceOrientation orientation = [[UIApplication sharedApplication]statusBarOrientation];
    if ((orientation == UIInterfaceOrientationPortrait) || (orientation == UIInterfaceOrientationPortraitUpsideDown)) {
        if (carousel1.scrollOffset < 0.6) {
            [UIView beginAnimations:@"" context:nil];
            [UIView setAnimationDuration:0.7];
            [carousel1 scrollToOffset:0.6 duration:0.0];
            [UIView commitAnimations];
        }
    }
    else if ((orientation == UIInterfaceOrientationLandscapeLeft) || (orientation == UIInterfaceOrientationLandscapeRight))
    {
        if (carousel1.scrollOffset < 1.1) {
            [UIView beginAnimations:@"" context:nil];
            [UIView setAnimationDuration:0.7];
            [carousel1 scrollToOffset:1.1 duration:0.0];
            [UIView commitAnimations];
        }
    }
}

Thanks,
Nilesh M. Prajapati

How to resolve apple's UDID issue for Push Notification in iPhone?

Hi,
Everyone,

Rumors say that Apple starts to prohibit the use of UDID in iOS applications from iOS 6.0 and onwards. You can use below solution for  < & >=  iOS 6.0 .

- The device token (used by APNS) and the UDID are two different things. They have nothing to do with each other.

- You obtain the device token for push notifications in your app delegate's application:didRegisterForRemoteNotificationsWithDeviceToken: method.


- The documentation notes the following:
The device token is different from the uniqueIdentifier property of UIDevice because, for security and privacy reasons, it must change when the device is wiped.


#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)

NSString *deviceID = nil;

#pragma mark -------------------
#pragma mark UIApplication methods
#pragma mark -------------------

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    if (![[NSUserDefaults standardUserDefaults] valueForKey:@"UUID"]) {
        if (SYSTEM_VERSION_LESS_THAN(@"6.0")) {
            self.deviceID = [[self GetUUID] retain];
        }
        else{
            NSUUID* udid= [UIDevice currentDevice].identifierForVendor;
            self.deviceID = [[udid UUIDString] retain];
        }
        [[NSUserDefaults standardUserDefaults] setValue:self.deviceID forKey:@"UUID"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
    else{
        self.deviceID = [[[NSUserDefaults standardUserDefaults] valueForKey:@"UUID"] retain];
    }
}

#pragma mark -------------------
#pragma mark Push notification methods
#pragma mark -------------------

- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
    //NSLog(@"My token is: %@", deviceToken);
    NSString *devicetoken = [[[[deviceToken description]
                               stringByReplacingOccurrencesOfString:@"<"withString:@""]
                              stringByReplacingOccurrencesOfString:@">" withString:@""]
                             stringByReplacingOccurrencesOfString: @" " withString: @""];
    [self updateToken:devicetoken];
}

- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error
{
    //NSLog(@"Failed to get token, error: %@", error);
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    // get state
    UIApplicationState state=[application applicationState];
    if(state== UIApplicationStateActive)
        self.badgeCount=[[[userInfo valueForKey:@"aps"] valueForKey:@"badge"] intValue];
    else
        self.badgeCount= [UIApplication sharedApplication].applicationIconBadgeNumber;
}

- (NSString *)GetUUID
{
    CFUUIDRef theUUID = CFUUIDCreate(NULL);
    CFStringRef string = CFUUIDCreateString(NULL, theUUID);
    CFRelease(theUUID);
    return [(NSString *)string autorelease];
}

Thanks, 
Nilesh M. Prajapati