我正在尝试将图片发布到Facebook。昨天我得到了它,但今天我再次测试它并且它不起作用:S。我找不到问题。我修改了一些东西但没有Facebook与之交互的方法。我希望有人可以帮助我。
我在viewController中获取了所有内容,因为它仅在用户请求时才会登录。
我调试了它,只调用了“publishFacebook”,因为我称之为。它进入Facebook并请求权限,然后你返回,没有任何反应。
网址方案是正确的。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>fb235694579858240</string>
</array>
</dict>
</array>
</plist>
ViewController.h
@interface ViewController : UIViewController <UIImagePickerControllerDelegate, UIActionSheetDelegate, UIGestureRecognizerDelegate, MFMailComposeViewControllerDelegate, FBSessionDelegate, FBDialogDelegate, FBRequestDelegate>
@property (nonatomic, retain) Facebook *facebook;
-(void)postWall;
ViewController.m
-(void) publishFacebook:(UIImage *)img {
//start the facebook action by clicking any button
_publishedImage = img;
_facebook = [[Facebook alloc] initWithAppId:@"235694579858240" 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 alloc] initWithObjects:
@"user_photos",
@"user_likes",
@"read_stream",
nil];
_facebook.sessionDelegate = self;
[_facebook authorize:permissions];
}else{
[self postWall];
}
}
- (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:@"png"];
NSData *imageData = UIImagePNGRepresentation(_publishedImage);
//[NSData dataWithContentsOfFile:filePath]
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
@"Check out my awesome image!. I make it with Trolling for iOS", @"message", imageData, @"source", nil];
[_facebook requestWithGraphPath:@"me/photos" andParams:params andHttpMethod:@"POST" andDelegate:self];
NSLog(@"Image posted");
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Image Posted" message:@"Your image was successfully posted of facebook" delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
[alertView show];
}
-(void)fbDidNotLogin:(BOOL)cancelled{
if (cancelled) {
UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"Could not Login" message:@"Facebook Cannot login please try again" delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles: nil];
[alertView show];
}
}
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
return [_facebook handleOpenURL:url];
}
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
return [_facebook handleOpenURL:url];
}
由于
答案 0 :(得分:1)
您需要仔细关注Facebook的教程,一切都应该有效:developers.facebook.com/docs/mobile/ios/build
您需要的唯一调整是通过UIButton IBAction调用触发登录过程。其余的完全相同。这是一个非常精细的过程,如果你错过了一些东西,很难调试。