Permalink
Browse files

Pinterest sharer migrated to a new sdk

SUBMODULE ADD: PinterestSDK, AFNetworking
  • Loading branch information...
VilemKurz committed Dec 29, 2015
1 parent 76c391c commit 2a8ad5b87e3c862568a627bfbf0d5c68aad6b545
Showing with 704 additions and 122 deletions.
  1. +7 −0 .gitmodules
  2. +2 −0 Classes/ShareKit/Configuration/DefaultSHKConfigurator.h
  3. +6 −2 Classes/ShareKit/Configuration/DefaultSHKConfigurator.m
  4. +7 −0 Classes/ShareKit/Localization/en.lproj/Localizable.strings
  5. +248 −16 Classes/ShareKit/Sharers/Services/Pinterest/SHKPinterest.m
  6. +0 −1 Frameworks/Pinterest.embeddedframework/Pinterest.framework/Headers
  7. +0 −1 Frameworks/Pinterest.embeddedframework/Pinterest.framework/Pinterest
  8. +0 −1 Frameworks/Pinterest.embeddedframework/Pinterest.framework/Resources
  9. +0 −99 Frameworks/Pinterest.embeddedframework/Pinterest.framework/Versions/A/Headers/Pinterest.h
  10. BIN Frameworks/Pinterest.embeddedframework/Pinterest.framework/Versions/A/Pinterest
  11. BIN Frameworks/Pinterest.embeddedframework/Pinterest.framework/Versions/A/Resources/Info.plist
  12. BIN ...work/Pinterest.framework/Versions/A/Resources/PinterestSDKResources.bundle/Btn_PinIt_Disabled.png
  13. BIN ...k/Pinterest.framework/Versions/A/Resources/PinterestSDKResources.bundle/Btn_PinIt_Disabled@2x.png
  14. BIN ...mework/Pinterest.framework/Versions/A/Resources/PinterestSDKResources.bundle/Btn_PinIt_Normal.png
  15. BIN ...ork/Pinterest.framework/Versions/A/Resources/PinterestSDKResources.bundle/Btn_PinIt_Normal@2x.png
  16. BIN ...ework/Pinterest.framework/Versions/A/Resources/PinterestSDKResources.bundle/Btn_PinIt_Pressed.png
  17. BIN ...rk/Pinterest.framework/Versions/A/Resources/PinterestSDKResources.bundle/Btn_PinIt_Pressed@2x.png
  18. BIN ...s/Pinterest.embeddedframework/Pinterest.framework/Versions/A/Resources/en.lproj/InfoPlist.strings
  19. +0 −1 Frameworks/Pinterest.embeddedframework/Pinterest.framework/Versions/Current
  20. +0 −1 Frameworks/Pinterest.embeddedframework/Resources/PinterestSDKResources.bundle
  21. +432 −0 ShareKit.xcodeproj/project.pbxproj
  22. +1 −0 Submodules/AFNetworking
  23. +1 −0 Submodules/ios-pdk
