关于C#:FaceBook Connect。

FaceBook Connect. Login on tap

我有一个非常简单的问题。

在FB教程https://developers.facebook.com/docs/mobile/ios/build/中,它开始登录到"didFinishLaunchingWithOptions"中——就在应用程序启动之后。

我需要在tap上登录,然后等待回调并在fb wall上发送消息。我认为Hackbook应用程序示例设计应用程序对于这个目的来说太复杂了。

实现这一目标最简单的方法是什么?

upd:我已经使用了hackbook示例,但是viewcontrollers仍然没有得到回调:(

YAppDealth.H:

1
2
3
4
5
6
7
8
9
10
11
12
13
    #import
    #import"FBConnect.h"

    @interface yAppDelegate : UIResponder
    {
        Facebook *facebook;

    }

    @property (strong, nonatomic) UIWindow *window;
    @property (nonatomic, retain) Facebook *facebook;

    @end

YppReavig.M:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
    #import"yAppDelegate.h"
    #import"yViewController.h"

    static NSString* kAppId = @"350435425024264";

    @implementation yAppDelegate

    @synthesize window = _window;
    @synthesize facebook;

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {    
        yViewController *viewController = [[yViewController alloc] init];

        facebook = [[Facebook alloc] initWithAppId:kAppId andDelegate:viewController];

        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        if ([defaults objectForKey:@"FBAccessTokenKey"] && [defaults objectForKey:@"FBExpirationDateKey"]) {
            facebook.accessToken = [defaults objectForKey:@"FBAccessTokenKey"];
            facebook.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"];
        }

        return YES;
    }

    - (void)applicationDidBecomeActive:(UIApplication *)application {
        [[self facebook] extendAccessTokenIfNeeded];
    }

    - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
        return [self.facebook handleOpenURL:url];
    }

    - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
        return [self.facebook handleOpenURL:url];
    }

yview控制器.h:

1
2
3
4
5
6
7
8
9
10
11
    #import
    #import"FBConnect.h"

    @interface yViewController : UIViewController
    {
        NSArray *permissions;
    }

    @property (nonatomic, retain) NSArray *permissions;

    @end

Y视图控制器.m

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
    #import"yViewController.h"
    #import"yAppDelegate.h"
    #import"FBConnect.h"

    @interface yViewController ()
    @end

    @implementation yViewController
    @synthesize permissions;

    - (IBAction)buttonPressed:(UIButton *)sender {

        NSLog(@"Button pressed!");

        permissions = [[NSArray alloc] initWithObjects:@"offline_access", nil];

        yAppDelegate *delegate = (yAppDelegate *)[UIApplication sharedApplication].delegate;
        if (![[delegate facebook] isSessionValid]) {
            [[delegate facebook] authorize:permissions];
        } else {
            //[self showLoggedIn];
        }
        NSLog(@"login!!");

    }


    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
    }

    - (void)viewDidUnload
    {
        [super viewDidUnload];
        // Release any retained subviews of the main view.
    }

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
    }

    #pragma mark - FBSessionDelegate Methods
    /**
     * Called when the user has logged in successfully.
     */

    - (void)fbDidLogin {
        NSLog(@"did login");
    }

    -(void)fbDidExtendToken:(NSString *)accessToken expiresAt:(NSDate *)expiresAt {
        NSLog(@"token extended");
    }

    /**
     * Called when the user canceled the authorization dialog.
     */

    -(void)fbDidNotLogin:(BOOL)cancelled {
        NSLog(@"fbDidNotLogin");
    }

    /**
     * Called when the request logout has succeeded.
     */

    - (void)fbDidLogout {

        NSLog(@"fbDidLogout");

        // Remove saved authorization information if it exists and it is
        // ok to clear it (logout, session invalid, app unauthorized)
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        [defaults removeObjectForKey:@"FBAccessTokenKey"];
        [defaults removeObjectForKey:@"FBExpirationDateKey"];
        [defaults synchronize];

    }

    /**
     * Called when the session has expired.
     */

    - (void)fbSessionInvalidated {

        NSLog(@"fbSessionInvalidated");

        [self fbDidLogout];
    }

    #pragma mark - FBRequestDelegate Methods
    /**
     * Called when the Facebook API request has returned a response.
     *
     * This callback gives you access to the raw response. It's called before
     * (void)request:(FBRequest *)request didLoad:(id)result,
     * which is passed the parsed response object.
     */

    - (void)request:(FBRequest *)request didReceiveResponse:(NSURLResponse *)response {
        //NSLog(@"received response");
    }

    /**
     * Called when a request returns and its response has been parsed into
     * an object.
     *
     * The resulting object may be a dictionary, an array or a string, depending
     * on the format of the API response. If you need access to the raw response,
     * use:
     *
     * (void)request:(FBRequest *)request
     *      didReceiveResponse:(NSURLResponse *)response
     */

    - (void)request:(FBRequest *)request didLoad:(id)result {
        NSLog(@"-(void)request");
    }

    /**
     * Called when an error prevents the Facebook API request from completing
     * successfully.
     */

    - (void)request:(FBRequest *)request didFailWithError:(NSError *)error {
        NSLog(@"Err message: %@", [[error userInfo] objectForKey:@"error_msg"]);
        NSLog(@"Err code: %d", [error code]);
    }

@end

P.S.对不起我的英语:)


按照FB教程所示的方法执行,只需将登录代码移动到按钮事件或任何您想触发它的事件中。