1

我浏览了许多链接和教程,但对我不起作用。谁能解释我如何使用 SLRequest 通过 iOS 更改为通过 MyAppName ?逐步或给我一些链接,逐步提供此解决方案。

编辑:我试过了。下面是我的代码可能会有所帮助。

    ACAccountStore *accountStore = [[ACAccountStore alloc] init];

    ACAccountType *facebookAccountType = [accountStore
                                          accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];

        // Specify App ID and permissions
    NSDictionary *options = @{
                              ACFacebookAppIdKey: @"012345678912345",
                              ACFacebookPermissionsKey: @[@"publish_stream", @"publish_actions"],
                              ACFacebookAudienceKey: ACFacebookAudienceFriends
                              };

    [accountStore requestAccessToAccountsWithType:facebookAccountType
                                          options:options completion:^(BOOL granted, NSError *e) {
                                              if (granted) {
                                                  NSArray *accounts = [accountStore
                                                                       accountsWithAccountType:facebookAccountType];
                                                  facebookAccount = [accounts lastObject];
                                              }
                                              else
                                                  {
                                                      // Handle Failure
                                                  }
                                          }];

    NSDictionary *parameters = @{@"message": @"My first iOS 6 Facebook posting "};

    NSURL *feedURL = [NSURL URLWithString:@"https://graph.facebook.com/me/feed"];

    SLRequest *feedRequest = [SLRequest 
                              requestForServiceType:SLServiceTypeFacebook
                              requestMethod:SLRequestMethodPOST 
                              URL:feedURL 
                              parameters:parameters];

    feedRequest.account = self->facebookAccount;

    [feedRequest performRequestWithHandler:^(NSData *responseData, 
                                             NSHTTPURLResponse *urlResponse, NSError *error)
     {
         // Handle response
     }];
}
4

1 回答 1

1

首先使用Facebook 开发者帐户设置设置您的 facebook 设置

然后从设备中重置您的模拟或删除应用程序。

添加并导入此框架

#import <Accounts/Accounts.h>
#import <Social/Social.h>
#import <AdSupport/AdSupport.h>
#import <FacebookSDK/FacebookSDK.h> 

使用此代码进行进一步处理

在你的class.h文件中

@property (nonatomic, strong)ACAccountStore *accountStore;

在你的class.m 文件中

@synthesize accountStore;

- (IBAction)facebookButtonTouch:(id)sender {

    if (self.accountStore == nil) 
        self.accountStore = [[ACAccountStore alloc] init];


    __block ACAccount *facebookAccount = nil;

      ACAccountType *facebookAccountType = [self.accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];

      NSArray * permissions = @[@"publish_stream", @"publish_actions",@"email"];
      NSMutableDictionary *options = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@"your app id", ACFacebookAppIdKey, permissions, ACFacebookPermissionsKey, ACFacebookAudienceOnlyMe, ACFacebookAudienceKey, nil];



   [self.accountStore requestAccessToAccountsWithType:facebookAccountType
                                          options:options completion:^(BOOL granted, NSError *error)
     {
         if (granted)
         {
             NSArray *readPermissions = @[@"read_stream", @"read_friendlists"];
             [options setObject:readPermissions forKey: ACFacebookPermissionsKey];

             [self.accountStore requestAccessToAccountsWithType:facebookAccountType options:options completion:^(BOOL granted, NSError *error) {
                 if(granted && error == nil) {
                     /**
                      * We now should have some read permission
                      * Now we may ask for write permissions or
                      * do something else.
                      **/
                 } else {
                     NSLog(@"error is: %@",[error description]);
                 }
             }];

             NSArray *accounts = [self.accountStore accountsWithAccountType:facebookAccountType];
             facebookAccount = [accounts lastObject];
         }
         else {

             NSLog(@"error.localizedDescription======= %@", error.localizedDescription);
         }

     }];

    NSArray *accounts = [self.accountStore accountsWithAccountType:facebookAccountType];
    facebookAccount = [accounts lastObject];



//-------------- start code for posting message on wall via SLRequest------------------
    NSDictionary *parameters = @{@"message": @"Hi Friends i m ....."};

    NSURL *feedURL = [NSURL URLWithString:@"https://graph.facebook.com/me/feed"];

    SLRequest *feedRequest = [SLRequest
                              requestForServiceType:SLServiceTypeFacebook
                              requestMethod:SLRequestMethodPOST
                              URL:feedURL
                              parameters:parameters];

    feedRequest.account = facebookAccount;

    [feedRequest performRequestWithHandler:^(NSData *responseData,
                                             NSHTTPURLResponse *urlResponse, NSError *error)
     {
         NSLog(@"responseData %@",[[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]);
     }];

//-------------- end code for posting message on wall via SLRequest------------------

}

我希望它会帮助你。谢谢

于 2013-05-24T07:22:10.090 回答