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