我正在尝试与Facebook分享视频但由于某种原因没有任何共享。我之前用图像尝试了它并且它有效但当我尝试视频时它没有用。除非是在网络服务器上,否则你不能分享视频给Facebook吗?
目标代码:
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"sample_movie" ofType:@"mov"];
NSData *videoData = [NSData dataWithContentsOfFile:filePath];
NSMutableDictionary<FBOpenGraphObject> *object = [FBGraphObject openGraphObjectForPost];
object.provisionedForPost = YES;
// for og:title
object[@"title"] = @"Roasted pumpkin seeds";
// for og:type, this corresponds to the Namespace you've set for your app and the object type name
object[@"type"] = @"namespace:video.other";
object[@"video"] = videoData;
代码:
-(void)ShareFB {
[FBRequestConnection startWithGraphPath:@"/me/permissions"
completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (!error){
NSDictionary *permissions= [(NSArray *)[result data] objectAtIndex:0];
if (![permissions objectForKey:@"publish_actions"]){
// Permission hasn't been granted, so ask for publish_actions
[FBSession.activeSession requestNewPublishPermissions:[NSArray arrayWithObject:@"publish_actions"]
defaultAudience:FBSessionDefaultAudienceFriends
completionHandler:^(FBSession *session, NSError *error) {
if (!error) {
if ([FBSession.activeSession.permissions indexOfObject:@"publish_actions"] == NSNotFound){
// Permission not granted, tell the user we will not share to Facebook
NSLog(@"Permission not granted, we will not share to Facebook.");
} else {
// Permission granted, publish the OG story
[self facebookSharing];
}
} else {
// An error occurred, we need to handle the error
// See: https://developers.facebook.com/docs/ios/errors
NSLog(@"Encountered an error requesting permissions: %@", error.description);
}
}];
} else {
// Permissions present, publish the OG story
[self facebookSharing];
}
} else {
// An error occurred, we need to handle the error
// See: https://developers.facebook.com/docs/ios/errors
NSLog(@"Encountered an error checking permissions: %@", error.description);
}
}];
}
// When the user is done picking the image
- (void)facebookSharing
{
// Get the image
// UIImage* image = [UIImage imageNamed:@"button"];
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"sample_movie" ofType:@"mov"];
NSData *videoData = [NSData dataWithContentsOfFile:filePath];
NSMutableDictionary<FBOpenGraphObject> *object = [FBGraphObject openGraphObjectForPost];
object.provisionedForPost = YES;
// for og:title
object[@"title"] = @"Roasted pumpkin seeds";
// for og:type, this corresponds to the Namespace you've set for your app and the object type name
object[@"type"] = @"namespace:video.other";
object[@"video"] = videoData;
// Post custom object
[FBRequestConnection startForPostOpenGraphObject:object completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if(!error) {
// get the object ID for the Open Graph object that is now stored in the Object API
NSString *objectId = [result objectForKey:@"id"];
NSLog(@"object id: %@", objectId);
// create an Open Graph action
id<FBOpenGraphAction> action = (id<FBOpenGraphAction>)[FBGraphObject graphObject];
[action setObject:objectId forKey:@"video.other"];
// create action referencing user owned object
[FBRequestConnection startForPostWithGraphPath:@"/me/namespace:record" graphObject:action completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if(!error) {
NSLog(@"OG story posted, story id: %@", [result objectForKey:@"id"]);
[[[UIAlertView alloc] initWithTitle:@"OG story posted"
message:@"Check your Facebook profile or activity log to see the story."
delegate:self
cancelButtonTitle:@"OK!"
otherButtonTitles:nil] show];
} else {
// An error occurred, we need to handle the error
// See: https://developers.facebook.com/docs/ios/errors
NSLog(@"Encountered an error posting to Open Graph: %@", error.description);
}
}];
} else {
// An error occurred, we need to handle the error
// See: https://developers.facebook.com/docs/ios/errors
NSLog(@"Encountered an error posting to Open Graph: %@", error.description);
}
}];
}