【问题标题】:example of uploading video to facebook in ios 6 using SLRequest (without dialog)在 iOS 6 中使用 SLRequest 将视频上传到 facebook 的示例(无对话框)
【发布时间】:2012-11-12 16:05:34
【问题描述】:

我正在寻找一个使用 iOS 6 和 SLRequest 将视频发布到 Facebook 的示例。我已经有用于上传照片的代码。

提前致谢!

【问题讨论】:

    标签: ios facebook video ios6


    【解决方案1】:

    用于分享视频到 FB :

    • 在 developer.facebook.com 上创建应用
    • 在 appName-info.plist 中提及 AppId
    • 在iOS平台下您的应用页面的develper.facebook.com中提及应用BundleID

    然后是编码:

    我们必须先申请读权限,然后再申请写权限。

    -(void)shareOnFB
    {
          __block ACAccount * facebookAccount;
          ACAccountStore *accountStore = [[ACAccountStore alloc] init];
          NSDictionary *emailReadPermisson = [[NSDictionary alloc] initWithObjectsAndKeys:
                                        FB_APP_ID,ACFacebookAppIdKey,
                                        @[@"email"],ACFacebookPermissionsKey,
                                        ACFacebookAudienceFriends,ACFacebookAudienceKey,
                                        nil];
    
          NSDictionary *publishWritePermisson = [[NSDictionary alloc] initWithObjectsAndKeys:
                                           FB_APP_ID,ACFacebookAppIdKey,
                                           @[@"publish_stream"],ACFacebookPermissionsKey,
                                           ACFacebookAudienceFriends,ACFacebookAudienceKey,
                                           nil];
    
         ACAccountType *facebookAccountType = [accountStore
                                          accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
          //Request for Read permission
    
        [accountStore requestAccessToAccountsWithType:facebookAccountType options:emailReadPermisson completion:^(BOOL granted, NSError *error) {
    
        if (granted)
        {
            //Request for write permission
            [accountStore requestAccessToAccountsWithType:facebookAccountType options:publishWritePermisson completion:^(BOOL granted, NSError *error) {
    
            if (granted)
            {
                 NSArray *accounts = [accountStore
                                         accountsWithAccountType:facebookAccountType];
                 facebookAccount = [accounts lastObject];
                 NSLog(@"access to facebook account ok %@", facebookAccount.username);
                 [self uploadWithFBAccount:facebookAccount];
            } 
            else 
            {
                 NSLog(@"access to facebook is not granted");
                   // extra handling here if necesary
                 dispatch_async(dispatch_get_main_queue(), ^{
                     // Fail gracefully...
                    NSLog(@"%@",error.description);
                    [self errorMethodFromFB:error];
                    });
             }
          }];
        }
        else
        {
           [self errorMethodFromFB:error];
        }
      }];
    }
    

    然后从facebook处理错误方法

    -(void)errorMethodFromFB:(NSError *)error
    {
    
        NSLog(@"access to facebook is not granted");
       // extra handling here if necesary
    
      dispatch_async(dispatch_get_main_queue(), ^{
    
          // Fail gracefully...
          NSLog(@"%@",error.description);
    
          if([error code]== ACErrorAccountNotFound)
              [self throwAlertWithTitle:@"Error" message:@"Account not found. Please setup your account in settings app."];
          if ([error code] == ACErrorAccessInfoInvalid)
              [self throwAlertWithTitle:@"Error" message:@"The client's access info dictionary has incorrect or missing values."];
         if ([error code] ==  ACErrorPermissionDenied)
              [self throwAlertWithTitle:@"Error" message:@"The operation didn't complete because the user denied permission."];
         else
              [self throwAlertWithTitle:@"Error" message:@"Account access denied."];
      });
    }
    

    然后消息提醒

    -(void)throwAlertWithTitle:(NSString *)title message:(NSString *)msg
    {
         [[[UIAlertView alloc]initWithTitle:title message:msg delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]show];
    }
    

    处理上传方式

    -(void)uploadWithFBAccount:(ACAccount *)facebookAccount
    {
        ACAccountCredential *fbCredential = [facebookAccount credential];
        NSString *accessToken = [fbCredential oauthToken];
    
        NSURL *videourl = [NSURL URLWithString:[NSString stringWithFormat:@"https://graph.facebook.com/me/videos?access_token=%@",accessToken]];
    
        NSFileManager *fileManager = [NSFileManager defaultManager];
    
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString* foofile = [documentsDirectory stringByAppendingPathComponent:@"me.mov"];
        BOOL fileExists = [fileManager fileExistsAtPath:foofile];
       if (fileExists) 
       {
           NSLog(@"file saved");
       }
       NSString *filePath = foofile;
       NSURL *pathURL = [[NSURL alloc]initFileURLWithPath:filePath isDirectory:NO];
       NSData *videoData = [NSData dataWithContentsOfFile:filePath];
       NSDictionary *params = @{
                             @"title": @"Me  silly",
                             @"description": @"Me testing the video upload to Facebook with the new Social Framework."
                             };
    
       SLRequest *uploadRequest = [SLRequest requestForServiceType:SLServiceTypeFacebook
                                                  requestMethod:SLRequestMethodPOST
                                                            URL:videourl
                                                     parameters:params];
      [uploadRequest addMultipartData:videoData
                           withName:@"source"
                               type:@"video/quicktime"
                           filename:[pathURL absoluteString]];
    
        uploadRequest.account = facebookAccount;
    
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) {
            [uploadRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
                NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
                NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
    
               if(error)
               {
                     NSLog(@"Error %@", error.localizedDescription);
               }
               else
               {
                    [[[UIAlertView alloc]initWithTitle:@"Congratulations!" message:@"Your video is suucessfully posted to your FB newsfeed" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]show];
                    NSLog(@"%@", responseString);
               }
           }];
       });
    }
    

    【讨论】:

      【解决方案2】:

      查看这篇文章:Video Upload with SLRequest

      它清楚地指出了如何将它与集成在 iOS 6 (SLRequest) 中的社交框架一起使用,甚至还指出了如何使用 Facebook 的 SDK。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-09-16
        • 1970-01-01
        • 1970-01-01
        • 2012-10-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多