当前位置: 首页 > 知识库问答 >
问题:

脸谱网连接。点击登录

葛子昂
2023-03-14

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

在FB教程中https://developers.facebook.com/docs/mobile/ios/build/它开始登录didFinishLaunchingSusOptions-在应用程序启动后。

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

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

UPD:我遵循了Hackbook的示例,但ViewControllers仍然没有得到回调:(

yAppServer. h:


    #import 
    #import "FBConnect.h"
    
    @interface yAppDelegate : UIResponder 
    {
        Facebook *facebook;
        
    }
    
    @property (strong, nonatomic) UIWindow *window;
    @property (nonatomic, retain) Facebook *facebook;
    
    @end

yAppDelegate。m:


    #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];
    }

yViewController。h:


    #import 
    #import "FBConnect.h"
    
    @interface yViewController : UIViewController 
    {
        NSArray *permissions;
    }
    
    @property (nonatomic, retain) NSArray *permissions;
    
    @end

yViewController。m级


    #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

共有1个答案

卞成荫
2023-03-14

以与FB教程显示的完全相同的方式进行操作,只需将登录代码移动到按钮事件或您想要触发它的任何内容中。

 类似资料:
  • 我正在使用facebook PHP api练习 当我尝试在这里创建登录功能时,出现了这个警告 语法错误,/home/u528851895/public_html/Desktap/facebook-php-sdk-v4-4.0-dev/src/facebook/FacebookResponse中出现意外的T_OBJECT_OPERATOR。php第137行 但当我使用mamplocalhost时,这

  • 我一直试图测试脸书PHP登录系统,但不断得到错误: 解析错误:语法错误,/xxxxx/httpd.www/Facebook/FacebookRedirectLoginHelper.php 第 146 行中的意外T_OBJECT_OPERATOR 我已经阅读了几个不同的例子,并在第一次遇到这些例子后尝试实施每个例子,以及阅读了这里的不同问题/答案,但都没有效果。 如果有人碰到这个问题并找到了解决办法

  • 我在Xcode的日志中有这样一条消息: 我的应用程序不会打开Facebook应用程序,而是Safari。 然后我在回调中有一个错误。 (iOS8上一切都好) 以下是所有日志:

  • 出现错误: ElementClickInterceptedException:Message:element click Intercepted:element...在点(1273,80)不可单击。其他元素将收到click:...(Session info:chrome=79.0.3945.130) 还尝试了,我使用扩展名为Google Chrome生成,得到了与上面提到的相同的错误... 至于我

  • 问题内容: 我正在FireFox中使用Selenium的IDE进行一些测试,我希望Selenium单击第二个链接(Text2)。知道我该怎么做吗?不幸的是,我无权访问HTML并且无法对其进行修改。记录功能似乎没有注册点击。 该代码附在下面。提前致谢! 问题答案: 硒也支持定位器。如果您知道确切的链接文本,则可以使用此定位器,但不能使用其他定位器。因此,对于上述示例:或。(有关其他定位器,请参见此网