How to upload/share video on Facebook ?

前端 未结 3 938
不知归路
不知归路 2021-02-04 20:16

I am making a test app through that I want to post video on facebook. I am using latest sdk of facebook. But I am not able to post it on facebook.

My code is as below.<

相关标签:
3条回答
  • 2021-02-04 20:47

    Successfully tested On FaceBook SDK 3.14.1

    Recommendation: 3 properties in .plist file

    set FacebookAppID,FacebookDisplayName,
    URL types->Item 0->URL Schemes set to facebookappId prefix with fb See

    -(void)shareOnFaceBook
    {
        //sample_video.mov is the name of file
        NSString *filePathOfVideo = [[NSBundle mainBundle] pathForResource:@"sample_video" ofType:@"mov"];
    
        NSLog(@"Path  Of Video is %@", filePathOfVideo);
        NSData *videoData = [NSData dataWithContentsOfFile:filePathOfVideo];
        //you can use dataWithContentsOfURL if you have a Url of video file
        //NSData *videoData = [NSData dataWithContentsOfURL:shareURL];
        //NSLog(@"data is :%@",videoData);
        NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                   videoData, @"video.mov",
                                   @"video/quicktime", @"contentType",
                                   @"Video name ", @"name",
                                   @"description of Video", @"description",
                                   nil];
    
       if (FBSession.activeSession.isOpen)
       {
            [FBRequestConnection startWithGraphPath:@"me/videos"
                                     parameters:params
                                     HTTPMethod:@"POST"
                              completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
                                  if(!error)
                                  {
                                      NSLog(@"RESULT: %@", result);
                                      [self throwAlertWithTitle:@"Success" message:@"Video uploaded"];
                                  }
                                  else
                                  {
                                      NSLog(@"ERROR: %@", error.localizedDescription);
                                      [self throwAlertWithTitle:@"Denied" message:@"Try Again"];
                                  }
                              }];
        }
        else
        {
            NSArray *permissions = [[NSArray alloc] initWithObjects:
                                @"publish_actions",
                                nil];
            // OPEN Session!
            [FBSession openActiveSessionWithPublishPermissions:permissions defaultAudience:FBSessionDefaultAudienceEveryone  allowLoginUI:YES
                                         completionHandler:^(FBSession *session,
                                                             FBSessionState status,
                                                             NSError *error) {
                                             if (error)
                                             {
                                                 NSLog(@"Login fail :%@",error);
                                             }
                                             else if (FB_ISSESSIONOPENWITHSTATE(status))
                                             {
                                                 [FBRequestConnection startWithGraphPath:@"me/videos"
                                                                              parameters:params
                                                                              HTTPMethod:@"POST"
                                                                       completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
                                                                           if(!error)
                                                                           {
                                                                               [self throwAlertWithTitle:@"Success" message:@"Video uploaded"];
    
                                                                               NSLog(@"RESULT: %@", result);
                                                                           }
                                                                           else
                                                                           {
                                                                               [self throwAlertWithTitle:@"Denied" message:@"Try Again"];
    
                                                                               NSLog(@"ERROR: %@", error.localizedDescription);
                                                                           }
    
                                                                       }];
                                             }
                                         }];
            }
    }
    

    I GOT Error In first time of App runs:

     The operation couldn’t be completed. (com.facebook.sdk error 5.)
    

    It happens when facebook is being inited. Next time i open my app, it works fine, its always the first time. Tried everything in app, but it seems to be on the Facebook SDK side.

    Few causes for seeing com.facebook.sdk error 5:

    • Session is is not open. Validate.
    • Facebook has detected that you're spamming the system. Change video name.
    • Facebook has a defined limit using the SDK. Try a different app.
    • Wrong publish permission. Give publish_actions a spin.
    • many more....
    0 讨论(0)
  • 2021-02-04 20:50

    You need to download FacebookSDK first and then add following framework into your project

    FacebookSDK.framework, FBSDKLoginKit.framework, FBSDKShareKit.framework,
    Bolts.framework,FBSDKCoreKit.framework
    

    import them, and write followin code

    if(![FBSDKAccessToken currentAccessToken])
        {
            FBSDKLoginManager *login1 = [[FBSDKLoginManager alloc]init];
    
            [login1 logInWithPublishPermissions:@[@"publish_actions"] handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
    
                FBSDKShareVideo *video = [[FBSDKShareVideo alloc] init];
    
                video.videoURL = videoAssetURL;
                FBSDKShareVideoContent *content = [[FBSDKShareVideoContent alloc] init];
                content.video = video;
    
                [FBSDKShareDialog showFromViewController:self withContent:content delegate:nil];
    
    
    
    
            }];
        }
        else {
            FBSDKShareVideo *video = [[FBSDKShareVideo alloc] init];
            video.videoURL = videoAssetURL;
            FBSDKShareVideoContent *content = [[FBSDKShareVideoContent alloc] init];
            content.video = video;
    
            [FBSDKShareDialog showFromViewController:self withContent:content delegate:nil];
    
    
    
    
        }
    

    The video URL videoURL must be an asset URL. You can get a video asset URL e.g. from UIImagePickerController.

    or for recording video you can take as follow

    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    [library writeVideoAtPathToSavedPhotosAlbum:[NSURL URLWithString:[[NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches"] stringByAppendingFormat:@"/current.mov"]] completionBlock:^(NSURL *assetURL, NSError *error)
     {
    
         videoAssetURL =assetURL;
    
     }];
    

    for more detail you can use https://developers.facebook.com/docs/sharing/ios

    0 讨论(0)
  • 2021-02-04 20:51

    Get the publish permission

    NSArray* permissions = [[NSArray alloc] initWithObjects:
                                @"publish_stream", nil];
        [facebook authorize:permissions delegate:self];
        [permissions release];
    

    Try this

    - (void)fbDidLogin {
        NSString *filePath = [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"mov"];
        NSData *videoData = [NSData dataWithContentsOfFile:filePath];
        NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                       videoData, @"video.mov",
                                       @"video/quicktime", @"contentType",
                                       @"Video Test Title", @"title",
                                       @"Video Test Description", @"description",
                           nil];
        [facebook requestWithGraphPath:@"me/videos"
                             andParams:params
                         andHttpMethod:@"POST"
                           andDelegate:self];
    }
    
    0 讨论(0)
提交回复
热议问题