2017-05-11 13 views
0

私はFacebookのビデオを共有し、このビデオのfileURLを持っていますが共有する必要があります。この動画のアセットURLが必要です。 UIImagePickerViewControllerを使用せずにローカルビデオからアセットURLを取得するにはどうすればよいですか?iOS SDKでfacebookでビデオを共有

答えて

0
import FacebookShare 

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

try shareDialog.show() 

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

+0

あなたexempleはちょうど私がそれを開く方法を知って、ダイアログボックスを開く方法を示しています。私の質問は、 'UIImagePickerViewController'を使わずにアセットURLをローカルビデオから取得する方法です。 –

0

詳細については、Facebookの開発者向けAPIを参照してくださいFacebookのSDKは、資産URLを要求するので、あなたは、資産ライブラリにビデオファイルを渡し、そこから新しいURLを取得する必要があります。

(9.0以降のiOS SDKのために)が更新
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]; 
} 

::のような完全なコードを見

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

iOS SDK 9.0の 'ALAssetsLibrary'の代わりに' PHPhotoLibrary'を使用するためのアップデートをチェックしてください。ビデオファイルの拡張子が正しいことを確認してください(デフォルトではMOV) –

関連する問題