1

我已经创建了一个应用程序并成功完成了我只想在 Fcebook 中分享,我通过参考一些教程进行了尝试。这是我在 viewcontroller.m 文件中的代码。

//
#import "ViewController.h"


@interface ViewController ()

@end

@implementation ViewController

@synthesize btnLogin;
@synthesize btnPublish;
@synthesize lblUser;
@synthesize actView;
@synthesize facebook;
@synthesize permissions;
@synthesize isConnected;

-(void)checkForPreviouslySavedAccessTokenInfo
{
// Initially set the isConnected value to NO.
isConnected = NO;

// Check if there is a previous access token key in the user defaults file.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:@"FBAccessTokenKey"] &&
    [defaults objectForKey:@"FBExpirationDateKey"]) {
    facebook.accessToken = [defaults objectForKey:@"FBAccessTokenKey"];
    facebook.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"];

    // Check if the facebook session is valid.
    // If it’s not valid clear any authorization and mark the status as not connected.
    if (![facebook isSessionValid]) {
        [facebook authorize:nil];
        isConnected = NO;
    }
    else {
        isConnected = YES;
    }
}
}


-(void)setLoginButtonImage{
UIImage *imgNormal;
UIImage *imgHighlighted;
UIImageView *tempImage;

// Check if the user is connected or not.
if (!isConnected) {
    // In case the user is not connected (logged in) show the appropriate
    // images for both normal and highlighted states.
    imgNormal = [UIImage imageNamed:@"LoginNormal.png"];
    imgHighlighted = [UIImage imageNamed:@"LoginPressed.png"];
}
else {
    imgNormal = [UIImage imageNamed:@"LogoutNormal.png"];
    imgHighlighted = [UIImage imageNamed:@"LogoutPressed.png"];
}

// Get the screen width to use it to center the login/logout button.
// We’ll use a temporary image view to get the appopriate width and height.
float screenWidth = [UIScreen mainScreen].bounds.size.width;    
tempImage = [[UIImageView alloc] initWithImage:imgNormal];
[btnLogin setFrame:CGRectMake(screenWidth / 2 - tempImage.frame.size.width / 2, btnLogin.frame.origin.y, tempImage.frame.size.width, tempImage.frame.size.height)];

// Set the button’s images.
[btnLogin setBackgroundImage:imgNormal forState:UIControlStateNormal];
[btnLogin setBackgroundImage:imgHighlighted forState:UIControlStateHighlighted];

// Release the temporary image view.
[tempImage  release];
}

-(void)showActivityView{
// Show an alert with a message without the buttons.
UIAlertView *msgAlert = [[UIAlertView alloc] initWithTitle:@"My test app" message:@"Please wait..." delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
[msgAlert show];

// Show the activity view indicator.
actView = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0.0, 0.0, 40.0, 40.0)];
[actView setCenter:CGPointMake(160.0, 350.0)];
[actView setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];
[self.view addSubview:actView];
[actView startAnimating];
}


-(void)stopShowingActivity{
[actView stopAnimating];
UIAlertView *msgAlert = nil;
[UIAlertView:msgAlert dismissWithClickedButtonIndex:0 animated:YES];
}

-(void)saveAccessTokenKeyInfo{
// Save the access token key info into the user defaults.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[facebook accessToken] forKey:@"FBAccessTokenKey"];
[defaults setObject:[facebook expirationDate] forKey:@"FBExpirationDateKey"];
[defaults synchronize];
}

-(void)request:(FBRequest *)request didReceiveResponse:(NSURLResponse *)response {
// Keep this just for testing purposes.
NSLog(@"received response");
}


