0

我正在尝试集成Pinterest到我的 iOS 应用程序中。

这是我尝试过的。

- (void)viewDidLoad
{

    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}
 - (void)viewWillAppear:(BOOL)animated
{
    [_pinterestWebView.scrollView setShowsHorizontalScrollIndicator:NO];
    [_pinterestWebView.scrollView setShowsVerticalScrollIndicator:NO];
    [super viewWillAppear:animated];
    [self postToPinterestWithImageUrl:_url andDescription:_description];

}
-(void)postToPinterestWithImageUrl:(NSString *)url andDescription:(NSString *)description
{

    NSString *buttonUrl = [NSString stringWithFormat:@"http://pinterest.com/pin/create/button/?url=http://www.flor.com&media=%@&description=%@", url, @"hello"];
    [_pinterestWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:buttonUrl] cachePolicy:NSURLCacheStorageNotAllowed timeoutInterval:40]];    
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSLog(@"WebView URL = %@",[request.URL absoluteString]);
    return YES;
}

- (void)webViewDidStartLoad:(UIWebView *)webView {
    [_activityIndicator startAnimating];
    [_activityIndicator setHidden:NO];
}

-(void)webViewDidFinishLoad:(UIWebView *)webView {


    NSRange range = [[webView.request.URL absoluteString] rangeOfString:@"login"];
    if (range.location==NSNotFound) {


        CGRect modifiedFrame = self.view.superview.frame;
        CGPoint center = self.view.superview.center;

        UIInterfaceOrientation orinetation= [[UIApplication sharedApplication] statusBarOrientation];
        if (UIInterfaceOrientationIsLandscape(orinetation)) {
            modifiedFrame.size=CGSizeMake(350, 650);
        }
        else
        {
            modifiedFrame.size=CGSizeMake(650, 350);
        }
        [self.view.superview setFrame:modifiedFrame];
        [self.view.superview setCenter:center];
        NSLog(@"Content Offset = %@",NSStringFromCGPoint(webView.scrollView.contentOffset));
        [webView.scrollView setContentOffset:CGPointMake(40, 0)];

    }
    NSLog(@"Content Size = %@",NSStringFromCGSize([webView.scrollView contentSize]));
    [_activityIndicator stopAnimating];
    [_activityIndicator setHidden:YES];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    NSLog(@"Error = %@",[error localizedDescription]);
}
- (void)viewDidUnload
{
    [self setActivityIndicator:nil];
    [self setPinterestWebView:nil];
    [super viewDidUnload];
}
- (IBAction)dismissIt:(id)sender
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

-(BOOL)shouldAutorotate
{
    return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAll;
}
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    return YES;
}

但是,一旦 webview 加载了pinterest登录页面并且如果我更改方向并尝试登录,那么我会在 webview 上显示以下消息。

在此处输入图像描述

可能是什么问题呢?我在加载Pinterest网址时犯了任何错误吗?此外,它总是提示进行身份验证。我怎样才能避免这种情况?

4

1 回答 1

0

发生的事情是您试图在没有 Django CSRF(跨站点请求伪造)令牌的情况下向 Pinterest 发出请求。您需要获取该 CSRF 令牌并将其放入postToPinterestWithImageUrl. 此外,在尝试以这种方式与 Pinterest 交互时,您可能会想要使用 POST 而不是 GET 请求。此外,您每次都必须登录的原因是因为您将其NSURLCacheStorageNotAllowed用作缓存策略,该策略可确保不会将有关 HTTP 会话的任何内容(包括会话信息)保存到磁盘。尝试将缓存策略更改为NSURLCacheStorageAllowed.

于 2013-02-19T20:11:08.950 回答