5

i want to post image on pinterest using this link but i change coding Little bit according to my own Requirement here is my MainClass code

- (void)pInterest {
      UIImage *myImage;
      myImage=imgView.image;
      WebViewController *webViewController = [[WebViewController alloc] initWithNibName:@"WebViewController" bundle:nil];
      webViewController.mypimage = myImage;
     [[[[UIApplication sharedApplication] keyWindow] rootViewController] presentModalViewController:webViewController animated:YES];
}

So after passing image to my WebViewController now my WebViewController class code is

- (void)viewDidLoad {
     //imageview.image=mypimage;
     [super viewDidLoad];
     NSString *description = @"Post your description here";

     // Generate urls for button and image
     NSString *sUrl = [NSString stringWithFormat:@"http://d30t6wl9ttrlhf.cloudfront.net/media/catalog/product/Heros/mypimage"];
     // 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>"];
     [mywebview setBackgroundColor:[UIColor clearColor]];
     [mywebview loadHTMLString:htmlString baseURL:nil];
     [mywebview setOpaque:NO];
}

When i use the above coding my webview show no image and my webview looks like below screenshow enter image description here enter image description here

When i assign the same image instance to image view in WebViewController its working imageview.image=mypimage;

So can some one suggest me why my webveiw not show the image.Thanks.

Community
  • 1
  • 1
jamil
  • 2,419
  • 3
  • 37
  • 64
  • 1
    I should say that this is not a proper button url. Please try to make sure the url exists for the resource you are trying to load before actually posting the question. –  Feb 01 '13 at 22:22
  • when i click on pinit button which is on first screenshot it open second screenshot which means there is no problem about url.so only problem there is that it not pass my image. – jamil Feb 02 '13 at 06:02

3 Answers3

8

use this code...... Hope it will help you.

- (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://4.bp.blogspot.com/-w4oTZjlpgwo/T5_pi-KJPuI/AAAAAAAAAoM/rKm3E0XCbgY/s1600/red_rose_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];
}
Sudha Tiwari
  • 2,499
  • 26
  • 50
  • 1
    Flagged your last post as duplicate and was removed by the moderator, so my compensation here, +1.Please dont post links as answers. – DD_ Apr 01 '13 at 10:17
7

Before Posting a answer, I would like to pay special thanks to @Sudha who gave me idea that without posting image on server we can not post local image from app directly on pinterest. Hence moving in this way idea when I posted images on server and assigned appropriate URL Address to pinit button, then it worked for me. Cheers! when I started search about iOS app integration with pinterest, I found very little help and material in this regard. I think most of the developers used this Method, but when I used this above quoted method it didn't post my local app images on pinterest. At the end, I would like to post my answers for those who will be most likely to post local images from app on pinterest.

Steps

  1. Create a New Class WebViewController and add Following things.

    #import <UIKit/UIKit.h>
    
    @interface WebViewController : UIViewController
    {
     IBOutlet UIActivityIndicatorView *UIHelper;
     IBOutlet UIWebView *mywebview;
     UIImage *mypimage;
     }
     @property(nonatomic,retain)UIActivityIndicatorView *UIHelper;
     @property(nonatomic,retain)UIWebView *mywebview;
     @property(nonatomic,retain) UIImage *mypimage;
     -(IBAction)closeClicked:(id)sender ;
     @end
    
  2. Now WebViewController.m file Code

    #import "WebViewController.h"
    
    @interface WebViewController ()
    
    @end
    
     @implementation WebViewController
     @synthesize UIHelper,mypimage,mywebview;
     NSString *username;
    
    -(IBAction)closeClicked:(id)sender {
     [self dismissModalViewControllerAnimated:YES];
     }
     - (void)webViewDidStartLoad:(UIWebView *)webView {
       [UIHelper startAnimating];
       }
    -(void)webViewDidFinishLoad:(UIWebView *)webView {
     [UIHelper stopAnimating];
      }
    - (void)viewDidLoad
     {
       username = @"j&D";
    [username retain];
    
    NSData *imageData = UIImageJPEGRepresentation(mypimage, 90);
    NSString *urlString = @"ServerURl/cap.php";
    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
    [request setURL:[NSURL URLWithString:urlString]];
    [request setHTTPMethod:@"POST"];
    NSString *boundary = @"---------------------------14737809831466499882746641449";
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
    [request addValue:contentType forHTTPHeaderField: @"Content-Type"];
    NSMutableData *body = [NSMutableData data];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@.jpg\"\r\n",username] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[NSData dataWithData:imageData]];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [request setHTTPBody:body];
    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
        NSLog(@"serverurl :%@",returnString);
    
        NSString *htmlString = [self generatePinterestHTML];
        NSLog(@"Generated HTML String:%@", htmlString);
        mywebview.backgroundColor = [UIColor clearColor];
        mywebview.opaque = NO;
        if ([mywebview isHidden]) {
        [mywebview setHidden:NO];
        }
        [mywebview loadHTMLString:htmlString baseURL:nil];
         [super viewDidLoad];
         }
        - (NSString*) generatePinterestHTML {
          NSString *description = @"Post your description here";
          NSString *sUrl = [NSString stringWithFormat:@"SerVerUrl/cap/j&D.jpg"];
          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;
           }
          @end
    
  3. Create a method to call when user taps your "Pinit" button, which shows that webView with the image, that you will post and the "Pinit" button on the UIWebView. This is my code:

     -(void)pInterest
     {
       UIImage *myImage;
       myImage=imgView.image;
       WebViewController *webViewController = [[WebViewController alloc] initWithNibName:@"WebViewController" bundle:nil];
       webViewController.mypimage = myImage;
       [[[[UIApplication sharedApplication] keyWindow] rootViewController] presentModalViewController:webViewController animated:YES];
    }
    

    Note: In the Above Code Replace SerVerUrl with your Own Server url.

To Make description Clearer, I have provided few Screenshots at the end to remove any ambiguities and misconceptions that may arise.

enter image description hereenter image description hereenter image description here

For any query, please feel free to comment!

Community
  • 1
  • 1
jamil
  • 2,419
  • 3
  • 37
  • 64
  • Bob Apple and Sudha Is there any way to fit sharing screen in my view means in 320X480 for Pinterest sharing? – Nikunj Jadav Feb 19 '13 at 09:19
  • Explain it what you want please ? – jamil Feb 19 '13 at 09:40
  • @BobApple I want fit in my screen means image and discription all are fit in my screen so plz give me any link or code and 1more query Is this possible to share video via Pinterest?Yes then How plz? – Nikunj Jadav Feb 19 '13 at 10:08
0

You can use the Pinterest iOS SDK

Pinterest *_pinterest = [[Pinterest alloc] initWithClientId:pinterestKey];
[_pinterest createPinWithImageURL:@"http://placekitten.com/500/400"
                        sourceURL:@"http://placekitten.com"
                        description:@"Pinning from Pin It Demo"];

For this however, you'll need to have the Pinterest app on the device.

rounak
  • 9,217
  • 3
  • 42
  • 59