2

我已经在我的应用程序中实现了 facebook。它在 ios 6 上运行,但是当它在 ios5 上运行时,它给出错误“操作无法完成(com.facebook.sdk 错误 2)”

以下是我的代码

 - (void)postStatusUpdateClick:(NSString *)url:(UIViewController*)view:(UIView*)view1
{

NSString *shareString=[NSString stringWithFormat:@"Check out this great deal at %@",url];

NSURL *urlToShare = [NSURL URLWithString:url];

FBAppCall *appCall = [FBDialogs presentShareDialogWithLink:urlToShare
                                                      name:@"Hello Facebook"
                                                   caption:nil
                                               description:shareString
                                                   picture:nil
                                               clientState:nil
                                                   handler:^(FBAppCall *call, NSDictionary *results, NSError *error) {
                                                       if (error) {
                                                           NSLog(@"Error: %@", error.description);
                                                       } else {
                                                           NSLog(@"Success!");
                                                       }
                                                   }];

if (!appCall) {
    // Next try to post using Facebook's iOS6 integration
    BOOL displayedNativeDialog = [FBDialogs presentOSIntegratedShareDialogModallyFrom:view
                                                                          initialText:shareString
                                                                                image:nil
                                                                                  url:urlToShare
                                                                              handler:^(FBOSIntegratedShareDialogResult result, NSError *error) {

                                                                                  if (error && [error code] == 7) {
                                                                                      return;
                                                                                  }                                                                                      NSString *alertText = @"";
                                                                                  if (error) {
                                                                                      alertText = [NSString stringWithFormat:
                                                                                                   @"error: domain = %@, code = %d",
                                                                                                   error.domain, error.code];
                                                                                  } else if (result == FBNativeDialogResultSucceeded) {
                                                                                      alertText = @"Posted successfully.";
                                                                                  }
                                                                                  if (![alertText isEqualToString:@""]) {
                                                                                      // Show the result in an alert
                                                                                      [[[UIAlertView alloc] initWithTitle:@"Result"
                                                                                                                  message:alertText
                                                                                                                 delegate:self
                                                                                                        cancelButtonTitle:@"OK!"
                                                                                                        otherButtonTitles:nil]
                                                                                       show];
                                                                                  }
                                                                       }];
     if (!displayedNativeDialog) {
        // Lastly, fall back on a request for permissions and a direct post using the Graph API
        [self performPublishAction:^{


            [FBRequestConnection startForPostStatusUpdate:shareString
                                        completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {

                                            [self showAlert:@"jhj" result:result error:error];

                                        }];


        }];
             }
         }
   }

- (void)showAlert:(NSString *)message
       result:(id)result
        error:(NSError *)error {

NSString *alertMsg;
NSString *alertTitle;
if (error) {
    alertTitle = @"Error";
    if (error.fberrorShouldNotifyUser ||
        error.fberrorCategory == FBErrorCategoryPermissions ||
        error.fberrorCategory == FBErrorCategoryAuthenticationReopenSession) {
        alertMsg = error.fberrorUserMessage;
    } else {
        alertMsg = @"Operation failed due to a connection problem, retry later.";
    }
} else {
    NSDictionary *resultDict = (NSDictionary *)result;
    alertMsg = [NSString stringWithFormat:@"Successfully posted '%@'.", message];
    NSString *postId = [resultDict valueForKey:@"id"];
    if (!postId) {
        postId = [resultDict valueForKey:@"postId"];
    }
    if (postId) {
        alertMsg = [NSString stringWithFormat:@"%@\nPost ID: %@", alertMsg, postId];
    }
    alertTitle = @"Success";
}

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:alertTitle
                                                    message:alertMsg
                                                   delegate:nil
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
[alertView show];

}

 - (void) performPublishAction:(void (^)(void)) action {


if ([[FBSession activeSession]isOpen]) {
    /*
     * if the current session has no publish permission we need to reauthorize
     */
    if ([[[FBSession activeSession]permissions]indexOfObject:@"publish_actions"] == NSNotFound) {

        [[FBSession activeSession] requestNewPublishPermissions:[NSArray arrayWithObject:@"publish_action"] defaultAudience:FBSessionDefaultAudienceFriends
                                              completionHandler:^(FBSession *session,NSError *error){
                                                  if (!error) {
                                                      action();
                                                  }
                                              }];  
    }else{
        //[self publishStory];
        action();
    }
}else{
    /*
     * open a new session with publish permission


     */
    [FBSession openActiveSessionWithPublishPermissions:[NSArray arrayWithObject:@"publish_actions"]
                                       defaultAudience:FBSessionDefaultAudienceFriends
                                          allowLoginUI:YES
                                     completionHandler:^(FBSession *session,
                                                         FBSessionState status,
                                                         NSError *error) {
                                         // if login fails for any reason, we alert
                                         if (error) {
                                             UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                                                             message:error.localizedDescription
                                                                                            delegate:nil
                                                                                   cancelButtonTitle:@"OK"
                                                                                   otherButtonTitles:nil];
                                             [alert show];

}

}

当我调试代码时,它显示 FBSessionStateClosedLoginFailed

4

0 回答 0