0

I have a text message window setup in my View Controller's viewDidLoad method implementation. If a specific NSString object contains "YES", then I call my text message window's method and it pops up.

All of this works fine, the only problem is that my text message method call is literally the first thing in my viewDidLoad, yet for some reason the text message window pops up AFTER everything else in the viewDidLoad is performed.

In my viewDidLoad, underneath the text message window code, I setup and launch an AVCapture session and also create a "preview layer" that shows what the camera sees.

Even though this code is below the text message window code, You can see the preview layer of the camera for a couple of seconds before the text message window pops up.

Here is my viewDidLoad method implementation. Notice how my text message window method call of [self showSMS] is first before everything else:

- (void)viewDidLoad
{

    [super viewDidLoad];

    if([_justFinishedSigningUp isEqual:@"YES"]) {

        [self showSMS];        
    }



    if ([self respondsToSelector:@selector(setNeedsStatusBarAppearanceUpdate)]) {
        // iOS 7
        [self prefersStatusBarHidden];
        [self performSelector:@selector(setNeedsStatusBarAppearanceUpdate)];
    } else {
        // iOS 6
        [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];
    }


    _session =[[AVCaptureSession alloc]init];


    [_session setSessionPreset:AVCaptureSessionPresetPhoto];


    _inputDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];


    NSError *error;


    _deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:_inputDevice error:&error];


    if([_session canAddInput:_deviceInput])
        [_session addInput:_deviceInput];


    _previewLayer = [[AVCaptureVideoPreviewLayer alloc]initWithSession:_session];


    CALayer *rootLayer = [[self view]layer];

    [rootLayer setMasksToBounds:YES];


    [_previewLayer setFrame:CGRectMake(0, 0, rootLayer.bounds.size.width, rootLayer.bounds.size.height/2)];

    [_previewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];


    [rootLayer insertSublayer:_previewLayer atIndex:0];


    _stillImageOutput = [[AVCaptureStillImageOutput alloc] init];


    [_session addOutput:_stillImageOutput];

    [_session startRunning];

    }

And then here are the method implementations that help control the text message window:

- (void)showSMS {

    if(![MFMessageComposeViewController canSendText]) {
        UIAlertView *warningAlert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Your device doesn't support SMS!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [warningAlert show];
        return;
    }

    NSArray *recipents = _usersToInviteToApp;
    NSString *message = _textMessageInviteText;

    MFMessageComposeViewController *messageController = [[MFMessageComposeViewController alloc] init];
    messageController.messageComposeDelegate = self;
    [messageController setSubject:@"New Message"];
    [messageController setRecipients:recipents];
    [messageController setBody:message];

    // Present message view controller on screen
    [self presentViewController:messageController animated:YES completion:nil];
}


- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult) result
{
    switch (result) {
        case MessageComposeResultCancelled:
            break;

        case MessageComposeResultFailed:
        {
            UIAlertView *warningAlert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Failed to send SMS!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [warningAlert show];
            break;
        }

        case MessageComposeResultSent:
            break;

        default:
            break;
    }

    [self dismissViewControllerAnimated:YES completion:nil];
}
user3344977
  • 3,454
  • 4
  • 29
  • 82

1 Answers1

1

You're loading something to the view with animated:YES in viewDidLoad, which happens before anything is even on the screen yet ([self presentViewController:messageController animated:YES completion:nil];)

Have you tried either moving everything to viewDidAppear or, if you want the screen there before everything even comes in to view, try setting animated:NO in your -(void)showSMS method.

Stonz2
  • 6,072
  • 4
  • 41
  • 60
  • I tried turning animation off but that didn't help. If I try using viewDidAppear, do I use it to replace viewDidLoad or should I put it above viewDidLoad? Below viewDidLoad? – user3344977 Feb 28 '14 at 22:47
  • can you confirm whether or not viewDidAppear replaces viewDidLoad, and if not where it should be placed? – user3344977 Mar 01 '14 at 08:40
  • It doesn't replace `viewDidLoad`, it works along-side it. Doesn't matter where in the code you physically place it. Check this post for a little more insight: http://stackoverflow.com/questions/5562938/looking-to-understand-the-ios-uiviewcontroller-lifecycle – Stonz2 Mar 03 '14 at 18:03