在uiwebview中的app内打开facebook发布/ pageurl与facebook会话

时间:2014-02-21 11:09:31

标签: ios uiwebview facebook-ios-sdk

我正在使用最新的Facebook iOS Sdk进行Facebook整合。我能够很好地执行登录和图形api / FQL操作。但是现在我想在登录后在UIWebView内的应用内打开Facebook发布/页面浏览器。那么,有谁知道怎么做?

1 个答案:

答案 0 :(得分:2)

要做到这一点,你必须强制Facebook webView登录

// Initialize a session object
FBSession *session = [[FBSession alloc] init];
// Set the active session
[FBSession setActiveSession:session];
// Open the session
[session openWithBehavior: FBSessionLoginBehaviorForcingWebView
        completionHandler:^(FBSession *session,
                            FBSessionState status,
                            NSError *error) {
            // Respond to session state changes, 
            // ex: updating the view
        }];

基于webView的登录将为您的应用添加一些登录cookie(cookie管理器)

下一个: 当您在webView中打开帖子时加载cookie:

// req is NSURLRequest that you pass to your UIWebView
// this code should be before loading the UIWebView
NSArray *cooks = [[NSHTTPCookieStorage sharedHTTPCookieStorage]cookiesForURL:req.URL];
[self addCookies:cooks forRequest:req];

以下是addCookies的实现:forRequest:

- (void)addCookies:(NSArray *)cookies forRequest:(NSMutableURLRequest *)request
{
    if ([cookies count] > 0)
    {
        NSHTTPCookie *cookie;
        NSString *cookieHeader = nil;
        for (cookie in cookies)
        {
            if (!cookieHeader)
            {
                cookieHeader = [NSString stringWithFormat: @"%@=%@",[cookie name],[cookie value]];
            }
            else
            {
                cookieHeader = [NSString stringWithFormat: @"%@; %@=%@",cookieHeader,[cookie name],[cookie value]];
            }
        }
        if (cookieHeader)
        {
            [request setValue:cookieHeader forHTTPHeaderField:@"Cookie"];
        }
    }
}