【发布时间】:2015-08-25 07:57:15
【问题描述】:
我正在尝试在 iOS 应用程序中嵌入 Facebook cmets 小部件,该应用程序已获得 Facebook 授权。
我可以使用用户的访问令牌在 UIWebView 中设置适当的 cookie 吗?
- (void)loadComments {
[_webView loadHTMLString:(@"<!DOCTYPE html>\n"
"<html>\n"
"<head>\n"
"\t<title></title>\n"
"</head>\n"
"<body>\n"
"<div id=\"fb-root\"></div>\n"
"<script>(function(d, s, id) {\n"
" var js, fjs = d.getElementsByTagName(s)[0];\n"
" if (d.getElementById(id)) return;\n"
" js = d.createElement(s); js.id = id;\n"
" js.src = \"http://connect.facebook.net/en-US/sdk.js#xfbml=1&version=v2.4&appId=appId\";\n"
" fjs.parentNode.insertBefore(js, fjs);\n"
"}(document, 'script', 'facebook-jssdk'));</script>\n"
"<div class=\"fb-comments\" data-href=\"myUrl\" data-width=\"375\" data-numposts=\"20\"></div>\n"
"</body>\n"
"</html>") baseURL:[[NSURL alloc] initWithString:(@"https://myUrl")]];
}
#pragma mark - UIWebViewDelegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
//Maybe there is a better way to check auth redirect?
if ([[request.URL absoluteString] containsString:(@"login")]) {
[_webView loadHTMLString:(@"") baseURL:nil];
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
[login logInWithReadPermissions: @[(@"public_profile")]
handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
if (error) {
} else if (result.isCancelled) {
} else {
//Here I need to set cookies, or something like that
[self loadComments];
}
}];
return NO;
}
else {
return YES;
}
}
我看到了这个问题,但建议的答案无法通过本机/facebook 应用验证:FBConnect login, share with a webview?
我知道,我可以使用 GraphAPI,但我现在不想实现整个 cmets 逻辑和 UI。
【问题讨论】:
标签: ios facebook-graph-api uiwebview facebook-ios-sdk facebook-social-plugins