View
@@ -37,3 +37,10 @@ url=https://github.com/google/google-toolbox-for-mac.git
[submodule "Submodules/gtm-http-fetcher"]
path = Submodules/gtm-http-fetcher
url = https://github.com/google/gtm-http-fetcher.git
[submodule "Submodules/ios-pdk"]
path = Submodules/ios-pdk
url = https://github.com/pinterest/ios-pdk.git
[submodule "Submodules/AFNetworking"]
path = Submodules/AFNetworking
url = https://github.com/AFNetworking/AFNetworking.git
branch = 2.x
@@ -89,6 +89,8 @@
- (NSNumber *)instagramOnly;
- (NSString*)youTubeConsumerKey;
- (NSString*)youTubeSecret;
- (NSNumber *)pinterestAllowUnauthenticatedPins;
- (NSString *)pinterestAppId;
- (NSNumber *)useAppleShareUI;
- (NSNumber*)shareMenuAlphabeticalOrder;
- (NSString*)barStyle;
@@ -418,8 +418,12 @@ - (NSNumber *)imgurAnonymousUploads {
return @NO;
}
///You can get Pinterest client ID from https://developers.pinterest.com/manage/
- (NSString *)pinterestClientId {
///You can choose to allow user to log in (via OAuth) and share within your app, or choose to create unauthenticated pins - this always mean trip to Safari or Pinterest app if present.
- (NSNumber *)pinterestAllowUnauthenticatedPins {
return @YES;
}
///You can get Pinterest app ID from https://developers.pinterest.com
- (NSString *)pinterestAppId {
return @"";
}
@@ -290,3 +290,10 @@
//Tumblr
"Select blog" = "Select blog";
//Pinterest
"Pinterest" = "Pinterest";
"Board" = "Board";
"Select board" = "Select board";
"Failed to fetch boards." = "Failed to fetch boards.";
@@ -25,15 +25,41 @@
#import "SHKPinterest.h"
#import "SharersCommonHeaders.h"
#ifdef COCOAPODS
#import "Pinterest.h"
#else
#import <Pinterest/Pinterest.h>
#endif
#import "PinterestSDK.h"
#import "PDKPin.h"
#import "PDKUser.h"
static NSString *const SHKPinterestUserInfoKey = @"kSHKPinterestUserInfo";
static NSString *const SHKPinterestParsedUserObjectDataKey = @"data";
static NSString *const SHKPinterestBoardIdField = @"id";
static NSString *const SHKPinterestBoardNameField = @"name";
static NSString *const SHKPinterestBoardCustomItemKey = @"board";
@interface SHKPinterest () <SHKFormOptionControllerOptionProvider>
@end
@implementation SHKPinterest
#pragma mark -
#pragma mark - Initialization
+ (void)setupPinterestSDK {
[PDKClient configureSharedInstanceWithAppId:SHKCONFIG(pinterestAppId)];
}
- (instancetype)init {
self = [super init];
if (self) {
[SHKPinterest setupPinterestSDK];
}
return self;
}
#pragma mark -
#pragma mark Configuration : Service Definition
@@ -49,14 +75,75 @@ + (BOOL)canShareItem:(SHKItem *)item {
return isOfImageType || isPictureURI;
}
+ (BOOL)requiresAuthentication { return NO; }
+ (BOOL)requiresAuthentication {
if ([SHKCONFIG(pinterestAllowUnauthenticatedPins) boolValue]) {
return NO;
} else {
return YES;
}
}
+ (BOOL)canAutoShare { return NO; }
#pragma mark -
#pragma mark Configuration : Dynamic Enable
#pragma mark Authorization
- (BOOL)isAuthorized {
BOOL result = [[PDKClient sharedInstance] oauthToken] != nil;
if (!result) {
[[PDKClient sharedInstance] silentlyAuthenticateWithSuccess:[self authenticationSuccessBlock]
andFailure:^(NSError *error) {
[[NSUserDefaults standardUserDefaults] removeObjectForKey:SHKPinterestUserInfoKey];
}];
}
return result;
}
- (void)authorizationFormShow {
[[PDKClient sharedInstance] authenticateWithPermissions:@[PDKClientReadPublicPermissions,
PDKClientWritePublicPermissions,
PDKClientReadPrivatePermissions,
PDKClientWritePrivatePermissions]
withSuccess:[self authenticationSuccessBlock]
andFailure:^(NSError *error) {
SHKLog(@"pinterest auth failure %@", [error description]);
[self authDidFinish:NO];
}];
}
- (PDKClientSuccess)authenticationSuccessBlock {
PDKClientSuccess result = ^(PDKResponseObject *responseObject) {
[[NSUserDefaults standardUserDefaults] setObject:responseObject.parsedJSONDictionary[SHKPinterestParsedUserObjectDataKey] forKey:SHKPinterestUserInfoKey];
[self authDidFinish:YES];
[self restoreItem];
[self tryPendingAction];
};
return result;
}
+ (void)logout {
[PDKClient clearAuthorizedUser];
[PDKClient sharedInstance].oauthToken = nil;
[[NSUserDefaults standardUserDefaults] removeObjectForKey:SHKPinterestUserInfoKey];
}
+ (BOOL)canShare {
NSString *clientId = SHKCONFIG(pinterestClientId);
return clientId && [[[Pinterest alloc] initWithClientId:clientId] canPinWithSDK];
+ (NSString *)username {
NSString *result = nil;
NSDictionary *parsedResponseObject = [[NSUserDefaults standardUserDefaults] objectForKey:SHKPinterestUserInfoKey];
if (parsedResponseObject) {
PDKUser *userInfo = [PDKUser userFromDictionary:parsedResponseObject];
result = [[NSString alloc] initWithFormat:@"%@ %@", userInfo.firstName, userInfo.lastName];
}
return result;
}
#pragma mark -
@@ -65,7 +152,29 @@ + (BOOL)canShare {
- (NSArray *)shareFormFieldsForType:(SHKShareType)type
{
SHKFormFieldLargeTextSettings *descriptionField = [SHKFormFieldLargeTextSettings label:SHKLocalizedString(@"Description") key:@"title" start:self.item.title item:self.item];
return @[descriptionField];
if ([SHKPinterest requiresAuthentication]) {
SHKFormFieldOptionPickerSettings *boardPicker = [SHKFormFieldOptionPickerSettings label:SHKLocalizedString(@"Board")
key:SHKPinterestBoardCustomItemKey
start:SHKLocalizedString(@"Select board")
pickerTitle:SHKLocalizedString(@"Select board")
selectedIndexes:nil
displayValues:nil
saveValues:nil
allowMultiple:NO
fetchFromWeb:YES
provider:self];
boardPicker.validationBlock = ^ (SHKFormFieldOptionPickerSettings *formFieldSettings) {
BOOL result = [formFieldSettings valueToSave].length > 0;
return result;
};
return @[descriptionField, boardPicker];
} else {
return @[descriptionField];
}
}
#pragma mark -
@@ -83,16 +192,139 @@ - (BOOL) send {
if (![self validateItem])
return NO;
NSString *clientId = SHKCONFIG(pinterestClientId);
Pinterest *pinterest = [[Pinterest alloc] initWithClientId:clientId];
if (![SHKPinterest requiresAuthentication]) {
self.quiet = YES; //callbacks from Safari are not reliable on PinterestSDK, if the app is not installed. Otherwise activity indicator would spin forever
}
if (self.item.URLPictureURI) {
[pinterest createPinWithImageURL:self.item.URLPictureURI sourceURL:self.item.URL description:self.item.title];
if ([SHKPinterest requiresAuthentication]) {
[[PDKClient sharedInstance] createPinWithImageURL:self.item.URLPictureURI
link:self.item.URL
onBoard:[self.item customValueForKey:SHKPinterestBoardCustomItemKey]
description:self.item.title
withSuccess:[self pinCreationSuccessBlock]
andFailure:[self pinCreationFailureBlock]];
} else {
[PDKPin pinWithImageURL:self.item.URLPictureURI
link:self.item.URL
suggestedBoardName:@"board"
note:self.item.title
withSuccess:[self unauthPinCreationSuccessBlock]
andFailure:[self unauthPinCreationFailureBlock]];
}
} else {
[pinterest createPinWithImageURL:self.item.URL sourceURL:nil description:self.item.title];
if ([SHKPinterest requiresAuthentication]) {
[[PDKClient sharedInstance] createPinWithImageURL:self.item.URL
link:nil
onBoard:[self.item customValueForKey:SHKPinterestBoardCustomItemKey]
description:self.item.title
withSuccess:[self pinCreationSuccessBlock]
andFailure:[self pinCreationFailureBlock]];
} else {
[PDKPin pinWithImageURL:self.item.URL
link:nil
suggestedBoardName:nil
note:self.item.title
withSuccess:[self unauthPinCreationSuccessBlock]
andFailure:[self unauthPinCreationFailureBlock]];
}
}
[self sendDidStart];
return YES;
}
- (PDKUnauthPinCreationSuccess)unauthPinCreationSuccessBlock {
PDKUnauthPinCreationSuccess result = ^{
[self sendDidFinish];
};
return result;
}
- (PDKUnauthPinCreationFailure)unauthPinCreationFailureBlock {
PDKUnauthPinCreationFailure result = ^(NSError *error) {
[self sendDidFailWithError:error];
};
return result;
}
- (PDKClientSuccess)pinCreationSuccessBlock {
PDKClientSuccess result = ^(PDKResponseObject *responseObject) {
[self sendDidFinishWithResponse:responseObject.parsedJSONDictionary];
};
return result;
}
- (PDKClientFailure)pinCreationFailureBlock {
PDKClientFailure result = ^(NSError *error) {
[self sendDidFailWithError:error];
};
return result;
}
#pragma mark -
#pragma mark SHKFormOptionControllerOptionProvider
- (void)SHKFormOptionControllerEnumerateOptions:(SHKFormOptionController *)optionController {
NSAssert(self.curOptionController == nil, @"there should never be more than one picker open.");
self.curOptionController = optionController;
[self displayActivity:SHKLocalizedString(@"Loading...")];
NSSet *boardFields = [[NSSet alloc] initWithObjects:SHKPinterestBoardIdField, SHKPinterestBoardNameField, nil];
[[PDKClient sharedInstance] getAuthenticatedUserBoardsWithFields:boardFields
success:^(PDKResponseObject *responseObject) {
[self hideActivityIndicator];
NSDictionary *boardsList = responseObject.parsedJSONDictionary[SHKPinterestParsedUserObjectDataKey];
NSMutableArray *displayValues = [[NSMutableArray alloc] initWithCapacity:10];
NSMutableArray *saveValues = [[NSMutableArray alloc] initWithCapacity:10];
for (NSDictionary *board in boardsList) {
[displayValues addObject:board[SHKPinterestBoardNameField]];
[saveValues addObject:board[SHKPinterestBoardIdField]];
}
[self.curOptionController optionsEnumeratedDisplay:displayValues save:saveValues];
}
andFailure:^(NSError *error) {
[self hideActivityIndicator];
NSHTTPURLResponse *errorResponse = error.userInfo[AFNetworkingOperationFailingURLResponseErrorKey];
if (errorResponse.statusCode == 401) {
//revoked access, login again
[self shouldReloginWithPendingAction:SHKPendingShare];
} else {
SHKLog(@"Failed to fetch boards with error:%@", [error debugDescription]);
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:@"Failed to fetch boards." forKey:NSLocalizedDescriptionKey];
NSError *err = [NSError errorWithDomain:@"PTR" code:1 userInfo:userInfo];
[self.curOptionController optionsEnumerationFailedWithError:err];
}
}];
}
- (void)SHKFormOptionControllerCancelEnumerateOptions:(SHKFormOptionController *)optionController {
[self hideActivityIndicator];
NSAssert(self.curOptionController == optionController, @"there should never be more than one picker open.");
}
@end
Oops, something went wrong.

0 comments on commit 2a8ad5b

Please sign in to comment.