title: iOS UIImagePickerController and ModalViewController
body: <p>I have written a piece of code which runs inside PhoneGap based app. The code has a plugin which starts UIImagePickerController when some binded button on web page is clicked using following code.</p>
  
  <p>PhotoSelectorCommand.h</p>
  
  <pre><code>#import &lt;Foundation/Foundation.h&gt;
  #import "PhoneGapCommand.h"
  
  @interface PhotoSelectorCommand : PhoneGapCommand&lt;UINavigationControllerDelegate, UIImagePickerControllerDelegate&gt;  {
      NSString *url;
      NSString *extra;
  }
  
  @property (nonatomic, copy) NSString *url;
  @property (nonatomic, copy) NSString *extra;
  
  - (void) startPhotoSelector:(NSMutableArray *)arguments withDict:(NSMutableDictionary *)options;
  - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info;
  - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker;
  
  @end
  </code></pre>
  
  <p>PhotoSelectorCommand.m</p>
  
  <pre><code>#import "PhotoSelectorCommand.h"
  #import "PhoneGapViewController.h"
  #import "PhotoUploaderViewController.h"
  
  @implementation PhotoSelectorCommand
  
  @synthesize url;
  @synthesize extra;
  
  - (void) startPhotoSelector:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options // args: url
  {   
      NSUInteger argc = [arguments count];
  
      if (argc &lt; 1) {
          return; 
      }
      self.url = [arguments objectAtIndex:0];
      if (argc &gt; 1) {
          self.extra = [arguments objectAtIndex:1];   
      }
  
      UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
      imagePicker.delegate = self;
      imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
  
      PhoneGapViewController* cont = (PhoneGapViewController *)[super appViewController];
      [cont presentModalViewController:imagePicker animated:YES];
  }
  
  - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
      //on selected
  
      [picker dismissModalViewControllerAnimated:YES];
  
      PhotoUploaderViewController *photoUploader = [PhotoUploaderViewController alloc];
  
      PhoneGapViewController* cont = (PhoneGapViewController *)[super appViewController];
  
      [cont presentModalViewController:photoUploader animated:YES];
  
      [photoUploader.view removeFromSuperview];
      [picker release];
  
      [picker.view removeFromSuperview];
      [picker release];
  }
  
  - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
      //on cancel
      NSString *failureCallback = [NSString stringWithFormat:@"PhotoSelector._onError('%@');", @"No image selected."];
      [webView stringByEvaluatingJavaScriptFromString:failureCallback];
  
      [picker dismissModalViewControllerAnimated:YES];
      [picker.view removeFromSuperview];
      [picker release];
  }
  
  @end
  </code></pre>
  
  <p>PhotoSelectorViewController.h</p>
  
  <pre><code>#import &lt;UIKit/UIKit.h&gt;
  
  @interface PhotoUploaderViewController : UIViewController {
  
  }
  
  @end
  </code></pre>
  
  <p>PhotoSelectorViewController.m</p>
  
  <pre><code>#import "PhotoSelectorViewController.h"
  
  @implementation PhotoSelectorViewController
  
  -(void)init {
  }
  - (void)viewDidLoad {
      [super viewDidLoad];
  }
  - (void)didReceiveMemoryWarning {
      // Releases the view if it doesn't have a superview.
      [super didReceiveMemoryWarning];
  }
  - (void)viewDidUnload {
      [super viewDidUnload];
  }
  - (void)dealloc {
      [super dealloc];
  }
  
  @end
  </code></pre>
  
  <p>The above code works till it shows UIImagePickerController where user can pick the image. Once after picking any image the controller goes down but another view isn't being presented like the UIImagePickerController. Any idea why?</p>