2

I have an application that presents a view controller from a framework. In the framework's view controller, I check for a valid license in loadView. A callback is returned to the UI to not load the view if license is not valid. So, now I'm testing for if the user of my SDK tries to not implement the license check. Even though the license is not valid the vie still displays.

I've tried the following code, but the view gets displayed anyway:

- (void)viewWillAppear:(BOOL)animated
{
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        if (self.validLicense) {
            [self loadCameraPreview];
        } else {
            [self dismissViewControllerAnimated:YES completion:nil];
        }
    } else {
        IDLog(@"No Camera!");
        self.tapRecognizer.enabled = NO;
    }
}

Does anybody know how I can kill the view and not display it? This view is in an SDK. It is not in the application. With the above code, the application just gets stuck on this page and does not work. Which I guess is OK. But I'd really like to make the presented view unload.

The presentation code looks like this:

if (bundle) {
    if (!self.cameraVC) {
        self.cameraVC =
            [[IDCameraViewController alloc] initWithNibName:@"IDCameraViewController"
                                                     bundle:bundle];
        [self.cameraVC setOutImageBinarization:NO];
        [self.cameraVC setTapRecognizerEnabled:YES];
        [self.cameraVC setReturnType:BOTH];

    }
    UIView *cameraView = self.cameraVC.view;
    //if (self.validLicense) {
        [self.cameraVC setCaptureMode:self.mode];
        [self.cameraVC willMoveToParentViewController:self];
        [self addChildViewController:self.cameraVC];
        [self.view addSubview:cameraView];
        [self.cameraVC didMoveToParentViewController:self];
    //}

//        if (self.validLicense == NO) {
//            UIAlertView *alert =
//                [[UIAlertView alloc] initWithTitle:@"License Activation"
//                                           message:@"A valid license must be activated for this product."
//                                          delegate:self
//                                 cancelButtonTitle:@"OK"
//                                 otherButtonTitles:nil];
//            [alert show];
//        }
}

Remember, I'm commenting out the check to test if a user of my SDK tries to use it WITHOUT a valid license.

Patricia
  • 5,019
  • 14
  • 72
  • 152
  • I dont think it is possible to dismiss view controller loadView. Why dont you check the requirements before presenting the viewcontroller such that if the requirements are not fulfilled, you do not present a new viewcontroller. – Sandeep May 12 '14 at 23:09
  • @insane-36 - Give me an answer and I'll give you the credit. Hint: It starts with an "i". – Patricia May 13 '14 at 20:47
  • 1
    Do you mean to override the init method and then return nil for self in any such conditions ? – Sandeep May 14 '14 at 08:00
  • @insane-36 - Give me an answer and I'll give you the credit. – Patricia May 14 '14 at 22:19
  • Ha ha, now thats really funny. What do you mean to give answer ? I told you whatever I knew, but yeah if you could answer yourself that would be good for you. You you get some point yourself and I will be able to learn something new :) – Sandeep May 14 '14 at 22:26
  • @insane-36 - No. It doesn't work that way. If I answer my own question, I don't get any points. You're right....Why not post the answer and get the points? – Patricia May 14 '14 at 22:36

1 Answers1

1

I think it is not possible to close any view controller when loadView method is already called, you will have to wait until the viewDidLoad gets called and then dismiss it or remove it from parentViewController. However, good way will be to decide beforehand whether to present such view controller or not. So, if you check that the person has valid license before presenting the view, that would be much better. But, if you have found out something, then I would surely love to hear it.

Sandeep
  • 20,908
  • 7
  • 66
  • 106
  • 1
    Yes, you really answered the question with a question (above), "Do you mean to override the init method and then return nil for self in any such conditions?". But that helped me solve the problem. I ended up throwing an NSException in the initWithNibName method. If the user of my SDK does't handle the exception, their application will crash. What a way to enforce the rules!!!! – Patricia May 14 '14 at 23:30
  • I never thought it that way. – Sandeep May 15 '14 at 00:10