Socialize Like Button

Introduction

In v1.7 of the Socialize SDK for iOS is a new stand-alone Like Button. This is simply the “like” capablity of Socialize contained in a self-standing UI element which can be easily customized to suit your app.

images/like_button_liked_unliked.png

Displaying the Like Button

The Like Button is itself a view. To use the Like Button, simply instantiate it and add it to your view hierarchy.

// Header

// Store the action bar in a property on your view controller

#import <Socialize/Socialize.h>

@interface CreateLikeButtonViewController : UIViewController
@property (nonatomic, retain) SZLikeButton *likeButton;
@end
@implementation CreateLikeButtonViewController
@synthesize likeButton = likeButton_;


// Implementation

// Instantiate the action bar in your view controller

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    if (self.likeButton == nil) {
        SocializeEntity *entity = [SocializeEntity entityWithKey:@"hot_ticket_1" name:@"A Hot Ticket"];
        self.likeButton = [[SZLikeButton alloc] initWithFrame:CGRectMake(120, 30, 0, 0) entity:entity viewController:self tabBarStyle:YES];
        
        // Turn of automatic sizing of the like button (default NO)
        // likeButton.autoresizeDisabled = YES;
        
        // Hide the count display
        // likeButton.hideCount = YES;
        
        [self.view addSubview:self.likeButton];
    }
}


@end

Customizing the Like Button

All of the images on the like button can be changed. To do this, just set the appropriate property. The full list of image properties is:

@property (nonatomic, strong) UIImage *inactiveImage;
@property (nonatomic, strong) UIImage *inactiveHighlightedImage;

@property (nonatomic, strong) UIImage *activeImage;
@property (nonatomic, strong) UIImage *activeHighlightedImage;

@property (nonatomic, strong) UIImage *likedIcon;
@property (nonatomic, strong) UIImage *unlikedIcon;

NOTE: For iOS 7, the Like Button has been redesigned with a “flatter” look, something you may want to consider when designing custom images for it. For references, Apple’s guidelines on iOS 7 UI can be found here.

Responding to State Changes

You may find you would like to be notified when the like button changes state. You can accomplish this by listening for the SocializeLikeButtonDidChangeStateNotification as follows:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(likeButtonChanged:) name:SZLikeButtonDidChangeStateNotification object:self.likeButton];
    }
    
    return self;
}

- (void)likeButtonChanged:(NSNotification*)notification {
    NSLog(@"You've changed, %@", [notification object]);
}

Refreshing the Like Button

The Like Button has an explicit refresh call.

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self.likeButton refresh];
}

Changing the Like Button’s Entity

The Like Button’s current entity can be changed by setting the entity property.

- (void)changeLikeButtonEntity {
    SocializeEntity *newEntity = [SocializeEntity entityWithKey:@"new_entity" name:@"New Entity"];
    self.likeButton.entity = newEntity;
}

Note

An explicit refresh is not required when changing the entity