I am using latest Facebook iOS Sdk
for Facebook integration. I am able to perform login and graph api/FQL operation very well. But now I want to open Facebook post/pageurl with facebook session within app in UIWebView
after login. So, anyone know how to do it?
Asked
Active
Viewed 1,470 times
0

Sunil Zalavadiya
- 1,993
- 25
- 36
-
do you mean you want the user to be logged when you open the post in a web view ? – Basheer_CAD Feb 21 '14 at 11:18
-
@Basheer_CAD After user has finished login process via Facebook iOS Sdk, I need to open Facebook url in UIWebView with logged in Facebook session. So I need not to do Facebook login again within UIWebView. – Sunil Zalavadiya Feb 21 '14 at 11:27
1 Answers
2
To do this you have to force Facebook webView login
// 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
}];
The webView based login will add some login cookies to your app (cookies manager)
Next: When you open post in webView load the cookies:
// 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];
Here is the implementation for 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"];
}
}
}

Basheer_CAD
- 4,908
- 24
- 36
-
What If user did login via Facebook app/Safari/from device settings instead of UIWebView? – Sunil Zalavadiya Feb 21 '14 at 11:54
-
Then no cookies. The problem is you can not pass the session information into UIWebView @sunilz – Basheer_CAD Feb 21 '14 at 11:56
-
Is there any way to force Facebook Login with permissions via Safari browser? – Sunil Zalavadiya Feb 22 '14 at 11:31
-
of course, you can find your answer here: https://developers.facebook.com/docs/ios/login-tutorial/ What about what I provided above in my answer, did that help you ? – Basheer_CAD Feb 22 '14 at 14:28
-
Yes, above answer will help, If I only need login. But If I need read/write permission with provided method by Facebook iOS Sdk, it will redirected to safari or Facebook app. Thats why I need to force login to Safari browser. Have you any solution? Also I have read given link by you, but I did not found way to force login via Safari. – Sunil Zalavadiya Feb 24 '14 at 06:05
-
use openWithBehaviour:FBSessionLoginBehaviorWithNoFallbackToWebView – Basheer_CAD Feb 24 '14 at 09:23
-
I want to achieve the same but FBSession is no longer available. How to to do this now? – Mohammad Sadiq Jul 13 '16 at 08:37