2016-02-24 123 views
7

我正在寫一個簡單的測試應用程序,從iOS上傳視頻到Facebook。分享到Facebook的視頻

我發現很難找到關於如何使用Swift執行此操作的示例/教程,因爲FacebookSDK的所有文檔都在Objective-C中。

我有這個迄今爲止放在我的用戶界面上的分享按鈕,但它看起來禁用,從我讀過這是因爲沒有內容設置,但我不明白這是如何可能的。

let video_content : FBSDKShareVideoContent = FBSDKShareVideoContent() 

    video_content.video = FBSDKShareVideo(videoURL: getVideoURL()) 

    let button : FBSDKShareButton = FBSDKShareButton() 
    button.shareContent = video_content 

    button.frame = CGRectMake((UIScreen.mainScreen().bounds.width - 100) * 0.5, 50, 100, 25) 
    self.view.addSubview(button) 

getVideoURL()函數返回一個肯定包含的URL視頻NSURL(我已經打印了安慰,並檢查它指向我都存儲在一個文件夾前面的視頻)。

我看着社會框架和它的恥辱我不能使用它,因爲它似乎只用於共享照片和不能共享視頻。

我正在使用最新版本的Xcode,Swift和Facebook SDK。

+0

我有完全相同同樣的問題和我解決了它! 視頻網址應該來自圖書館,而不是您的應用,因此請保存您的視頻並從圖書館獲取網址。那麼它會工作。 – KimCrab

回答

3

@AngryDuck

試試這個代碼將工作

func uploadVideoOnFacebook() { 
    var pathURL: NSURL 
    var videoData: NSData 
    pathURL = NSURL(string: self.strUploadVideoURL)! 
    videoData = NSData.dataWithContentsOfURL(NSURL(string: self.strUploadVideoURL)!) 
    NSString * strDesc 
    strDesc = txtCaption.text! 
    var videoObject: [NSObject : AnyObject] = ["title": "application Name", "description": strDesc, pathURL.absoluteString(): videoData] 
    var uploadRequest: FBRequest = FBRequest(graphPath: "me/videos", parameters: videoObject, HTTPMethod: "POST") 
    self.view!.userInteractionEnabled = false 
    uploadRequest.startWithCompletionHandler({(connection: FBRequestConnection, result: AnyObject, error: NSError) -> Void in 
     if !error { 
      NSLog("Success") 
     } 
     else { 
      NSLog("Error") 
     } 
    }) 
} 
1

我已經在目標c中完成了這項工作,並且遇到了類似的問題。不知道這是否會很快適用。

問題是,如果由於某種原因,視頻必須存儲在NSDocumentDirectoryNSTemporaryDirectory,以便她可以工作。不是,如果這是你的情況。

雖然可以在最壞的情況下FBSDKGraphRequest與/ ME /視頻

我來看到的分享,你要使用視頻這種方式,直接從照片庫中得到的鏈接使用。正如你在this example

看到目前只有在objective-c例子,我希望這可以幫助你

- (void)uploadVideoWithoutSettingsCredentials:(NSURL *)videoUrl withTitle:(NSString *)title withDesc:(NSString *)desc withPrivacy:(NSString *)privacy 
{ 
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH,0), ^{ 
     NSError * error = nil; 

     NSData * videoData = [NSData dataWithContentsOfURL:videoUrl options:0 error:&error]; 
     NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:4L]; 
     [params setObject:videoData forKey:[NSString stringWithFormat:@"%@.MOV",title]]; 
     [params setObject:desc forKey:@"description"]; 

     NSString *privacyString = @"EVERYONE"; 
     if (privacy.length > 0) 
      privacyString = privacy; 

     NSDictionary *privacyDict = [[NSDictionary alloc] initWithObjectsAndKeys: privacyString, @"value", nil]; 
     [params setObject:[Utils objectToJsonString:privacyDict] forKey:@"privacy"]; 
     dispatch_async(dispatch_get_main_queue(), ^{ 
      [[[FBSDKGraphRequest alloc] initWithGraphPath:@"/me/videos" parameters:params HTTPMethod:@"POST"] startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) { 

       NSLog(@"RESPONSE!!! /me/videos"); 
       NSLog(@"result %@",result); 
       NSLog(@"error %@",error); 

       dispatch_async(dispatch_get_main_queue(), ^{ 
        if (!error) { 
         [[[UIAlertView alloc] initWithTitle:@"Success" message:@"Your clip was posted!" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil] show]; 
        } else { 
         [[[UIAlertView alloc] initWithTitle:@"Error!" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil] show]; 
        } 
       }); 
      }]; 
     }); 
    }); 
} 

你可以改變迅速sintax,使用Xcode的自動完成功能。

可以幫助自己的語法迅速一點與this

+0

我試圖上傳的視頻位於文檔目錄中,但我也有NSURL,我也將它保存到相冊中,我嘗試過,但仍顯示禁用,因爲我沒有設置任何媒體內容?你有一個與圖形請求一起工作的例子嗎? – AngryDuck

+0

@AngryDuck我編輯了我的答案 – jose920405

1

我在我的應用程序使用此代碼,並能正常工作對我來說。

此代碼適用於Objective-C但您可以在將它轉換爲swift語法後在swift中使用它。

我希望這會幫助你。

嘗試下面的代碼。

-(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) 
       NSLog("Success"); 
      else 
       NSLog("Error"); 
    }]; 
} 
+0

尋找swift如果你可以翻譯?謝謝 – AngryDuck

+0

我不能因爲我沒有在swift語言上做任何項目。 – DJ1

+0

@AngryDuck - 你有沒有試過把上面的代碼轉換成swift? – DJ1

9

轉換成快捷3和4: - 試試這個

func uploadVideoOnFacebook() { 
     var pathURL: URL 
     var videoData: Data 
     pathURL = URL(string: "Your video url string")! 
     do { 
      videoData = try Data(contentsOf: URL(string: "Your video url string")!) 
      print(videoData) 
      var strDesc : String 
      strDesc = "" 
      let videoObject: [String : Any] = ["title": "application Name", "description": strDesc, pathURL.absoluteString: videoData] 
      let uploadRequest: FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me/videos", parameters: videoObject, httpMethod: "POST") 
      self.view!.isUserInteractionEnabled = false 
      uploadRequest.start(completionHandler: {(connection: FBSDKGraphRequestConnection, result: AnyObject, error: NSError) -> Void in 
       if error == error { 
        NSLog("Error") 

       } 
       else { 
        NSLog("Success") 
       } 
      } as! FBSDKGraphRequestHandler) 
     } catch { 
      print(error) 
     } 

    } 
+0

如何附加place/placeId? –

+0

我收到兩個錯誤「使用未聲明的類型'FBSDKGraphRequest'」和「使用未聲明的類型'FBSDKGraphRequestConnection'」 – VagueExplanation