-(void)request:(FBRequest *)request didLoad:(id)result{
// With this method we’ll get any Facebook response in the form of an array.
// In this example the method will be used twice. Once to get the user’s name to
// when showing the welcome message and next to get the ID of the published post.
// Inside the result array there the data is stored as a NSDictionary.    
if ([result isKindOfClass:[NSArray class]]) {
    // The first object in the result is the data dictionary.
    result = [result objectAtIndex:0];
}

// Check it the “first_name” is contained into the returned data.
if ([result objectForKey:@"first_name"]) {
    // If the current result contains the "first_name" key then it's the user's data that have been returned.
    // Change the lblUser label's text.
    [lblUser setText:[NSString stringWithFormat:@"Welcome %@!", [result objectForKey:@"first_name"]]];
    // Show the publish button.
    [btnPublish setHidden:NO];
}
else if ([result objectForKey:@"id"]) {
    // Stop showing the activity view.
    [self stopShowingActivity];

    // If the result contains the "id" key then the data have been posted and the id of the published post have been returned.
    UIAlertView *al = [[UIAlertView alloc] initWithTitle:@"My test app" message:@"Your message has been posted on your wall!" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
    [al show];
    [al release];
}
}

-(void)request:(FBRequest *)request didFailWithError:(NSError *)error{
NSLog(@"%@", [error localizedDescription]);

// Stop the activity just in case there is a failure and the activity view is animating.
if ([actView isAnimating]) {
    [self stopShowingActivity];
}
}

-(void)fbDidLogin{
// Save the access token key info.
[self saveAccessTokenKeyInfo];

// Get the user's info.
[facebook requestWithGraphPath:@"me" andDelegate:self];
}

-(void)fbDidNotLogin:(BOOL)cancelled{
// Keep this for testing purposes.
//NSLog(@"Did not login");

UIAlertView *al = [[UIAlertView alloc] initWithTitle:@"My test app" message:@"Login cancelled." delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[al show];
}

-(void)fbDidLogout{
// Keep this for testing purposes.
//NSLog(@"Logged out");

// Hide the publish button.
[btnPublish setHidden:YES];
}

- (IBAction)LoginOrLogout {
// If the user is not connected (logged in) then connect.
// Otherwise logout.
if (!isConnected) {
    [facebook authorize:permissions];

    // Change the lblUser label's message.
    [lblUser setText:@"Please wait..."];
}
else {
    [facebook logout:self];
    [lblUser setText:@"Tap on the Login to connect to Facebook"];
}

isConnected = !isConnected;
[self setLoginButtonImage];
}

- (IBAction)Publish {
// Show the activity indicator.
[self showActivityView];

// Create the parameters dictionary that will keep the data that will be posted.
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                               @"My test app", @"name",
                               @"http://www.google.com", @"link",
                               @"FBTestApp app for iPhone!", @"caption",
                               @"This is a description of my app", @"description",
                               @"Hello!\n\nThis is a test message\nfrom my test iPhone app!", @"message",              
                               nil];

// Publish.
// This is the most important method that you call. It does the actual job, the message posting.
[facebook requestWithGraphPath:@"me/feed" andParams:params andHttpMethod:@"POST" andDelegate:self];
}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// Set the permissions.
// Without specifying permissions the access to Facebook is imposibble.
permissions = [[NSArray arrayWithObjects:@"read_stream", @"publish_stream", nil] retain];

// Set the Facebook object we declared. We’ll use the declared object from the application
// delegate.
facebook = [[Facebook alloc] initWithAppId:@"236058923xxxxxx" andDelegate:self];

// Check if there is a stored access token.
[self checkForPreviouslySavedAccessTokenInfo];


// Depending on the access token existence set the appropriate image to the login button.
[self setLoginButtonImage];

// Specify the lblUser label's message depending on the isConnected value.
// If the access token not found and the user is not connected then prompt him/her to login.
if (!isConnected) {
    [lblUser setText:@"Tap on the Login to connect to Facebook"];
}
else {
    // Get the user's name from the Facebook account. The message will be set later.
    [facebook requestWithGraphPath:@"me" andDelegate:self];
}

// Initially hide the publish button.
[btnPublish setHidden:YES];

}

- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

- (void)dealloc {
[btnLogin release];
[lblUser release];
[btnPublish release];
[actView release];
[facebook release];
[permissions release];
[super dealloc];
}


@end

viewcontroller.h 文件中的代码是

#import <UIKit/UIKit.h>
#import "FBConnect.h"
#import "Facebook.h"

@interface ViewController : UIViewController <FBSessionDelegate, FBRequestDelegate,        FBDialogDelegate>{
UIButton *btnLogin;
UIButton *btnPublish;
UILabel *lblUser;
UIActivityIndicatorView *actView;

Facebook *facebook;

NSArray *permissions; 
BOOL isConnected;    

}

@property (retain, nonatomic) IBOutlet UIButton *btnLogin;
@property (retain, nonatomic) IBOutlet UIButton *btnPublish;
@property (retain, nonatomic) IBOutlet UILabel *lblUser;
@property (retain, nonatomic) UIActivityIndicatorView *actView;
@property (retain, nonatomic) Facebook *facebook;
@property (retain, nonatomic) NSArray *permissions;
@property (nonatomic) BOOL isConnected;
  • (IBAction)登录或注销;
  • (IBAction)发布;

    @结尾

谁能说我收到这些错误。

4

1 回答 1

1

编辑:

从您的评论来看,您似乎还将main.cFacebook SDK 示例附带的文件复制到了您的项目中(或者您可能会删除您的文件;main.c 仅包含样板代码)。删除它(或者更好的是,删除与示例相关的所有文件),它应该可以工作。

我的猜测是您没有正确地将您的应用程序与 Facebook 框架链接,但您应该真正查看从编译器获得的确切错误消息。

至于第一点,请检查:创建您的 iOS 项目

至于第二个,在 Xcode 4 中,单击“警报”图标,如附图所示。

在此处输入图像描述

如果您需要更多帮助,请报告您从链接器获得的确切错误。

于 2012-06-01T07:17:36.473 回答