2012-11-03 40 views
1

我正在尝试构建简单的原型,其中我发布了一些文本到我的Facebook帐户。我已阅读了ios 6 facebook集成文档,并提出了以下代码。一切似乎工作正常,直到我打到最后一块,我在方法postTextToFacebook中创建SLRequest对象,并尝试执行与处理程序块执行performRequestWithHandler。控制块不会在处理程序块内执行。我假设performRequestWithHandler调用在这种情况下不成功。任何人都成功完成了?这里是供您参考的代码。SLRequest performRequestWithHandler在ios 6中不起作用

#import <Social/Social.h> 
#import "ViewController.h" 

@implementation ViewController 
@synthesize facebookAccount; 
@synthesize accountStore; 
@synthesize textToPost; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 


} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 

} 


-(IBAction) postToFacebook:(id)sender 
{ 
    self.statusLabel.text = @"Logging in ..."; 
    if(self.accountStore == nil) 
    { 
     self.accountStore = [[ACAccountStore alloc] init]; 
    } 

    ACAccountType *facebookAccountType = [self.accountStore enter code hereaccountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook]; 

    NSMutableDictionary *myOptions = [NSMutableDictionary dictionaryWithObjectsAndKeys: 
            @"172197712918906", ACFacebookAppIdKey, 
            [NSArray arrayWithObjects:@"email", @"user_about_me", @"user_likes", nil], ACFacebookPermissionsKey, ACFacebookAudienceFriends, ACFacebookAudienceKey, nil]; 
    [self.accountStore requestAccessToAccountsWithType:facebookAccountType options:myOptions completion:^(BOOL granted, NSError *error){ 
     __block NSString *statusText; 
     if(granted) 
     { 
      NSArray *accounts = [self.accountStore accountsWithAccountType:facebookAccountType]; 
      self.facebookAccount = [accounts lastObject]; 
      [myOptions setObject:[NSArray arrayWithObjects:@"publish_stream", nil] forKey:ACFacebookPermissionsKey]; 
      [self.accountStore requestAccessToAccountsWithType:facebookAccountType options:myOptions completion:^(BOOL granted, NSError *error) { 
       __block NSString *statusText1; 
       if(granted && error == nil) 
       { 
        NSArray *accounts = [self.accountStore accountsWithAccountType:facebookAccountType]; 
        self.facebookAccount = [accounts lastObject]; 
        [self postTextToFacebook]; 
        statusText1 = @"Text Posted."; 
       } 
       else{ 
        statusText1 = @"Publish Failed."; 
       } 

       dispatch_async(dispatch_get_main_queue(), ^{ 
        self.statusLabel.text = statusText1; 
       }); 
      }]; 
     } 
     else{ 
      statusText = @"Login Failed."; 
      NSLog(@"Error = %@",error); 
     } 


    }]; 

} 

-(void) postTextToFacebook 
{ 
    NSDictionary *parameters = @{@"message":self.textToPost.text}; 
    NSURL *feedURL = [NSURL URLWithString:@"https://graphs.facebook.com/me/feed"]; 

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

    request.account = self.facebookAccount; 

    [request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {   
     NSLog(@"Facebook request received, status code %d", urlResponse.statusCode); 
     NSString *response = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; 
     NSLog(@"Response data: %@", response); 

     //handle errors 
     if(error == nil) 
     { 
      dispatch_async(dispatch_get_main_queue(), ^{ 
       self.statusLabel.text = @"text posted to facebook"; 
      }); 
     } 

    }]; 
} 

@end 
+0

我遇到同样的问题。你有没有找到解决方案? – user717452

回答

相关问题