Facebook在iPhone应用程序 [英] Facebook in iPhone app

查看:132
本文介绍了Facebook在iPhone应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个iPhone应用程序,在这里我有一个共享我的帖子与面书的选项(我只是传递Facebook用户的用户ID,我的服务器端开发人员做贴子)
我可以使用用户ID登录面书
但是问题是,用户从脸书进行身份验证后,贴上你的帖子不会出现在应用程序
(我正在使用ios6 SDK )
(我已经在Facebook中创建了我的应用程序,检查了图表中的public_action)
我已经浏览了Facebook开发者页面中提供的教程(美味),但无法使它



这是我的代码 - 如果有人发现错误或缺失的部分,请帮我清除



AppDelegate。 h

  #import< FacebookSDK / FacebookSDK.h> 

@class ViewController;

@interface AppDelegate:UIResponder

@property(strong,nonatomic)FBSession * session;
@property(强,非原子)UIWindow *窗口;
@property(strong,nonatomic)ViewController * viewController;
@property(强,非原子)IBOutlet UINavigationController * navBar;
- (BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI;
@end

AppDelegate.m

  #importAppDelegate.h
#importViewController.h
#define APP_ID @fd725FDE45a44198a5b8ad3f7a0ffa09


@implementation AppDelegate
@synthesize session = _session;



- (BOOL)应用程序:(UIApplication *)应用程序
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
注释:(id)注释{


return [self.session handleOpenURL:url];






- (BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI {

NSArray * permission = [[NSArray alloc] initWithObjects:
@user_location,
@user_birthday,
@user_likes,
@email,
@ publish_action,
nil];
return [FBSession openActiveSessionWithPermissions:permissions
allowLoginUI:allowLoginUI
completionHandler:^(FBSession * session,
FBSessionState state,
NSError * error){
[ self sessionStateChanged:session
state:state error:error];
}];




- (void)sessionStateChanged:(FBSession *)session
状态:(FBSessionState)状态
错误:(NSError *)error
{

switch(state){
case FBSessionStateOpen:{
FBCacheDescriptor * cacheDescriptor = [FBFriendPickerViewController cacheDescriptor];
[cacheDescriptor prefetchAndCacheForSession:session];
}
break;
case FBSessionStateClosed:{
[FBSession.activeSession closeAndClearTokenInformation];

[self performSelector:@selector(showLoginView)
withObject:nil
afterDelay:0.5f];
}
break;
case FBSessionStateClosedLoginFailed:{
[self performSelector:@selector(showLoginView)
withObject:nil
afterDelay:0.5f];
}
break;
默认值:
break;
}


}



@end

InviteFriendViewController.h

  @界面InviteFriendViewController:
- (IBAction)fbfrnds:(id)sender;
- (void)updateView;
@end

InviteFriendViewController.m

   - (void)viewDidLoad 
{
[super viewDidLoad];
[self.navigationController setNavigationBarHidden:YES];
NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];

userid = [defaults objectForKey:@userid];

AppDelegate * appDelegate = [[UIApplication sharedApplication] delegate];

if(!appDelegate.session.isOpen){
appDelegate.session = [[FBSession alloc] init];


if(appDelegate.session.state == FBSessionStateCreatedTokenLoaded){
[appDelegate.session openWithCompletionHandler:^(FBSession * session,
FBSessionState status,
NSError * error){
[self updateView];
}
];
}
}
}




- (void)facebookReteriveInformation:(NSDictionary *)dictnry {



FriendlistViewController * friends = [[FriendlistViewController alloc] initWithNibName:@FriendlistViewControllerbundle:nil];

friends.token = string;

[self.navigationController pushViewController:friends animated:YES];


}



- (IBAction)fbfrnds:(id)sender {



sharedClass = [SharedClass sharedInstance];

appDelegate =(AppDelegate *)[UIApplication sharedApplication] .delegate;


AppDelegate * appDelegate = [[UIApplication sharedApplication] delegate];

if(appDelegate.session.isOpen){

[self updateView];
} else {
if(appDelegate.session.state!= FBSessionStateCreated){
appDelegate.session = [[FBSession alloc] init];
}

[appDelegate.session openWithCompletionHandler:^(FBSession * session,
FBSessionState status,
NSError * error){
[self updateView];
}];
}

NSLog(@string issss%@,string);


}


- (void)updateView {
AppDelegate * appDelegate = [[UIApplication sharedApplication] delegate];
if(appDelegate.session.isOpen){

string = [NSString stringWithFormat:@%@,
appDelegate.session.accessToken];

NSLog(@string issss%@,string);
NSString * urlstrng;
if(flag == 1){
urlstrng = [NSString stringWithFormat:@https://graph.facebook.com/me?access_token=%@,
string];
[self dataFetching:urlstrng];
}


} else {

string = [NSString stringWithFormat:@%@,
appDelegate.session.accessToken] ;
NSString * urlstrng;
if(flag == 1){
urlstrng = [NSString stringWithFormat:@https://graph.facebook.com/me?access_token=%@,
string];
[self dataFetching:urlstrng];
}
}


- (void)dataFetching:(NSString *)strng1 {

NSURL * url = [NSURL URLWithString :strng1];
ProfileConnector * obj = [[ProfileConnector alloc] init];
obj.delegate1 = self;
[obj parsingJson:url];

}


- (BOOL)shouldAutorotateToInterfaceOrientation :( UIInterfaceOrientation)interfaceOrientation {
return(interfaceOrientation == UIInterfaceOrientationPortrait);





- (void)performPublishAction :( void(^)(void))action {
if([FBSession .activeSession.permissions indexOfObject:@publish_actions] == NSNotFound){
[FBSession.activeSession reauthorizeWithPublishPermissions:[NSArray arrayWithObject:@publish_actions]
defaultAudience:FBSessionDefaultAudienceFriends
completionHandler:^( FBSession * session,NSError * error){
if(!error){
action();
}
}];
} else {
action();
}

}



}

@end


解决方案

由于您使用3.1版本的SDK SDK, openActiveSessionWithPermissions: allowLoginUI:completionHandler:不应该被使用,旧的方式似乎不再起作用了;从 3.1迁移文档; p>


...您将需要删除openActiveSessionWithPermissions的用法:allowLoginUI:completionHandler:并将其替换为openActiveSessionWithReadPermissions:allowLoginUI:completionHandler :(甚至更多稍后,当您的应用需要发布回Facebook时,您应该使用reauthorizeWithPublishPermissions:defaultAudience:completionHandler:寻求附加权限。

>

更多详细信息可在迁移文档


I am working on an iPhone app,in this i have an option for sharing my posts with face book(i am just passing the user id of facebook user and my server side developer do the posting part) I am able to take the user id while log in in face book But problem is-after the user authenticate from facebook,"post on your behalf" is not coming in the app (i am using ios6 SDK) (i have created my app in facebook with checked that public_action from graph) I have gone through the tutorials(scrumptious) given in facebook developer page,but could not make it

Here are my code-If any one find the error or missing part,please help me to clear that

AppDelegate.h

#import <FacebookSDK/FacebookSDK.h>

@class ViewController;

@interface AppDelegate : UIResponder

@property (strong, nonatomic) FBSession *session;
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) ViewController *viewController;
@property (strong,nonatomic) IBOutlet UINavigationController *navBar;
- (BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI;
@end

AppDelegate.m

#import "AppDelegate.h"
#import "ViewController.h"
#define APP_ID @"fd725FDE45a44198a5b8ad3f7a0ffa09"


@implementation AppDelegate
@synthesize session = _session;



- (BOOL)application:(UIApplication *)application
        openURL:(NSURL *)url
 sourceApplication:(NSString *)sourceApplication
     annotation:(id)annotation {


     return [self.session handleOpenURL:url];


 }



 - (BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI {

   NSArray *permissions = [[NSArray alloc] initWithObjects:
                        @"user_location",
                        @"user_birthday",
                        @"user_likes",
                        @"email",
                        @"publish_action",
                        nil];
   return [FBSession openActiveSessionWithPermissions:permissions
                                      allowLoginUI:allowLoginUI
                                 completionHandler:^(FBSession *session,
                                                     FBSessionState state,
                                                     NSError *error) {
                                     [self sessionStateChanged:session
                                                         state:state error:error];
                                 }];


      }

     - (void)sessionStateChanged:(FBSession *)session
                  state:(FBSessionState)state
                  error:(NSError *)error
     {

    switch (state) {
        case FBSessionStateOpen: {
             FBCacheDescriptor *cacheDescriptor = [FBFriendPickerViewController    cacheDescriptor];
        [cacheDescriptor prefetchAndCacheForSession:session];
       }
        break;
        case FBSessionStateClosed: {
                    [FBSession.activeSession closeAndClearTokenInformation];

        [self performSelector:@selector(showLoginView)
                   withObject:nil
                   afterDelay:0.5f];
       }
        break;
        case FBSessionStateClosedLoginFailed: {
                    [self performSelector:@selector(showLoginView)
                   withObject:nil
                   afterDelay:0.5f];
        }
        break;
        default:
        break;
        }


        }



   @end

InviteFriendViewController.h

    @interface InviteFriendViewController : 
    -(IBAction)fbfrnds:(id)sender;
    - (void)updateView;
    @end

InviteFriendViewController.m

  - (void)viewDidLoad
  {
   [super viewDidLoad];
    [self.navigationController setNavigationBarHidden:YES];
     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

     userid = [defaults objectForKey:@"userid"];

      AppDelegate *appDelegate = [[UIApplication sharedApplication]delegate];

     if (!appDelegate.session.isOpen) {
      appDelegate.session = [[FBSession alloc] init];


       if (appDelegate.session.state == FBSessionStateCreatedTokenLoaded) {
        [appDelegate.session openWithCompletionHandler:^(FBSession *session,
                                                         FBSessionState status,
                                                         NSError *error) {
            [self updateView];
        }
         ];
         }
         }
         }




     -(void)facebookReteriveInformation:(NSDictionary *)dictnry{



       FriendlistViewController *friends = [[FriendlistViewController      alloc]initWithNibName:@"FriendlistViewController" bundle:nil];

           friends.token = string;

      [self.navigationController pushViewController:friends animated:YES];


       }



     -(IBAction)fbfrnds:(id)sender{



     sharedClass=[SharedClass sharedInstance];

     appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;


      AppDelegate *appDelegate = [[UIApplication sharedApplication]delegate];

     if (appDelegate.session.isOpen) {

       [self updateView];
        } else {
    if (appDelegate.session.state != FBSessionStateCreated) {
        appDelegate.session = [[FBSession alloc] init];
    }

    [appDelegate.session openWithCompletionHandler:^(FBSession *session,
                                                     FBSessionState status,
                                                     NSError *error) {
        [self updateView];
     }];
      }

    NSLog(@"string issss %@",string);


       }


    - (void)updateView {
     AppDelegate *appDelegate = [[UIApplication sharedApplication]delegate];
     if (appDelegate.session.isOpen) {

    string = [NSString stringWithFormat:@"%@",
              appDelegate.session.accessToken];

    NSLog(@"string issss %@",string);
    NSString *urlstrng;
    if(flag == 1){
        urlstrng = [NSString stringWithFormat:@"https://graph.facebook.com/me?access_token=%@",
                    string];
        [self dataFetching:urlstrng];
        }


       } else {

    string = [NSString stringWithFormat:@"%@",
              appDelegate.session.accessToken];
    NSString *urlstrng;
    if(flag == 1){
        urlstrng = [NSString stringWithFormat:@"https://graph.facebook.com/me?access_token=%@",
                    string];
        [self dataFetching:urlstrng];
       }
       }
       }

     -(void)dataFetching:(NSString*)strng1{

       NSURL *url = [NSURL URLWithString:strng1];
       ProfileConnector *obj = [[ProfileConnector alloc] init];
      obj.delegate1 = self;
       [obj parsingJson:url];

       }


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




     - (void) performPublishAction:(void (^)(void)) action {
   if ([FBSession.activeSession.permissions indexOfObject:@"publish_actions"] ==  NSNotFound) {
     [FBSession.activeSession reauthorizeWithPublishPermissions:[NSArray  arrayWithObject:@"publish_actions"]
                                                defaultAudience:FBSessionDefaultAudienceFriends
                                             completionHandler:^(FBSession *session,  NSError *error) {
     if (!error) {
                     action();
                                                 }
                                             }];
       } else {
    action();
       }

        }



        }

       @end

解决方案

Since you're using the 3.1 version of Facebook SDK, openActiveSessionWithPermissions:allowLoginUI:completionHandler: is not supposed to be used and the old way of doing it does not seem to be functional anymore; from the 3.1 migration documentation;

...you will need to remove usage of openActiveSessionWithPermissions:allowLoginUI:completionHandler: and replace it with openActiveSessionWithReadPermissions:allowLoginUI:completionHandler: (or even more simply, openActiveSessionWithAllowLoginUI).

Later, when your app needs to publish back to Facebook, you should use reauthorizeWithPublishPermissions:defaultAudience:completionHandler: to seek the additional permissions.

Even more details available at the migration docs.

这篇关于Facebook在iPhone应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