1

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.

PhotoSelectorCommand.h

#import <Foundation/Foundation.h>
#import "PhoneGapCommand.h"

@interface PhotoSelectorCommand : PhoneGapCommand<UINavigationControllerDelegate, UIImagePickerControllerDelegate>  {
    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

PhotoSelectorCommand.m

#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 < 1) {
        return; 
    }
    self.url = [arguments objectAtIndex:0];
    if (argc > 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

PhotoSelectorViewController.h

#import <UIKit/UIKit.h>

@interface PhotoUploaderViewController : UIViewController {

}

@end

PhotoSelectorViewController.m

#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

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?

Umair A.
  • 6,690
  • 20
  • 83
  • 130

1 Answers1

0

I don't know why you are dismissing picker? this method will be called only after controller is dismissed. Also, you are releasing picker twice and removerFromSuperView? please try with following code and see the commented lines.

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    //on selected

   // [picker dismissModalViewControllerAnimated:YES]; you need not to do this

    PhotoUploaderViewController *photoUploader = [PhotoUploaderViewController alloc];

    PhoneGapViewController* cont = (PhoneGapViewController *)[super appViewController];

    [cont presentModalViewController:photoUploader animated:YES];

   // [photoUploader.view removeFromSuperview]; // where you have added this view?
   // [picker release]; //you do not own this object..

    //[picker.view removeFromSuperview]; // and ?
    //[picker release]; // same here
}
Ravin
  • 8,544
  • 3
  • 20
  • 19
  • this will work but please show me how to present multiple modal views one after another dismissing. – Umair A. Apr 17 '11 at 18:14
  • why you want to do so? you can try this by firs dismissing on viewcontroller without animation and presenting another with animation. – Ravin Apr 18 '11 at 03:15
  • Actually a button on web page allows user to select picker which native code has to upload to some server via HTTP. One modal view is for letting user to select photo and another is for app to upload selected photo showing "the photo is being uploaded..." – Umair A. Apr 18 '11 at 15:55
  • I just tried to do like "I am presenting UIView using presentModalViewController and then inside this view I call again presentModalViewController for UIImagePickerController but the second modal view isn't being opened" – Umair A. Apr 18 '11 at 15:58
  • The basic principal behind Modal Presentation is of temporaray task. If you are having continuous task and dependency then you should better use pushController(May be sometimes what you are trying to do can work but it may cause problems). Also you should present Viewcontroller and not uiview. So Umair, I think you should first decide whether what is the right steps and in which way you should proceed.Thanks. – Ravin Apr 18 '11 at 16:44