title: How to download video to iPhone photo library?
body: <p>I am trying to download video from a URL to photo album of my iPhone device. I did following code. Its working with my simulator but not working with actual iOS device. I have tried this with iPhone and iPad mini both. </p>
  
  <p>Here is my code</p>
  
  <pre><code>//Download method
  - (IBAction)methodToDownload:(id)sender
  {
       ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
       UISaveVideoAtPathToSavedPhotosAlbum([self   saveVideoToLocal], self, @selector(video:didFinishSavingWithError:contextInfo:), nil);
      library =   nil;
  }
  
  
  
  
  - (void)video:(NSString *) videoPath didFinishSavingWithError: (NSError *) error contextInfo: (void *) contextInfo
   {
      if(error)
         NSLog(@"didFinishSavingWithError: %@", error);
      else
      {
            UIAlertView *alertView  =   [[UIAlertView    alloc]initWithTitle:@"Download" message:@"Download completed." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
           [alertView   show];
  
         //Delete File after creation
         NSFileManager *fileManager = [NSFileManager defaultManager];
         NSError *error;
         BOOL success = [fileManager removeItemAtPath:videoPath error:&amp;error];
      if (success) {
          //            UIAlertView *removeSuccessFulAlert=[[UIAlertView alloc]initWithTitle:@"Congratulation:" message:@"Successfully removed" delegate:self cancelButtonTitle:@"Close" otherButtonTitles:nil];
          //            [removeSuccessFulAlert show];
          NSLog(@"Remove Completed");
      }
      else
      {
          NSLog(@"Could not delete file -:%@ ",[error localizedDescription]);
      }
    }
  }
  
    //Save file to locally
    - (NSString *)saveVideoToLocal {
          NSArray       *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
          NSString  *documentsDirectory = [paths objectAtIndex:0];
          NSString  *filePath = [NSString stringWithFormat:@"%@/test.%@", documentsDirectory,@"mp4"];
          NSLog(@"filePath %@",filePath);
  
  
         //download the file in a seperate thread.
         dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
         NSLog(@"Downloading Started");
  
         NSURL  *url = [NSURL URLWithString:@"XYZ_MOVIE_URL.mov"];
         NSLog(@"URL is %@",url);
         NSData *urlData = [NSData dataWithContentsOfURL:url];
         if ( urlData )
         {
            //saving is done on main thread
            dispatch_async(dispatch_get_main_queue(), ^{
              [urlData writeToFile:filePath atomically:YES];
  
           });
        }
  
   });
   return filePath;
  }
  </code></pre>