Pinterest integration in ios app?

2019-07-29 22:51发布

问题:

I am trying to post image from my app on pinterest.From last five days i am looking for perfect solution finally after posting some question on SO now i am inch away to solve my problem.the issue is when i post any image using url its successfuly post my image on pinterest as my below code show.

-(NSString*) generatePinterestHTML {
NSString *description = @"Post your description here";
NSString *sUrl = [NSString stringWithFormat:@"http://4.bp.blogspot.com/-w4oTZjlpgwo/T5_pi-KJPuI/AAAAAAAAAoM/rKm3E0XCbgY/s1600/flower.png"];
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;
}
-(void)viewDidLoad
{
 [super viewDidLoad];
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];
}

below screen shot show my above code result

But when i am trying to upload image from my app its not works fine here is my code

 - (void)viewDidLoad
 {
 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
savedImagePath = [[documentsDirectory stringByAppendingPathComponent:@"savedImage.png"] mutableCopy];
NSLog(@"savedImagePath..%@",savedImagePath);
NSData*  imageData = UIImagePNGRepresentation(mypimage);
[imageData writeToFile:savedImagePath atomically:YES];
[super viewDidLoad];
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];
}
- (NSString*) generatePinterestHTML {
NSString *description = @"Post your description here";
NSString *stringUrl = [NSString stringWithFormat:@"%@",savedImagePath];
NSLog(@"stringUrl:%@", stringUrl);
NSURL *urlToUpload = [[NSURL alloc]initFileURLWithPath:stringUrl];
NSString* sUrl = [NSString stringWithFormat:@"%@",urlToUpload];
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;
}

For above code my screen shot result is link1link2 so please suggest any method to reslove this problem.Thanks

回答1:

After posted images on server and assigned appropriate URL Address to pinit button, then it worked for me. I post my answers herefor those who will be most likely to post local images from app on pinterest.

Hope it will be helpful for all.

Thanks