Pergunta

What is the most reliable way currently to allow pinterest sharing from iOS app?

Pinterest API is not public yet and only suggested way to share is their web button.

Foi útil?

Solução

As of May 20th 2013, Pinterest have released an iOS SDK for pinning content.

Check out their Developers site for more information.

Outras dicas

I made a pinterest integration in my iPad app. But, because Pinterest doesn't have an API for posting yet, I used the following method. I just create programmatically an HTML Web Page and add a Pin it button to that page programmatically. Then I show a Web View and allow user to click Pin it once more. These are more explained steps.

1) Create a WebViewController, that has a UIWebView. Add Close button, add UIWebViewDelegateProtocol, spinner, htmlString property.

2) Generate an HTML programmatically to put to that UIWebView, when user clicks your "Pin it" button in your app. In this case I put to the HTML page different images for different products.

- (NSString*) generatePinterestHTMLForSKU:(NSString*)sku {
NSString *description = @"Post your description here";

// Generate urls for button and image
NSString *sUrl = [NSString stringWithFormat:@"http://d30t6wl9ttrlhf.cloudfront.net/media/catalog/product/Heros/%@-1.jpg", sku];
NSLog(@"URL:%@", sUrl);
NSString *protectedUrl = (__bridge NSString *)CFURLCreateStringByAddingPercentEscapes(NULL,(__bridge 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;
}

This is an example of my HTML page generation method.

3) Create a method to call when user taps your "Pin it" button, which shows that webView with the image, that you will post and the "Pin it" button on the UIWebView. This is my example:

- (void) postToPinterest {

NSString *htmlString = [self generatePinterestHTMLForSKU:self.flProduct.sku];
NSLog(@"Generated HTML String:%@", htmlString);
WebViewController *webViewController = [[WebViewController alloc] initWithNibName:@"WebViewController" bundle:nil];
webViewController.htmlString = htmlString;
webViewController.showSpinner = YES;

[[[[UIApplication sharedApplication] keyWindow] rootViewController] presentModalViewController:webViewController animated:YES];
}

4) Put a "Close" button to your WebViewController to close it. Also you can add spinners to track the loading of the UIWebView.

- (IBAction)closeClicked:(id)sender {
[self dismissModalViewControllerAnimated:YES];

}

- (void)webViewDidStartLoad:(UIWebView *)webView {
if (showSpinner) {
    // If we want to show Spinner, we show it everyTime
    [UIHelper startShowingSpinner:self.webView];
}
else {
    // If we don't -> we show it only once (some sites annoy with it)
    if (!spinnerWasShown) {
        [UIHelper startShowingSpinner:self.webView];
        spinnerWasShown = YES; 
    }
}
}

-(void)webViewDidFinishLoad:(UIWebView *)webView {
    [UIHelper stopShowingSpinner];
}

P.S. I used the same method to add Google Plus's +1 button to the iPad app. (It doesn't have posting API too, only readonly API at the moment)

If you only want to share(i.e. pin on a user board), then you can use iphone-URL-Scheme and call the Pinterest application along with the parameters url(URL of the page to pin), media(URL of the image to pin) & description(Description of the page to be pinned). Present a UIAlertView and forward them to appstore to download the official Pinterest application if the user has not installed it.

Reference: http://wiki.akosma.com/IPhone_URL_Schemes#Pinterest

Code to open Pinterest Applicaiton:

NSURL *url = [NSURL URLWithString:@"pinit12://pinterest.com/pin/create/bookmarklet/?url=URL-OF-THE-PAGE-TO-PIN&media=URL-OF-THE-IMAGE-TO-PIN&description=ENTER-YOUR-DESCRIPTION-FOR-THE-PIN"];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
    [[UIApplication sharedApplication] openURL:url];
}else {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Pinterest" message:@"Would you like to download Pinterest Application to share?" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Continue", nil];
    [alert show];
}

UIAlertViewDelegate Method

- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 1){
        NSString *stringURL = @"http://itunes.apple.com/us/app/pinterest/id429047995?mt=8";
        NSURL *url = [NSURL URLWithString:stringURL];
        [[UIApplication sharedApplication] openURL:url];
    }
}

I've looked high and low too. I've even contacted Pinterest Team about an SDK. The closest thing I have found is a PHP wrapper on github https://github.com/kellan/pinterest.api.php.

It's not the best solution though because it is unofficial api and will most likely break.

I tried to integrate Pinterest with Custom URL Scheme, also download the Pinterest application on my device, but not abel to integrate with it.

And the thing is that i don't want to used webView for integration so is it possible to do that, i didn't found any application on app store which have pinterest integration.

I did googling also but all worse, Snapguide also remove pinterest integration from their application.

I used a webView code to pin image , no need to open full Pinterest website,

NSString *urlStr =[NSString stringWithFormat:@"http://pinterest.com/pin/create/bookmarklet/?url=www.<domainname>.com&media=http://<domainname>.files.com/hello.jpg?w=495&description=hello"];

This would be easy but with webView and that i don't want.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top