Reputation: 1049
I am making and app that you modify images and then you could upload it to Twitter, Facebook, etc... My problem it's with Facebook. also fbDidLogin never called. I Follow allot of tutorials to this and search allot and try allot of solutions and nothing.
Because i don't know if the user will export the photo to Facebook I login it only when its necessary I leave the code. I hope someone can help me.
What I need it's to publish my image that I create into the users wall
//<FBSessionDelegate, FBDialogDelegate, FBRequestDelegate>
- (void)publishFacebook:(UIImage *)img {
[self loginFacebook];
NSMutableDictionary * params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
img, @"picture", @"Testing...", @"caption",
nil];
[_facebook requestWithGraphPath:@"me/photos" andParams:params andHttpMethod:@"POST" andDelegate:self];
NSLog(@"Image posted");
}
- (void)loginFacebook {
if (_facebook == nil) {
_facebook = [[Facebook alloc] initWithAppId:kAppId andDelegate:self];
}
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:@"FBAccessTokenKey"] && [defaults objectForKey:@"FBExpirationDateKey"]) {
_facebook.accessToken = [defaults objectForKey:@"FBAccessTokenKey"];
_facebook.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"];
}
if (![_facebook isSessionValid]) {
NSArray *permissions = [NSArray arrayWithObjects:@"email", @"offline_access", @"publish_stream", nil];
[_facebook authorize:permissions];
[defaults synchronize];
}
}
- (void)fbDidLogin {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[_facebook accessToken] forKey:@"FBAccessTokenKey"];
[defaults setObject:[_facebook expirationDate] forKey:@"FBExpirationDateKey"];
[defaults synchronize];
}
And in my app delegate
// Pre 4.2 support
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
return [[_viewController facebook] handleOpenURL:url];
}
// For 4.2+ support
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
return [_viewController.facebook handleOpenURL:url];
}
It go to the Facebook app, and it request all the permissions etc.. and IT said in the console "Image Published" but if I stay in Facebook app without accepting or declining, just stay there it execute the upload image...
If in the Facebook App I accept all the permissions it return to my normal app. The app Delegate methods are return normal values a webpage of Facebook with a really long string.
What could I do?
Thanks
Upvotes: 0
Views: 1239
Reputation: 8501
Try this code...
-(void)buttonPressed:(id)sender{ //start the facebook action by clicking any button
facebook = [[Facebook alloc] initWithAppId:@"YOUR_APP_ID" andDelegate:self];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:@"FBAccessTokenKey"] && [defaults objectForKey:@"FBExpirationDateKey"]) {
facebook.accessToken = [defaults objectForKey:@"FBAccessTokenKey"];
facebook.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"];
}
if (![facebook isSessionValid]) {
[facebook authorize:nil];
}else{
[self postWall];
}
// Pre 4.2 support
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
return [facebook handleOpenURL:url];
}
// after 4.2 support
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
return [facebook handleOpenURL:url];
}
- (void)fbDidLogin {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[facebook accessToken] forKey:@"FBAccessTokenKey"];
[defaults setObject:[facebook expirationDate] forKey:@"FBExpirationDateKey"];
[defaults synchronize];
[self postWall];
}
-(void)postWall{
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"image" ofType:@"jpg"];
NSData *imageData = [NSData dataWithContentsOfFile:filePath];
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
@"Test message", @"message", imageData, @"source", nil];
[facebook requestWithGraphPath:@"me/photos" andParams:params andHttpMethod:@"POST" andDelegate:self];
NSLog(@"Image posted");
}
-(void)fbDidNotLogin:(BOOL)cancelled{
if (cancelled) {
UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"Could not Login" message:@"Facebook Cannot login for your application" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
[alertView show];
}
}
Check out did you put delegate to FBsessionDelegate,FBRequestDelegate in your controller File.Try to run your facebook in your app itself and NOT in SAFARI.To do that check this link. Facebook Implementation within app (without opening in safari or native app)
EDITED: Changed code with postWall method and added fbDidNotLoginMethod.This is the correct one.
Upvotes: 1