iphone - 通过 iOS 6 应用程序以编程方式在 facebook 上分享视频?

标签 iphone ios facebook video

我正在开发一款应用,需要在 Facebook 上分享视频。当我尝试developer.facebook.com寻求解决方案时,我发现use the graph api to upload a video in ios .它通过图形 API 共享视频。图形 API 可以在 iOS 6 中使用吗?请建议。如果有的话,还请提供任何示例代码。

提前致谢。

最佳答案

尝试一下,我使用了 Facebook SDK 3.1 https://developers.facebook.com/ios/

.h文件

    #import <FacebookSDK/FacebookSDK.h>
    extern NSString *const SCSessionStateChangedNotificationCamera;

.m文件

   NSString *const SCSessionStateChangedNotificationCamera = @"com.facebook.Scrumptious:SCSessionStateChangedNotification";

   -(IBAction)btnFacebookShareClick:(id)sender {
    if (![self openSessionWithAllowLoginUI:YES]) {
           [self showLoginView];
       }
   }

   #pragma mark -
   #pragma mark - Facebook Method

   - (void) performPublishAction:(void (^)(void)) action {
       if ([FBSession.activeSession.permissions indexOfObject:@"publish_actions"] == NSNotFound) {
           [FBSession.activeSession reauthorizeWithPublishPermissions:[NSArray arrayWithObject:@"publish_actions"] defaultAudience:FBSessionDefaultAudienceFriends completionHandler:^(FBSession *session, NSError *error) {
               if (!error) {
                   action();
               }
           }];
       }
       else {
           action();
       }
   }

   #pragma mark -
   #pragma mark Facebook Login Code

   - (void)showLoginView {
       [self dismissViewControllerAnimated:NO completion:nil];
       [self performSelector:@selector(showLoginView1) withObject:nil afterDelay:1.5f];
   }

   - (void)showLoginView1 {

   }

   - (void)sessionStateChanged:(FBSession *)session state:(FBSessionState)state error:(NSError *)error {
       switch (state) {
           case FBSessionStateOpen: {
               if (self != nil) {
                   [[FBRequest requestForMe] startWithCompletionHandler: ^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) {
                       if (error) {
                           //error
                       }else{
                           if ([FBSession.activeSession.permissions indexOfObject:@"publish_actions"] == NSNotFound) {
                               NSArray *permissions = [[NSArray alloc] initWithObjects: @"publish_actions", @"publish_stream", nil];
                               [FBSession.activeSession reauthorizeWithPublishPermissions:permissions defaultAudience:FBSessionDefaultAudienceFriends completionHandler:^(FBSession *session, NSError *error) {
                                   if (!error) {
                                       [self uploadVideoOnFacebook];
                                   }
                                   else {
                                       NSLog(@"%@",error);
                                   }
                               }];
                           }
                           else {
                               [self uploadVideoOnFacebook];
                           }
                       }
                   }];
               }
               FBCacheDescriptor *cacheDescriptor = [FBFriendPickerViewController cacheDescriptor];
               [cacheDescriptor prefetchAndCacheForSession:session];
           }
               break;
           case FBSessionStateClosed: {
               [self StopSpinner];
               UIViewController *topViewController = [self.navigationController topViewController];
               UIViewController *modalViewController = [topViewController modalViewController];
               if (modalViewController != nil) {
                   [topViewController dismissViewControllerAnimated:YES completion:nil];
               }
               //[self.navigationController popToRootViewControllerAnimated:NO];

               [FBSession.activeSession closeAndClearTokenInformation];

               [self performSelector:@selector(showLoginView) withObject:nil afterDelay:0.5f];
           }
               break;
           case FBSessionStateClosedLoginFailed: {
               [self StopSpinner];
               [self performSelector:@selector(showLoginView) withObject:nil afterDelay:0.5f];
           }
               break;
           default:
               break;
       }

       [[NSNotificationCenter defaultCenter] postNotificationName:SCSessionStateChangedNotificationCamera object:session];

       if (error) {
           UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"Error: %@", [CameraViewController FBErrorCodeDescription:error.code]] message:error.localizedDescription delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
           [alertView show];
           [alertView release];
       }
   }

   - (BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI {
       NSArray *permissions = [[NSArray alloc] initWithObjects: @"publish_actions", @"publish_stream", nil];    
       return [FBSession openActiveSessionWithPublishPermissions:permissions defaultAudience:FBSessionDefaultAudienceFriends allowLoginUI:allowLoginUI completionHandler:^(FBSession *session, FBSessionState state, NSError *error) {
           if (!error) {
               [self sessionStateChanged:session state:state error:error];
           }
           else {
               NSLog(@"%@",error);
           }
       }];
   }

   + (NSString *)FBErrorCodeDescription:(FBErrorCode) code {
       switch(code){
           case FBErrorInvalid :{
               return @"FBErrorInvalid";
           }
           case FBErrorOperationCancelled:{
               return @"FBErrorOperationCancelled";
           }
           case FBErrorLoginFailedOrCancelled:{
               return @"FBErrorLoginFailedOrCancelled";
           }
           case FBErrorRequestConnectionApi:{
               return @"FBErrorRequestConnectionApi";
           }case FBErrorProtocolMismatch:{
               return @"FBErrorProtocolMismatch";
           }
           case FBErrorHTTPError:{
               return @"FBErrorHTTPError";
           }
           case FBErrorNonTextMimeTypeReturned:{
               return @"FBErrorNonTextMimeTypeReturned";
           }
           case FBErrorNativeDialog:{
               return @"FBErrorNativeDialog";
           }
           default:
               return @"[Unknown]";
       }
   }

   -(void) uploadVideoOnFacebook {
       NSURL *pathURL;
           NSData *videoData;


               pathURL = [NSURL URLWithString:self.strUploadVideoURL];
               videoData = [NSData dataWithContentsOfURL:[NSURL URLWithString:self.strUploadVideoURL]];


           NSString *strDesc;

               strDesc = txtCaption.text;


           NSDictionary *videoObject = @{@"title": @"application Name",@"description": strDesc,[pathURL absoluteString]: videoData};
           FBRequest *uploadRequest = [FBRequest requestWithGraphPath:@"me/videos" parameters:videoObject HTTPMethod:@"POST"];
           [self.view setUserInteractionEnabled:NO];

           [uploadRequest startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
               if (!error)
                   [AJNotificationView showNoticeInView:self.view type:AJNotificationTypeGreen title:@"Video uploaded successfully" linedBackground:AJLinedBackgroundTypeAnimated hideAfter:3.0];
               else
                   [AJNotificationView showNoticeInView:self.view type:AJNotificationTypeRed title:@"Video uploaded error" linedBackground:AJLinedBackgroundTypeAnimated hideAfter:3.0];

               [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(popViewAfterMKInfo) userInfo:nil repeats:NO];
}];
       }

关于iphone - 通过 iOS 6 应用程序以编程方式在 facebook 上分享视频?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16912183/

相关文章:

ios - 有没有办法让自定义的ios项目模板只有arc?

iOS map 注释问题

iphone - 未找到 Restkit.h 文件

ios - 如何在 iPhone 中使用 AudioBuffer 写入从麦克风本地录制的音频文件?

ios - 在 Swift 中使用带有 IBOutlet 的 UIImageView 在 Interface Builder 中查看它

iOS FacebookSDK 的 Scrumptious 示例应用程序不起作用,因为它总是收到 FBErrorCategoryUserCancelled

php - 如何生成唯一的仅整数 ID,如 Facebook Twitter

reactjs - 命令 `brew install watchman` 运行 `chmod` 但最终安装失败

iphone - 检测何时按下自定义 UIButton

iphone - 如何在 iOS 中进行渐变色更改?