Search code examples
iosvideoavassetairdrop

Sharing video with airdrop


I am having some trouble in sharing videos with airdrop. So, i am using the AssetLibrary like this :

else if (conformsToVideo) {

        UIAlertView *alert = [[UIAlertView alloc]  initWithTitle:@"Asset Loaded" message:@"Video One Loaded"
        delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];

        self.mediaAsset = [AVAsset assetWithURL:[info objectForKey:UIImagePickerControllerMediaURL]];

        UIActivityViewController * controller = [[UIActivityViewController alloc] initWithActivityItems:@[self.mediaAsset] applicationActivities:nil];
        [self presentViewController:controller animated:YES completion:nil];
}

Maybe this is just not the way to do it, I don't know and I didn't find any tutorial about it so if you have one, I will gladly take it.

Now my problem is that when i select a video everything works fine until the UIActivityViewController pops out , i don't have any error but I can't use airdrop (nor any other service BTW) the only thing I can do is press the Cancel button of the UIAVController.

I am using iOS 8.3.

Thanks for your help


Solution

  • Ok, eventually I found an answer, maybe not the best but I put it there in case it helps someone cause I didn't find much on sharing video with airdrop :

    NSURL * path = [info objectForKey:UIImagePickerControllerReferenceURL];
    BOOL conformsToVideo = (UTTypeConformsTo((__bridge_retained CFStringRef) mediaType, kUTTypeAudiovisualContent));
    
    if (conformsToVideo) {
    
                [self.library assetForURL:path
                              resultBlock:^(ALAsset *asset) {
                                  ALAssetRepresentation* assetRepresentation = [asset defaultRepresentation];
                                  NSString* outputDirectory = [NSString stringWithFormat:@"%@", NSTemporaryDirectory()];
                                  NSString* pathToCopy = [outputDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@", assetRepresentation.filename]];
    
                                  NSUInteger size = (NSUInteger)assetRepresentation.size;
                                  NSMutableData* data = [NSMutableData dataWithLength:size];
    
                                  NSUInteger bytesRead = [assetRepresentation getBytes:data.mutableBytes fromOffset:0 length:size error:nil];
                                  NSURL * url = nil;
                                  if ([data writeToFile:pathToCopy atomically:YES])
                                  {
                                      NSLog(@"Ok");
                                      url = [NSURL fileURLWithPath:pathToCopy];
                                      UIActivityViewController * controller = [[UIActivityViewController alloc] initWithActivityItems:@[url] applicationActivities:nil];
                                      [self presentViewController:controller animated:YES completion:nil];
                                  }
                              } failureBlock:^(NSError *error) {
                                  NSLog(@"Failure to use the ALAsset");
    
                              }
                 ];
            }
    

    I found the solution using this question : Share a video from asset library with AirDrop fails I am merely adding the previous steps to the code given there, hope it can be useful.