![](/img/trans.png)
[英]How to upload video on facebook using facebook sdk 4.6 on iOS8
[英]upload video to facebook using facebook sdk 3.1 on ios 6.0
我的一個應用是將視頻上傳到Facebook帳戶。 我在網上查了一下,但發現大多數解決方案都是舊的或已刪除。 有沒有更新的解決方案?
歡迎任何評論
在您發布到Facebook之前,您必須獲得發布(寫入)權限,使用本機集成或Facebook SDK,規則是您必須首先獲得寫入權限之前的讀取權限。
因此,請確保在嘗試上傳視頻之前,您應該已經請求了基本信息(例如電子郵件),然后,一旦您擁有此信息,您就可以請求寫入權限。 上傳視頻所需的權限是publish_stream
。
使用本機iOS 6 Facebook集成,您應該使用requestForServiceType:requestMethod:URL:parameters:
SLRequest
方法,如下所示:
- (void)upload{
NSURL *videourl = [NSURL URLWithString:@"https://graph.facebook.com/me/videos"];
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"me" ofType:@"mov"];
NSURL *pathURL = [[NSURL alloc]initFileURLWithPath:filePath isDirectory:NO];
NSData *videoData = [NSData dataWithContentsOfFile:filePath];
NSDictionary *params = @{
@"title": @"Me being silly",
@"description": @"Me testing the video upload to Facebook with the new system."
};
SLRequest *uploadRequest = [SLRequest requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodPOST
URL:videourl
parameters:params];
[uploadRequest addMultipartData:videoData
withName:@"source"
type:@"video/quicktime"
filename:[pathURL absoluteString]];
uploadRequest.account = self.facebookAccount;
[uploadRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
if(error){
NSLog(@"Error %@", error.localizedDescription);
}else
NSLog(@"%@", responseString);
}];
}
這里需要注意的是視頻數據不會進入參數字典,必須使用addMultipartData:withName:type:filename:
方法將其添加到SLRequest
對象中。
另請注意,添加視頻數據時,文件名非常重要。 這里我只是使用文件的完整路徑。
如果您必須支持iOS 6之前的iOS版本,或者您希望出於任何其他原因使用Facebook SDK 3.1,則上傳視頻會略有不同。
您必須使用FBRequest
對象和包含視頻詳細信息的NSDictionary
。 我推薦使用的方法是requestWithGraphPath:parameters:HTTPMethod:
我已經使用了這個方法,但你應該能夠使用其他一些方法來創建你的請求對象。
以下代碼適用於Facebook SDK 3.1上傳視頻:
- (void)upload{
if (FBSession.activeSession.isOpen) {
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"me" ofType:@"mov"];
NSURL *pathURL = [[NSURL alloc]initFileURLWithPath:filePath isDirectory:NO];
NSData *videoData = [NSData dataWithContentsOfFile:filePath];
NSDictionary *videoObject = @{
@"title": @"FB SDK 3.1",
@"description": @"hello there !",
[pathURL absoluteString]: videoData
};
FBRequest *uploadRequest = [FBRequest requestWithGraphPath:@"me/videos"
parameters:videoObject
HTTPMethod:@"POST"];
[uploadRequest startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (!error)
NSLog(@"Done: %@", result);
else
NSLog(@"Error: %@", error.localizedDescription);
}];
}
}
在這里,您可以看到,我們將視頻數據添加到parameters
字典中,與之前的解決方案不同,它與title
和description
(兩個可選參數)一起存在。 另請注意,這里沒有Facebook文檔中指定的密鑰source
。 密鑰的名稱是視頻的文件名。 我不知道為什么這不應該是source
,但使用source會導致com.facebook.sdk錯誤5 。
我提到的我在Facebook上提交的錯誤,您可以在此鏈接上看到此報告 - 除非我在解釋文檔時出錯。 請嘗試該bug並報告是否可以重現它。 謝謝 !
publish_stream
不足以上傳(讀取),您需要請求“ video_upload
”權限。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.