试试这个代码:
我认为这肯定会对您有所帮助:
Fb like Widget 可以嵌入到我们的应用程序中。您只需添加一个 webView 并在此处获取 Fb Like Widget html 代码/URL。
在 ViewController.h 中要添加 fb like 按钮:
#import <UIKit/UIKit.h>
@interface TestViewController : UIViewController <UIWebViewDelegate>
@property (strong, nonatomic) UIWebView * fbLikeWebView;
-(void)embedFBLikeButton;
@end
在 TestViewController.m
#import "AboutUsViewController.h"
@implementation AboutUsViewController
@synthesize fbLikeWebView = _fbLikeWebView;
- (void)viewDidLoad
{
[super viewDidLoad];
//Add this code for FbLike Webview
self.fbLikeWebView = [[UIWebView alloc] initWithFrame: CGRectMake(100.0, 50.0, 55.0, 70.0)];
_fbLikeWebView.opaque = NO;
_fbLikeWebView.backgroundColor = [UIColor clearColor];
_fbLikeWebView.delegate = self;
[self.view addSubview:_fbLikeWebView];
for (UIScrollView *subview in _fbLikeWebView.subviews)
{
if ([subview isKindOfClass:[UIScrollView class]]) {
subview.scrollEnabled = NO;
subview.bounces = NO;
}
}
}
然后在 ViewWillAppear 方法中调用 enbeddFBLikeButton 方法在 web 视图上添加 fbLike 按钮 wigdet:
-(void)viewWillAppear:(BOOL)animated
{
[self embedFBLikeButton];
[_fbLikeWebView reload];
}
-(void)embedFBLikeButton
{
NSString *facebookUrl = //here paste the url you get from fb developer link above;
[self.fbLikeWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:facebookUrl]]];
}
您现在符合 UIWebViewDelegate ,现在轮到在这里定义 edelegate 方法:
#pragma mark - WebView Delgate Methods
- (BOOL)webView:(UIWebView *)webview shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if ([request.URL.lastPathComponent isEqualToString:@"login.php"])
{
[self login];
return NO;
}
return YES;
}
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
[_fbLikeWebView stopLoading];
}
此方法用于将用户登录到 facebook 帐户:
- (void)login
{
[FBSession setActiveSession: [[FBSession alloc] initWithPermissions:@[@"publish_actions", @"publish_stream", @"user_photos"]]];
[[FBSession activeSession] openWithBehavior: FBSessionLoginBehaviorForcingWebView completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
switch (status) {
case FBSessionStateOpen:
// call the legacy session delegate
//Now the session is open do corresponding UI changes
if (session.isOpen) {
FBRequest *me = [FBRequest requestForMe];
[me startWithCompletionHandler: ^(FBRequestConnection *connection,
NSDictionary<FBGraphUser> *my,
NSError *error) {
if (!my) {
NSLog(@"Facebook error:\n%@", error.description);
[[[UIAlertView alloc] initWithTitle: @"Error"
message: @"Facebook Login error."
delegate: self
cancelButtonTitle: @"Ok"
otherButtonTitles: nil, nil] show];
return;
}
}];
[_fbLikeWebView reload];
[[[UIAlertView alloc] initWithTitle: @""
message: @"Successfully Login. Please click on like button"
delegate: self
cancelButtonTitle: @"Ok"
otherButtonTitles: nil, nil] show];
}
break;
case FBSessionStateClosedLoginFailed:
{
[_fbLikeWebView reload];
}
break;
default:
break; // so we do nothing in response to those state transitions
}
}];
}
享受编码!