我正在编写一个简单的测试应用程序来从iOS上传一个视频到Facebook。
我发现很难在网上找到关于如何使用Swift的示例/教程,因为FacebookSDK的所有文档都在Objective中。
到目前为止,我在UI上设置了一个共享按钮,但它看起来是禁用的,据我所读到,这是因为没有内容集,但我不知道这是怎么可能的。
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()函数返回一个NSURL,其中肯定包含视频的url (我已经将它打印到控制台,并检查它指向我先前存储在文件夹中的视频)。
我看了一下社会框架,可惜我不能使用它,因为它似乎只用于分享照片,不能分享视频。
我使用的是最新版本的Xcode、Swift和Facebook。
发布于 2016-03-07 13:12:42
@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")
}
})
}发布于 2017-07-04 07:22:08
将转换为this 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)
}
}发布于 2016-02-24 20:00:31
我在目标c中这样做过,我也遇到了类似的问题。不知道这对斯威夫特是否有效。
问题是,如果出于某种原因,视频必须存储在NSDocumentDirectory或NSTemporaryDirectory中,这样她才能工作。如果是你的案子就不会了。
尽管您可以在最坏的情况下使用FBSDKGraphRequest和/me/视频
我已经看到,这种分享视频的方式,你尝试使用,直接从照片库获得链接。正如您在这个例子中所看到的
目前在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];
}
});
}];
});
});
} 您可以使用Xcode自动完成功能更改为“快速新税”。
您可以在使用这时使用快速语法帮助自己。
https://stackoverflow.com/questions/35611241
复制相似问题