2017-05-11 205 views

回答

0
import FacebookShare 

let shareDialog = ShareDialog(content: myContent) 
shareDialog.mode = .Native 
shareDialog.failsOnInvalidData = true 
shareDialog.completion = { result in 
    // Handle share results 
} 

try shareDialog.show() 

请参阅Facebook的开发API获取更多详细信息

https://developers.facebook.com/docs/swift/sharing/share-dialog

+0

你只是为例说明如何打开的对话框中,我知道如何打开它。我的问题是如何在不使用“UIImagePickerViewController”的情况下从本地视频获取资产URL。 –

0

的Facebook SDK要求资源网址,所以你有你的视频文件传递到资产库,并从中获得新的URL。完整的代码如下所示:

NSURL *_videoURL = [URL to local video file]; 
//initilize the asset library object and define the completion block 
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init]; 
ALAssetsLibraryWriteVideoCompletionBlock videoWriteCompletionBlock = 
^(NSURL *newURL, NSError *error) { 
    if (error) { 
     NSLog(@"Error writing image with metadata to Photo Library: %@", error); 
    } else { 
     NSLog(@"Wrote image with metadata to Photo Library %@", newURL.absoluteString); 
     //share to facebook when we have new asset URL 
     FBSDKShareVideo *shareVideo = [[FBSDKShareVideo alloc]init]; 
     shareVideo.videoURL = newURL; 
     FBSDKShareVideoContent *shareContent = [[FBSDKShareVideoContent alloc] init]; 
     shareContent.video = shareVideo; 
     [FBSDKShareDialog showFromViewController:self withContent:shareContent delegate:nil]; 
    } 
}; 

//write video file and fire up facebook sharing diaglog when complete 
if ([library videoAtPathIsCompatibleWithSavedPhotosAlbum:_videoURL]) 
{ 
    [library writeVideoAtPathToSavedPhotosAlbum:_videoURL 
           completionBlock:videoWriteCompletionBlock]; 
} 

修订版(适用于iOS SDK 9.0或更高版本):

NSURL *_videoURL = [URL to local video file]; 
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{ 
    PHAssetChangeRequest *changeRequest= [PHAssetChangeRequest creationRequestForAssetFromVideoAtFileURL:_videoURL]; 
    _videoPlaceHolder= [changeRequest placeholderForCreatedAsset]; 

} completionHandler:^(BOOL success, NSError * _Nullable error) { 
    if (error) { 
     NSLog(@"Error writing image with metadata to Photo Library: %@", error); 
    } else { 
     NSLog(@"Wrote image with metadata to Photo Library %@", [_videoPlaceHolder localIdentifier]); 
     //example for localidentifier:2BC8A8A8-E974-42D9-AD0F-2F463B353914/L0/001 
     NSArray *id=[_videoPlaceHolder.localIdentifier componentsSeparatedByString:@"/"]; 
     NSString *path= [NSString stringWithFormat:@"assets-library://asset/asset.MOV?id=%@&ext=MOV",id[0]]; 
     _videoAssetURL = [NSURL URLWithString:path]; 

     dispatch_async(dispatch_get_main_queue(), ^{ 
      //share to facebook 
      FBSDKShareVideo *shareVideo = [[FBSDKShareVideo alloc]init]; 
      shareVideo.videoURL = _videoAssetURL; 
      FBSDKShareVideoContent *shareContent = [[FBSDKShareVideoContent alloc] init]; 
      shareContent.video = shareVideo; 
      [FBSDKShareDialog showFromViewController:self withContent:shareContent delegate:nil]; 
     }); 
    } 
}]; 
+0

谢谢你的解决方案!但是'ALAssetsLibrary'已经被弃用了,我需要这个代码在没有'ALAssetsLibrary'的情况下工作,你知道吗?! –

+0

请检查我的更新以使用'PHPhotoLibrary'替换iOS SDK 9.0中的'ALAssetsLibrary',只需确保视频文件的扩展名正确(默认为MOV) –