Likes¶
Like UI Elements¶
Like Button¶
A standalone like button is provided. See the like button section for more info.
Working with Likes¶
Liking¶
To create a like programmatically you simply call the like method on SZLikeUtils
As with comments and shares, you can also manually specify how the like is to be propagated to 3rd party networks such as Facebook and Twitter
- (void)like {
SZEntity *entity = [SZEntity entityWithKey:@"some_key_or_url" name:@"Something"];
SZLikeOptions *options = [SZLikeUtils userLikeOptions];
options.willAttemptPostingToSocialNetworkBlock = ^(SZSocialNetwork network, SZSocialNetworkPostData *postData) {
[postData.params setObject:@"Custom message" forKey:@"message"];
};
[SZLikeUtils likeWithEntity:entity options:nil networks:SZAvailableSocialNetworks() success:^(id<SZLike> like) {
NSLog(@"Created like: %d", [like objectID]);
} failure:^(NSError *error) {
NSLog(@"Failed creating like: %@", [error localizedDescription]);
}];
}
Unliking¶
You can also remove a previous like from an entity
- (void)unlike {
SZEntity *entity = [SZEntity entityWithKey:@"some_key_or_url" name:@"Something"];
[SZLikeUtils unlike:entity success:^(id<SZLike> like) {
NSLog(@"Deleted like: %d", [like objectID]);
} failure:^(NSError *error) {
NSLog(@"Failed deleting like: %@", [error localizedDescription]);
}];
}
Retreiving Likes¶
You can retrieve existing likes by User or Entity
List likes by User
- (void)getLikesByUser {
[SZLikeUtils getLikesForUser:nil start:nil end:nil success:^(NSArray *likes) {
NSLog(@"Got likes: %@", likes);
} failure:^(NSError *error) {
NSLog(@"Failed getting likes: %@", [error localizedDescription]);
}];
}
Get the like for a user on a given an entity, if it exists
- (void)getLikeForUserOnEntity {
SZEntity *entity = [SZEntity entityWithKey:@"some_key_or_url" name:@"Something"];
[SZLikeUtils getLikeForUser:nil entity:entity success:^(id<SZLike> like) {
if (like != nil) {
NSLog(@"Liked with like descriptor: %@", like);
} else {
NSLog(@"Not liked");
}
} failure:^(NSError *error) {
NSLog(@"Failed getting like: %@", [error localizedDescription]);
}];
}
Get likes by all users on a specific entity
- (void)getLikesByEntity {
SZEntity *entity = [SZEntity entityWithKey:@"some_key_or_url" name:@"Something"];
[SZLikeUtils getLikesForEntity:entity start:nil end:nil success:^(NSArray *likes) {
NSLog(@"Got likes: %@", likes);
} failure:^(NSError *error) {
NSLog(@"Failed getting likes: %@", [error localizedDescription]);
}];
}
List all likes in the application
- (void)listLikesByApplication {
[SZLikeUtils getLikesByApplicationWithFirst:nil last:nil success:^(NSArray *likes) {
NSLog(@"Fetched likes: %@", likes);
} failure:^(NSError *error) {
NSLog(@"Failed: %@", [error localizedDescription]);
}];
}
Responding to changes in like state¶
You can choose to be notified when likes change state
- (void)didCreate:(NSNotification*)notification {
NSArray *objects = [[notification userInfo] objectForKey:kSZCreatedObjectsKey];
id<SZLike> like = [objects lastObject];
if ([like conformsToProtocol:@protocol(SZLike)]) {
NSLog(@"Liked %@", like.entity);
}
}
- (void)didDelete:(NSNotification*)notification {
NSArray *objects = [[notification userInfo] objectForKey:kSZDeletedObjectsKey];
id<SZLike> like = [objects lastObject];
if ([like conformsToProtocol:@protocol(SZLike)]) {
NSLog(@"Unliked %@", like.entity);
}
}
- (void)respondToLikeChanges {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didCreate:) name:SZDidCreateObjectsNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didDelete:) name:SZDidDeleteObjectsNotification object:nil];
}