2012-11-15 34 views
0

我想用ios應用程序登錄Facebook。當我點擊我的應用中的登錄按鈕時,它崩潰。從ios應用程序簡單登錄到facebook

它崩潰在這一點:

[自我presentModalViewController:_loginDialog動畫:YES];

並返回此日誌中:

fbconnect=1&skip_api_login=1&m=m&next=https%3A%2F%2Fm.facebook.com%2Fdialog%2Fpermissions.request%3F_path%3Dpermissions.request%26app_id%3D169265183116528%26redirect_uri%3Dhttp%253A%252F%252Fwww.facebook.com%252Fconnect%252Flogin_success.html%26display%3Dtouch%26type%3Duser_agent%26perms%3Dpublish_stream%26fbconnect%3D1%26from_login%3D1%26client_id%3D169265183116528&refsrc=http%3A%2F%2Fm.facebook.com%2Flogin.php&cancel=http%3A%2F%2Fwww.facebook.com%2Fconnect%2Flogin_success.html%3Ferror_reason%3Duser_denied%26error%3Daccess_denied%26error_description%3DThe%2Buser%2Bdenied%2Byour%2Brequest.&landing_serial=1&refid=9> 

需要一些這方面的指導......在新的Facebook連接到iOS

EDITED

#import "FBFunViewController.h" 
#import "ASIHTTPRequest.h" 
#import "ASIFormDataRequest.h" 
#import "JSON.h" 
#import "faceAppDelegate.h" 

@implementation FBFunViewController 
@synthesize loginStatusLabel = _loginStatusLabel; 
@synthesize loginButton = _loginButton; 
@synthesize loginDialog = _loginDialog; 
@synthesize loginDialogView = _loginDialogView; 
@synthesize textView = _textView; 
@synthesize imageView = _imageView; 
@synthesize segControl = _segControl; 
@synthesize webView = _webView; 
@synthesize accessToken = _accessToken; 
@synthesize postmessage, act; 

#pragma mark Main 

- (void)dealloc { 
    self.loginStatusLabel = nil; 
    self.loginButton = nil; 
    self.loginDialog = nil; 
    self.loginDialogView = nil; 
    self.textView = nil; 
    self.imageView = nil; 
    self.segControl = nil; 
    self.webView = nil; 
    self.accessToken = nil; 
    self.postmessage=nil; 
    [super dealloc]; 
} 

- (void) setLoginState 
{ 
    NSString *appId = @""; // 
    NSString *permissions = @"publish_stream"; 
    _loginState = LoginStateStartup; 
    self.loginDialog = [[[FBFunLoginDialog alloc] initWithAppId:appId requestedPermissions:permissions delegate:self] autorelease]; 
    self.loginDialogView = _loginDialog.view; 
    [self showActivityIndicator]; 
} 

- (void)refresh { 

    if (_loginState == LoginStateStartup || _loginState == LoginStateLoggedOut) { 
     _loginStatusLabel.text = @"Not connected to Facebook"; 
     //[_loginButton setTitle:@"Login" forState:UIControlStateNormal]; 
     _loginButton.hidden = NO; 
     NSLog(@"_login state: <%d>",_loginState); 
     [self componentsVisibility:YES]; 

     if (_loginState == LoginStateLoggedOut) { 
      faceAppDelegate *pdel = (faceAppDelegate *)[[UIApplication sharedApplication] delegate]; 
      [pdel removeFBVC]; 
      [self.navigationController popViewControllerAnimated:YES]; 
     } 
     else { 
      [self loginButtonTapped:nil]; 
     } 


    } else if (_loginState == LoginStateLoggingIn) { 
     _loginStatusLabel.text = @"Connecting to Facebook..."; 
     _loginButton.hidden = YES; 
    } else if (_loginState == LoginStateLoggedIn) { 
     _loginStatusLabel.text = @"Connected to Facebook"; 
     // [_loginButton setTitle:@"Logout" forState:UIControlStateNormal]; 
     _loginButton.hidden = NO; 
     [self hideActivityIndicator]; 
     [self componentsVisibility:NO]; 
    } 
    _textView.text = self.postmessage; 
    _textView.font = [UIFont systemFontOfSize:13]; 
} 

- (void) componentsVisibility:(BOOL) flag 
{ 
    NSLog(@"control in components visibility setting: <%d>",flag); 
    [lblPostToWall setHidden:flag]; 
    [btnCancel setHidden:flag]; 
    [btnPublish setHidden:flag]; 
    [self.textView setHidden:flag]; 
} 

- (void)viewWillAppear:(BOOL)animated { 
    [self refresh]; 
} 

- (IBAction) btnCancelClicked: (id) sender 
{ 
    faceAppDelegate *pdel = (faceAppDelegate *)[[UIApplication sharedApplication] delegate]; 
    [pdel removeFBVC]; 
    [self.navigationController popViewControllerAnimated:YES]; 
} 

