我正在尝试使用 ios 应用程序登录 facebook。当我单击应用程序中的登录按钮时,它会崩溃。
它在这一点上崩溃:
[self 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 的新功能
已编辑
#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&width=292&connections=0&stream=false&header=false&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>