Boominadha Prakash M
Boominadha Prakash M

Reputation: 153

How to share image in Pinterest using latest iOS SDK

I want to share the image in Pinterest. I have configured Pinterest iOS SDK without CocoaPods.

I have written code that is making me to redirect to Pinterest app and getting authorisation. But after that I didn't get any response.

In previous version of iOS SDK, we have to only pass the url of that image. But now it is asking Board id also.

I don't know how to get the board id and share the image in Pinterest since I am not getting any response from success block.

This is the code I am using.

[[PDKClient sharedInstance] authenticateWithPermissions:[NSArray arrayWithObjects:PDKClientReadPublicPermissions, nil] withSuccess:^(PDKResponseObject *responseObject) {  

        NSLog(@"Response Object:%@",responseObject);

    } andFailure:^(NSError *error) {

        NSLog(@"Error:%@",error);
}];

I am trying it for past one week. Please suggest me where I am doing mistakes.

Thank you.

Upvotes: 5

Views: 2176

Answers (3)

CrazyPro007
CrazyPro007

Reputation: 1062

#import <PinterestSDK/PDKPin.h>

-(void)shareOnPinterestUsingSDKWithText:(NSString*)text andURL:(NSURL*)imageUrl Image:(UIImage*)imagePinterest completionHandler:(void (^)(BOOL completed))completion{

    [PDKPin pinWithImageURL:imageUrl link:imageUrl suggestedBoardName:@"" note:text withSuccess:^{
        NSLog(@"success");
        completion(true);
    } andFailure:^(NSError *error) {
        NSLog(@"error %@", error);
        completion(false);
    }];
}


TODO:- 

1) Install SDK using cocopods

pod 'PinterestSDK', '~> 1.0'

2) Add pinterest app id in Appdelegate

static NSString* const kPinterestAppId = @“{PINTEREST_ID}”;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{

[PDKClient configureSharedInstanceWithAppId: kPinterestAppId];
Return true;
}

3) Add pinterest key in info.plist

<key>CFBundleURLTypes</key>
    <array>
        <dict>
            <key>CFBundleURLName</key>
            <string>${BUNDLE_IDENTIFIER}</string>
            <key>CFBundleURLSchemes</key>
            <array>
                <string>${APP_URL_SCHEME}</string>
                <string>pdk${PINTEREST_ID}</string>
            </array>
        </dict>
    </array>

===========================================================

<key>LSApplicationQueriesSchemes</key>
    <array>
    <string>pinterestsdk.v1</string>
    </array>

===========================================================

    <key>PinterestAppId</key>
    <string>${PINTEREST_ID}</string>

===========================================================

4) For handling callback when pinterest sharing done and control return to app

-(BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options
{
    NSString *pinterestKey = [[[NSBundle mainBundle] infoDictionary] valueForKey:@"PinterestAppId"];
    if (pinterestKey && ![pinterestKey isKindOfClass:[NSNull class]])
    {
        pinterestKey = [NSString stringWithFormat:@"pdk%@", pinterestKey];
        if ([urlStr rangeOfString:pinterestKey].location != NSNotFound)
        {
            return [[PDKClient sharedInstance] handleCallbackURL:url];
        }
    }
}

Upvotes: 0

Boominadha Prakash M
Boominadha Prakash M

Reputation: 153

I solved this issue and I would like to say thank you to the people who helped me. Now, I can share the image in Pinterest. I have referred the example app from this Github link https://github.com/pinterest/ios-pdk to share the image in Pinterest. Here is the steps I followed.

1) I installed the Pinterest SDK using Cocoapods.

2) I have added the below line in didFinishLaunchingWithOptions

[PDKClient configureSharedInstanceWithAppId:@"1234566"];

3) I have added the below two functions in AppDelegate.m file

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
    return [[PDKClient sharedInstance] handleCallbackURL:url];
}

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary *)options
{
    return [[PDKClient sharedInstance] handleCallbackURL:url];
}

4) I have added the below code in image share button action.

[PDKPin pinWithImageURL:[NSURL URLWithString:@"https://about.pinterest.com/sites/about/files/logo.jpg"] link:[NSURL URLWithString:@"https://www.pinterest.com"]
                   suggestedBoardName:@"Testing" note:@"The Pinterest Logo" withSuccess:^
               {
                   // weakSelf.resultLabel.text = [NSString stringWithFormat:@"successfully pinned pin"];
               }
                           andFailure:^(NSError *error)
               {
                   //weakSelf.resultLabel.text = @"pin it failed";
                   NSLog(@"Error:%@",error);

               }];

Upvotes: 3

Aditya
Aditya

Reputation: 115

You can fetch the board info using Pinterest api via following code :

PDKClient.sharedInstance().getAuthenticatedUserBoardsWithFields(NSSet(array: ["id","image","description","name"]) as Set<NSObject>, success: { 
(responseObject: PDKResponseObject!) -> Void in

        self.currentResponseObject = responseObject
        self.boardsArray = responseObject.boards()

        print("self.boards are \(self.boardsArray)") //Contains identifiers(board-id) which is used for image sharing

        })
        {
            (err :NSError!) -> Void in
            print("e rror NSError: \(err)")
            self.hideHUD()
    }

Upvotes: 1

Related Questions