iOS应用程序与pinterest的集成

Pet*_*kop 20 iphone objective-c ios pinterest

目前允许从iOS应用程序共享pinterest 的最可靠方式是什么?

Pinterest API尚未公开,只有建议的分享方式是他们的网络按钮.

wzb*_*zon 11

我在我的iPad应用程序中进行了最好的集成.但是,因为Pinterest还没有用于发布的API,所以我使用了以下方法.我只是以编程方式创建一个HTML网页,并以编程方式将Pin it按钮添加到该页面.然后我显示一个Web视图,并允许用户再次单击Pin it.这些是更多解释的步骤.

1)创建一个具有UIWebView的WebViewController.添加关闭按钮,添加UIWebViewDelegateProtocol,spinner,htmlString属性.

2)当用户点击你的应用程序中的"Pin it"按钮时,以编程方式生成HTML以放入UIWebView.在这种情况下,我为不同的产品添加了不同的图像的HTML页面.

- (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;
}
Run Code Online (Sandbox Code Playgroud)

这是我的HTML页面生成方法的一个示例.

3)创建一个方法,当用户点击"Pin it"按钮时调用该方法,该按钮显示带有图像的webView,您将发布的UIWebView上的"Pin it"按钮.这是我的例子:

- (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];
}
Run Code Online (Sandbox Code Playgroud)

4)在WebViewController上放置一个"关闭"按钮以关闭它.您还可以添加微调器来跟踪UIWebView的加载.

- (IBAction)closeClicked:(id)sender {
[self dismissModalViewControllerAnimated:YES];
Run Code Online (Sandbox Code Playgroud)

}

- (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];
}
Run Code Online (Sandbox Code Playgroud)

PS我使用相同的方法将iPad Plus的+1按钮添加到iPad应用程序.(它也没有发布API,目前只有readonly API)


小智 5

如果您只想共享(即在用户板上固定),那么您可以使用iphone-URL-Scheme并调用Pinterest应用程序以及参数url(要固定的页面的URL), 媒体(图像的URL到pin)&description(要固定的页面的描述).如果用户尚未安装,请提供UIAlertView并将其转发到appstore以下载官方Pinterest应用程序.

参考:http: //wiki.akosma.com/IPhone_URL_Schemes#Pinterest

打开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];
}
Run Code Online (Sandbox Code Playgroud)

UIAlertViewDelegate方法

- (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];
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 我尝试了这段代码,但canOpenUrl总是返回NO,虽然我已经在我的设备上安装了Pinterest.我想也许是因为我已经在facebook上实现了共享功能,google +,...我应该添加到AppDelegate方法 - (BOOL)应用程序:(UIApplication*)应用程序openURL:(NSURL*)url sourceApplication: (NSString*)sourceApplication注释:打开Pinterest应用程序? (2认同)

Amy*_*all 2

截至 2013 年 5 月 20 日,Pinterest 发布了用于固定内容的 iOS SDK。

查看他们的开发者网站以获取更多信息。