#pragma mark Login Button 

- (IBAction)loginButtonTapped:(id)sender { 

    //NSLog(@"login tapped"); 
    NSString *appId = @""; // 
    NSString *permissions = @"publish_stream"; 
    // NSString *permissions = @"email"; 

    if (_loginDialog == nil) { 
     self.loginDialog = [[[FBFunLoginDialog alloc] initWithAppId:appId requestedPermissions:permissions delegate:self] autorelease]; 
     self.loginDialogView = _loginDialog.view; 
    } 

    if (_loginState == LoginStateStartup || _loginState == LoginStateLoggedOut) { 
     [self showActivityIndicator]; 
     _loginState = LoginStateLoggingIn; 
     [_loginDialog login]; 
    } else if (_loginState == LoginStateLoggedIn) { 
     _loginState = LoginStateLoggedOut;   
     [_loginDialog logout]; 
     [self hideActivityIndicator]; 
    } 
    [self refresh]; 

} 

- (void) showActivityIndicator 
{ 
    //NSLog(@"control in show activity indicator view>>>>>>>>>>>>>>>"); 
    //act = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(150, 220, 50, 50)]; 
    if (self.act == nil) { 
     self.act = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; 
    } 

    [self.act setCenter:CGPointMake(self.view.frame.size.width/2.0, self.view.frame.size.height/2.0)]; 
    [self.view addSubview:self.act]; 
    //act.backgroundColor = [UIColor grayColor]; 
    [self.act startAnimating]; 
} 

- (void) hideActivityIndicator 
{ 
    //NSLog(@"hide activity indicator"); 
    [self.act stopAnimating]; 
    [self.act removeFromSuperview]; 
    [self.act setFrame:CGRectMake(-100, -100, 20, 20)]; 
    self.act = nil; 
} 


#pragma mark FB Requests 

- (void)showLikeButton { 

    // Source: http://developers.facebook.com/docs/reference/plugins/like-box 
    NSString *likeButtonIframe = @"<iframe src=\"http://www.facebook.com/plugins/likebox.php?id=122723294429312&amp;width=292&amp;connections=0&amp;stream=false&amp;header=false&amp;height=62\" scrolling=\"no\" frameborder=\"0\" style=\"border:none; overflow:hidden; width:282px; height:62px;\" allowTransparency=\"true\"></iframe>"; 
    NSString *likeButtonHtml = [NSString stringWithFormat:@"<HTML><BODY>%@</BODY></HTML>", likeButtonIframe]; 

    [_webView loadHTMLString:likeButtonHtml baseURL:[NSURL URLWithString:@""]]; 

} 

