このコードを試してください:
これはきっとあなたを助けると思います:
Fb のようなウィジェットをアプリケーションに埋め込むことができます。webView を追加して、Fb Like Widget html コード/URL hereを取得するだけです。
fbのようなボタンを追加するViewController.hで:
#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 ボタン ウィジェットを追加します。
-(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
}
}];
}
コーディングをお楽しみください!!!