Login with pinterest in iOS

2019-06-23 19:21发布

问题:

There is an explanation for pin in Pinterest for developers.

But I still have got the two following questions:

  • How to log in?
  • How to get a response of logged in user from the server after logging in?

I have gone through towards all demo available on google & stack overflow ,all they explain by using web view. but using web view, we can't get response of logged in user and in some Demo they have explained just how to pin it ?

In my app, I want login with pinterest.

Any help with the questions would be greatly appreciated.

回答1:

The SDK provided by Pinterest only has the Pin it functionality, as described on their developer website. You cannot login with that SDK, but you can Pin images with it.

Pinterest has no official API for logging in, but they do use OAuth2 protocol. So you might have to write your own UIWebView handler that will allow the logging in. This will require research of OAuth2 protocol and storing cookies. Not a very easy task.

Check this GitHub project for a starting point.

There is some more information in the following questions:

  • Log in with Pinterest
  • pinterest api documentation


回答2:

The SDK provided by Pinterest You cannot login,but you can Pin images with it.there is no api available for the Pinterest logging so you not get the any response. you pin on the Pinterest using the below code.

- (IBAction)pinit:(id)sender {
    [self postToPinterest];
}

- (IBAction)closeWebVIew:(id)sender {
    [webViewPinterest setHidden:YES];
}

- (NSString*) generatePinterestHTML {
   NSString *description = @"Post your description here";
 NSURL* sUrl = [NSString stringWithFormat:@"http://flower3.jpg"];// pass your link here with your image name

    NSLog(@"URL:%@", sUrl);
   NSString *protectedUrl = ( NSString *)CFURLCreateStringByAddingPercentEscapes(NULL,( CFStringRef)sUrl, NULL, (CFStringRef)@"!*'\"();:@&=+$,/?%#[]% ",CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding));
    NSLog(@"Protected URL:%@", protectedUrl);
    NSString *imageUrl = [NSString stringWithFormat:@"\"%@\"", sUrl];
    NSString *buttonUrl = [NSString stringWithFormat:@"\"http://pinterest.com/pin/create/button/?url=www.flor.com&media=%@&description=%@\"", protectedUrl, description];

    NSMutableString *htmlString = [[NSMutableString alloc] initWithCapacity:1000];
    [htmlString appendFormat:@"<html> <body>"];
    [htmlString appendFormat:@"<p align=\"center\"><a href=%@ class=\"pin-it-button\" count-layout=\"horizontal\"><img border=\"0\" src=\"http://assets.pinterest.com/images/PinExt.png\" title=\"Pin It\" /></a></p>", buttonUrl];
    [htmlString appendFormat:@"<p align=\"center\"><img width=\"400px\" height = \"400px\" src=%@></img></p>", imageUrl];
    [htmlString appendFormat:@"<script type=\"text/javascript\" src=\"//assets.pinterest.com/js/pinit.js\"></script>"];
    [htmlString appendFormat:@"</body> </html>"];
    return htmlString;
}

- (void) postToPinterest {
    NSString *htmlString = [self generatePinterestHTML];
    NSLog(@"Generated HTML String:%@", htmlString);
    webViewPinterest.backgroundColor = [UIColor clearColor];
    webViewPinterest.opaque = NO;
    if ([webViewPinterest isHidden]) {
        [webViewPinterest setHidden:NO];
    }
    [webViewPinterest loadHTMLString:htmlString baseURL:nil];
    //[webViewPinterest loadHTMLString:@"<img src=images.png>" baseURL:nil];
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    return YES;
}

- (void)webViewDidStartLoad:(UIWebView *)webView {
    [busyIndicator startAnimating];
}

-(void)webViewDidFinishLoad:(UIWebView *)webView {
    [busyIndicator stopAnimating];
}


回答3:

Follow the steps 1 to 4 from here

Put following in Appdelgate.swift

import PinterestSDK

Add following line in didFinishLaunchingWithOptions method of appdelegate

PDKClient.configureSharedInstanceWithAppId("your-app-id")

add following on your login view controller

import PinterestSDK

  func PinterestLogin() {

    PDKClient.sharedInstance().authenticateWithPermissions([PDKClientReadPrivatePermissions,PDKClientReadPublicPermissions,PDKClientReadRelationshipsPermissions,PDKClientWritePublicPermissions,PDKClientWritePrivatePermissions,PDKClientWriteRelationshipsPermissions], withSuccess: { (success :PDKResponseObject!) -> Void in

        let user = success.user()
        print(user.identifier)
        print(user.image?.url)
        print(user.username);
        print(user.firstName);
        print(user.lastName);
        print(user.biography);
        print(user.largestImage().url)
        print(user.smallestImage().url)


        }) { (error: NSError!) -> Void in
            print(error.description)
    }


}

 func PinterestLogout() {
 PDKClient.clearAuthorizedUser()
}