- (void)getFacebookProfile { 
    NSString *urlString = [NSString stringWithFormat:@"https://graph.facebook.com/me?access_token=%@", [_accessToken stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; 
    NSURL *url = [NSURL URLWithString:urlString]; 
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; 
    [request setDidFinishSelector:@selector(getFacebookProfileFinished:)]; 

    [request setDelegate:self]; 
    [request startAsynchronous]; 
} 

- (void)rateTapped:(id)sender { 
    NSLog(@"control in rate tapped with message: <%@>",self.postmessage); 

    NSURL *url = [NSURL URLWithString:@"https://graph.facebook.com/me/feed"]; 
    ASIFormDataRequest *newRequest = [ASIFormDataRequest requestWithURL:url]; 
    [newRequest setPostValue:self.postmessage forKey:@"message"]; 
    [newRequest setPostValue:@"" forKey:@"name"]; 
    [newRequest setPostValue:@"" forKey:@"caption"]; 
    [newRequest setPostValue:@"" forKey:@"description"]; 
    [newRequest setPostValue:@"" forKey:@"link"]; 
    [newRequest setPostValue:@"http://CMS/images/forlogo/" forKey:@"picture"]; 
    [newRequest setPostValue:_accessToken forKey:@"access_token"]; 
    [newRequest setDidFinishSelector:@selector(postToWallFinished:)]; 

    [newRequest setDelegate:self]; 
    [newRequest startAsynchronous]; 


    /* 
    NSString *likeString; 


    // NSString *message = [NSString stringWithFormat:@"I think this is a %@ %@!", adjectiveString, likeString]; 

    NSURL *url = [NSURL URLWithString:@"https://graph.facebook.com/me/photos"]; 
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; 
    // [request addFile:filePath forKey:@"file"]; 
    [request setPostValue:self.postmessage forKey:@"message"]; 
    [request setPostValue:_accessToken forKey:@"access_token"]; 
    // [request setDidFinishSelector:@selector(sendToPhotosFinished:)]; 
    [request setDidFinishSelector:@selector(getFacebookPhotoFinished:)]; 

    [request setDelegate:self]; 
    [request startAsynchronous];*/ 

} 

- (void)sendToPhotosFinished:(ASIHTTPRequest *)request 
{ 
    // Use when fetching text data 
    NSString *responseString = [request responseString]; 

    NSMutableDictionary *responseJSON = [responseString JSONValue]; 
    NSString *photoId = [responseJSON objectForKey:@"id"]; 
    NSLog(@"Photo id is: %@", photoId); 

    NSString *urlString = [NSString stringWithFormat:@"https://graph.facebook.com/%@?access_token=%@", photoId, [_accessToken stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; 
    NSURL *url = [NSURL URLWithString:urlString]; 
    ASIHTTPRequest *newRequest = [ASIHTTPRequest requestWithURL:url]; 
    [newRequest setDidFinishSelector:@selector(getFacebookPhotoFinished:)]; 

    [newRequest setDelegate:self]; 
    [newRequest startAsynchronous]; 

} 

#pragma mark FB Responses 

- (void)getFacebookProfileFinished:(ASIHTTPRequest *)request 
{ 
    // Use when fetching text data 
    NSString *responseString = [request responseString]; 
    NSLog(@"Got Facebook Profile: %@", responseString); 

    NSString *likesString; 
    NSMutableDictionary *responseJSON = [responseString JSONValue]; 
    NSArray *interestedIn = [responseJSON objectForKey:@"interested_in"]; 
    if (interestedIn != nil) { 
     NSString *firstInterest = [interestedIn objectAtIndex:0]; 
     if ([firstInterest compare:@"male"] == 0) { 
      [_imageView setImage:[UIImage imageNamed:@"depp.jpg"]]; 
      likesString = @"dudes"; 
     } else if ([firstInterest compare:@"female"] == 0) { 
      [_imageView setImage:[UIImage imageNamed:@"angelina.jpg"]]; 
      likesString = @"babes"; 
     }   
    } else { 
     [_imageView setImage:[UIImage imageNamed:@"maltese.jpg"]]; 
     likesString = @"puppies"; 
    } 

    NSString *username; 
    NSString *firstName = [responseJSON objectForKey:@"first_name"]; 
    NSString *lastName = [responseJSON objectForKey:@"last_name"]; 
    if (firstName && lastName) { 
     username = [NSString stringWithFormat:@"%@ %@", firstName, lastName]; 
    } else { 
     username = @"mysterious user"; 
    } 

    // _textView.text = [NSString stringWithFormat:@"Hi %@! I noticed you like %@, so tell me if you think this pic is hot or not!", 
    //     username, likesString]; 
    _textView.text = self.postmessage; 
    [self refresh];  
} 

- (void)getFacebookPhotoFinished:(ASIHTTPRequest *)request 
{ 
    // Use when fetching text data 
    NSString *responseString = [request responseString]; 
    NSLog(@"Got Facebook Photo: %@", responseString); 

    NSMutableDictionary *responseJSON = [responseString JSONValue]; 

    // NSString *link = [responseJSON objectForKey:@"link"]; 
// if (link == nil) return; 
// NSLog(@"Link to photo: %@", link); 

    /* 
    NSString *adjectiveString; 
    if (_segControl.selectedSegmentIndex == 0) { 
     adjectiveString = @"cute"; 
    } else { 
     adjectiveString = @"ugly"; 
    } 
    */ 

    // Ideally I would have liked to include this as the image for the wall post, but Facebook doesn't allow this :P 
    // See: http://forum.developers.facebook.com/viewtopic.php?id=62521&p=2 
    // Update: actually now you can! 

    NSURL *url = [NSURL URLWithString:@"https://graph.facebook.com/me/feed"]; 
    ASIFormDataRequest *newRequest = [ASIFormDataRequest requestWithURL:url]; 
    [newRequest setPostValue:self.postmessage forKey:@"message"]; 
    [newRequest setPostValue:@"" forKey:@"name"]; 
    // [newRequest setPostValue:@"via " forKey:@"caption"]; 
    //[newRequest setPostValue:[NSString stringWithFormat:@"And by the way - check out this %@ pic.", adjectiveString] forKey:@"description"]; 
    // [newRequest setPostValue:@"From Ray Wenderlich's blog - an blog about iPhone and iOS development." forKey:@"description"]; 
    // [newRequest setPostValue:@"http://www.raywenderlich.com" forKey:@"link"]; 
    // [newRequest setPostValue:link forKey:@"picture"]; 
    //[newRequest setPostValue:@"http://www.raywenderlich.com/wp-content/themes/raywenderlich/images/logo.png" forKey:@"picture"]; 
    [newRequest setPostValue:_accessToken forKey:@"access_token"]; 
    [newRequest setDidFinishSelector:@selector(postToWallFinished:)]; 

    [newRequest setDelegate:self]; 
    [newRequest startAsynchronous]; 

} 

- (void)postToWallFinished:(ASIHTTPRequest *)request 
{ 
    // Use when fetching text data 
    NSString *responseString = [request responseString]; 
    NSLog(@"response string: <%@>",responseString); 

    NSMutableDictionary *responseJSON = [responseString JSONValue]; 
    NSString *postId = [responseJSON objectForKey:@"id"]; 
    NSLog(@"Post id is: %@", postId); 
    NSString *msg; 
    NSRange aRange = [responseString rangeOfString:@"error"]; 
    NSRange bRange = [responseString rangeOfString:@"Feed action request limit reached"]; 
    if (aRange.location ==NSNotFound) { 
     msg = @"Posted to Facebook successfully"; 
    } 
    else if (bRange.location != NSNotFound){ 
     msg = @"You have reached the maximum post limit to Facebook for today. Please try again later."; 
    } 
    else { 
     msg = @"Error occurred. Please try again later"; 
    } 

    UIAlertView *av = [[[UIAlertView alloc] initWithTitle:msg 
                message:@"" 
               delegate:self 
             cancelButtonTitle:@"OK" 
             otherButtonTitles:nil] autorelease]; 
    [av show]; 

} 

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    faceAppDelegate *pdel = (faceAppDelegate *)[[UIApplication sharedApplication] delegate]; 
    [pdel removeFBVC]; 
    [self.navigationController popViewControllerAnimated:YES]; 
} 

#pragma mark FBFunLoginDialogDelegate 

- (void)accessTokenFound:(NSString *)accessToken { 
    NSLog(@"Access token found: %@", accessToken); 
    self.accessToken = accessToken; 
    _loginState = LoginStateLoggedIn; 
    [self dismissModalViewControllerAnimated:YES];  
    [self getFacebookProfile]; 
    [self showLikeButton]; 
    [self refresh]; 
} 

- (void)displayRequired { 
    [self presentModalViewController:_loginDialog animated:YES]; 
    [self hideActivityIndicator]; 
} 

- (void)closeTapped { 
    [self dismissModalViewControllerAnimated:YES]; 
    _loginState = LoginStateLoggedOut;   
    [_loginDialog logout]; 
    [self refresh]; 
    [self hideActivityIndicator]; 

} 

@end 

錯誤:

fbconnect=1&skip_api_login=1&m=m&next=https%3A%2F%2Fm.facebook.com%2Fdialog%2Fpermissions.request%3F_path%3Dpermissions.request%26%26redirect_uri%3Dhttp%253A%252F%252Fwww.facebook.com%252Fconnect%252Flogin_success.html%26display%3Dtouch%26type%3Duser_agent%26perms%3Dpublish_stream%26fbconnect%3D1%26from_login%3D1%26client_id%3D169265183116528&refsrc=http%3A%2F%2Fm.facebook.com%2Flogin.php&cancel=http%3A%2F%2Fwww.facebook.com%2Fconnect%2Flogin_success.html%3Ferror_reason%3Duser_denied%26error%3Daccess_denied%26error_description%3DThe%2Buser%2Bdenied%2Byour%2Brequest.&landing_serial=1&refid=9> 

enter image description here

+0

你正在部署IOS6或以前的版本? 。你可以給錯誤代碼? –

+0

與ios5版本..將提供完整的代碼..與安全的理由刪除的應用程序ID ... – lakesh

+0

更新了這個問題..當我調試它卡在displayRequired函數... – lakesh

回答

0

@lakesh我認爲this會幫助你。

+0

不創建應用程序,調試現有的應用程序.... – lakesh

1

看起來你的代碼沒有問題。我想你正在關注Ray的教程,這是一個老教程。

正如你可以看到你的日誌r%3Daccess_denied%26error_訪問被Facebook拒絕。現在可能有多種原因。你需要找出答案。

最簡單的原因可能是您沒有在您的info.plist中添加或更改您的appID(在支持文件夾下的info.plist中的URL類型添加/更改NSdictionary項目確保字符串等於您的appID在您的情況下,無論是3D169265183116528fb3D169265183116528

如果那不行,我建議你看看這兩個問題:

Authentication Fails with Facebook App Installed (iOS)

iOS: Connect to Facebook without leaving the app for authorization

如果我記得正確的grap api這樣的東西應該在你的plist enter image description here

+0

我看到了URL類型。但基本上我添加了另一個名爲NSIDictionary項目稱爲appID項目,並在那裏添加字符串。 – lakesh

+0

看到圖片的答案,我剛剛編輯,從我的舊應用程序,我用facebook圖形API。第0項是facebook appId –

+0

是什麼圖片?你的意思是? – lakesh

相關問題