我已经实现了 Facebook SDK 3.0。以下代码完美运行,但仅当用户登录 Facebook时才会出现错误:
重现错误:
- 用户通过 fbView.m 类登录并可以发布状态
- 用户按下导航栏后退按钮 -> DetailViewController 已加载
- 按下 loadFB 按钮返回 FbView
- 仅当用户登录 Facebook 时应用程序崩溃
有人知道这个问题的解决方案吗?也许与登录缓存有关?...有人吗?
-(void)loadFB{
NSLog(@"test");
FbView *f = [[[FbView alloc] initWithNibName:@"FbView" bundle:[NSBundle mainBundle]]retain];
f.RSstatus = self.titleDV;
f.RSpostURL = self.postURL;
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:FbView animated:YES];
[FbView release];
}
// FbView.m
#import "FbView.h"
#import "DetailViewController.h"
#import "AppDelegate.h"
@interface FbView () <FBLoginViewDelegate>
@property (strong, nonatomic) IBOutlet FBProfilePictureView *profilePic;
@property (strong, nonatomic) IBOutlet UIButton *buttonPostStatus;
@property (strong, nonatomic) IBOutlet UIButton *buttonPostPhoto;
@property (strong, nonatomic) IBOutlet UIButton *buttonPickFriends;
@property (strong, nonatomic) IBOutlet UIButton *buttonPickPlace;
@property (strong, nonatomic) IBOutlet UILabel *labelFirstName;
@property (strong, nonatomic) id<FBGraphUser> loggedInUser;
- (IBAction)postStatusUpdateClick:(UIButton *)sender;
- (IBAction)postPhotoClick:(UIButton *)sender;
- (IBAction)pickFriendsClick:(UIButton *)sender;
- (IBAction)pickPlaceClick:(UIButton *)sender;
- (void)showAlert:(NSString *)message
result:(id)result
error:(NSError *)error;
@end
@implementation FbView
@synthesize buttonPostStatus = _buttonPostStatus;
@synthesize buttonPostPhoto = _buttonPostPhoto;
@synthesize buttonPickFriends = _buttonPickFriends;
@synthesize buttonPickPlace = _buttonPickPlace;
@synthesize labelFirstName = _labelFirstName;
@synthesize loggedInUser = _loggedInUser;
@synthesize profilePic = _profilePic;
@synthesize RSstatus,RSpostURL;
- (void)viewWillDisappear:(BOOL)animated
{
[FBSession.activeSession close];
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[FBSession.activeSession close];
[super viewDidDisappear:animated];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Create Login View so that the app will be granted "status_update" permission.
FBLoginView *loginview =
[[FBLoginView alloc] initWithPermissions:[NSArray arrayWithObject:@"status_update"]];
loginview.frame = CGRectOffset(loginview.frame, 5, 5);
loginview.delegate = self;
[self.view addSubview:loginview];
[loginview sizeToFit];
[self check];
}
-(void)check{
AppDelegate *del=[[AppDelegate alloc]init];
if(!FBSession.activeSession == nil){
NSLog(@"%@",FBSession.activeSession );
} else {
NSLog(@"nee");
}
}
- (void)viewDidUnload {
self.buttonPickFriends = nil;
self.buttonPickPlace = nil;
self.buttonPostPhoto = nil;
self.buttonPostStatus = nil;
self.labelFirstName = nil;
self.loggedInUser = nil;
self.profilePic = nil;
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
} else {
return YES;
}
}
- (void)loginViewShowingLoggedInUser:(FBLoginView *)loginView {
// first get the buttons set for login mode
self.buttonPostPhoto.enabled = YES;
self.buttonPostStatus.enabled = YES;
self.buttonPickFriends.enabled = YES;
self.buttonPickPlace.enabled = YES;
}
- (void)loginViewFetchedUserInfo:(FBLoginView *)loginView
user:(id<FBGraphUser>)user {
// here we use helper properties of FBGraphUser to dot-through to first_name and
// id properties of the json response from the server; alternatively we could use
// NSDictionary methods such as objectForKey to get values from the my json object
self.labelFirstName.text = [NSString stringWithFormat:@"Hello %@!", user.first_name];
// setting the profileID property of the FBProfilePictureView instance
// causes the control to fetch and display the profile picture for the user
self.profilePic.profileID = user.id;
self.loggedInUser = user;
}
- (void)loginViewShowingLoggedOutUser:(FBLoginView *)loginView {
self.buttonPostPhoto.enabled = NO;
self.buttonPostStatus.enabled = NO;
self.buttonPickFriends.enabled = NO;
self.buttonPickPlace.enabled = NO;
self.profilePic.profileID = nil;
self.labelFirstName.text = nil;
}
// Post Status Update button handler
- (IBAction)postStatusUpdateClick:(UIButton *)sender {
// Post a status update to the user's feed via the Graph API, and display an alert view
// with the results or an error.
NSString *message = [NSString stringWithFormat:@"%@ \n%@",RSstatus,RSpostURL];
[FBRequestConnection startForPostStatusUpdate:message
completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
[self showAlert:message result:result error:error];
self.buttonPostStatus.enabled = YES;
}];
self.buttonPostStatus.enabled = NO;
}
// Post Photo button handler
- (IBAction)postPhotoClick:(UIButton *)sender {
// Just use the icon image from the application itself. A real app would have a more
// useful way to get an image.
UIImage *img = [UIImage imageNamed:@"Icon-72@2x.png"];
[FBRequestConnection startForUploadPhoto:img
completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
[self showAlert:@"Photo Post" result:result error:error];
self.buttonPostPhoto.enabled = YES;
}];
self.buttonPostPhoto.enabled = NO;
}
// Pick Friends button handler
- (IBAction)pickFriendsClick:(UIButton *)sender {
FBFriendPickerViewController *friendPickerController = [[FBFriendPickerViewController alloc] init];
friendPickerController.title = @"Pick Friends";
[friendPickerController loadData];
// Use the modal wrapper method to display the picker.
[friendPickerController presentModallyFromViewController:self animated:YES handler:
^(FBViewController *sender, BOOL donePressed) {
if (!donePressed) {
return;
}
NSString *message;
if (friendPickerController.selection.count == 0) {
message = @"<No Friends Selected>";
} else {
NSMutableString *text = [[NSMutableString alloc] init];
// we pick up the users from the selection, and create a string that we use to update the text view
// at the bottom of the display; note that self.selection is a property inherited from our base class
for (id<FBGraphUser> user in friendPickerController.selection) {
if ([text length]) {
[text appendString:@", "];
}
[text appendString:user.name];
}
message = text;
}
[[[UIAlertView alloc] initWithTitle:@"You Picked:"
message:message
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil]
show];
}];
}
// Pick Place button handler
//- (IBAction)pickPlaceClick:(UIButton *)sender {
// FBPlacePickerViewController *placePickerController = [[FBPlacePickerViewController alloc] init];
// placePickerController.title = @"Pick a Seattle Place";
// placePickerController.locationCoordinate = CLLocationCoordinate2DMake(47.6097, -122.3331);
// [placePickerController loadData];
//
// // Use the modal wrapper method to display the picker.
// [placePickerController presentModallyFromViewController:self animated:YES handler:
// ^(FBViewController *sender, BOOL donePressed) {
// if (!donePressed) {
// return;
// }
//
// [[[UIAlertView alloc] initWithTitle:@"You Picked:"
// message:placePickerController.selection.name
// delegate:nil
// cancelButtonTitle:@"OK"
// otherButtonTitles:nil]
// show];
// }];
//}
// UIAlertView helper for post buttons
- (void)showAlert:(NSString *)message
result:(id)result
error:(NSError *)error {
NSString *alertMsg;
NSString *alertTitle;
if (error) {
alertMsg = error.localizedDescription;
alertTitle = @"Error";
} else {
NSDictionary *resultDict = (NSDictionary *)result;
alertMsg = [NSString stringWithFormat:@"Successfully posted '%@'.\nPost ID: %@",
message, [resultDict valueForKey:@"id"]];
alertTitle = @"Success";
}
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:alertTitle
message:alertMsg
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
}
@end