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];
}