Issue making method run at application launch - ios

I'm working on developing an app that uses the camera for a variety of different purposes. Right now, I'm struggling getting a tidbit of code to run when I launch the application:
UIImagePickerController *imageView = [[UIImagePickerController alloc]init];
imageView.delegate = self;
imageView.sourceType = UIImagePickerControllerSourceTypeCamera;
imageView.showsCameraControls = NO;
[self presentViewController:imageView animated:YES completion:NULL];
I need this to execute on the imageView UIView object at launch so when the application is opened, it goes straight to UIImagePickerController. Here is all of my code for the application:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)applicationDidBecomeActive
{
UIImagePickerController *imageView = [[UIImagePickerController alloc]init];
imageView.delegate = self;
imageView.sourceType = UIImagePickerControllerSourceTypeCamera;
imageView.showsCameraControls = NO;
[self presentViewController:imageView animated:YES completion:NULL];
}
#end

If you want to be informed in any controller, you should listen for UIApplicationDidBecomeActiveNotification or similar notifications.
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(applicationDidBecomeActive)
name:UIApplicationDidBecomeActiveNotification
object:nil];
}
to unregister, do
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIApplicationDidBecomeActiveNotification
object:nil];
in dealloc or whenever you don't want to receive the notification anymore.
If you use storyboards, your app delegate should be like this
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
return YES;
}
//…
#end
if you don't use storyboards your app delegate might be
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window makeKeyAndVisible];
self.window.rootViewController = [[ViewController alloc] init];
return YES;
}
//…
#end
and your view controller
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(applicationDidBecomeActive)
name:UIApplicationDidBecomeActiveNotification
object:nil];
}
-(void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIApplicationDidBecomeActiveNotification
object:nil];
}
- (void)applicationDidBecomeActive
{
UIImagePickerController *imageViewPickerController = [[UIImagePickerController alloc] init];
imageViewPickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:imageViewPickerController
animated:NO
completion:NULL];
}
#end

If you want code to run when you launch the app you should put it in:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
If you want the UIImagePickerController to be the first viewController presented you should set it as the window's rootViewController like this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Set up imageView here
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window makeKeyAndVisible];
self.window.rootViewController = imageView;
return YES;
}
Use presentViewController if/when you want to present it from another viewController that is already onscreen.

Related

Redirect to RootViewController ios

Im modifying an older ios app which has the mainscreen setup through :
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
rootViewController = [UITabBarController new];
self.window.rootViewController = rootViewController;
[self.window makeKeyAndVisible];
[self setup];
How can i programatically redirect back to that screen? Im adding a login screen and need to know how to redirect after successful login.
Thanks
#define kUserAuthChangedNotification #"kUserAuthStatusChanged"
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self changeRootControllerWithIsUserSignIn:NO];//You must send user sign in or not
[self.window makeKeyAndVisible];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(userAuthStatusChanged) name:kUserAuthChangedNotification object:nil];
}
#pragma mark helper methods
- (void)userAuthStatusChanged {
[self changeRootControllerWithIsUserSignIn:YES];//You must send user sign in or not
}
- (void)changeRootControllerWithIsUserSignIn:(BOOL)isSignIn {
if(isSignIn){
rootViewController = [UITabBarController new];
self.window.rootViewController = rootViewController;
[self setup];
} else {
YourLoginViewController * ctrl = [YourLoginViewController new];
self.window.rootViewController = ctrl;
}
}
When successful login Or log out you must call:
[[NSNotificationCenter defaultCenter] postNotificationName:kUserAuthChangedNotification object:nil];

Issues with changing URL in WebView after screenshot has been taken

Basically as the title says, I am having some issues in my WebViewAppDelegate
https://github.com/austin4195/Nucleus-iOS/blob/master/Classes/WebViewAppDelegate.m
Lines 5-48, but I believe the issue is something with defining the item that will store the value
Thanks!
There are a lot of issues with your code:
You have return statement in line 36, so your code is never called.
screenshotTaken should be declared outside of application:didFinishLaunchingWithOptions:.
You should use self.webViewController.theWebView instead webViewController.webView.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
navigationController = [[UINavigationController alloc] init];
navigationController.navigationBar.hidden = YES;
navigationController.toolbar.barStyle = UIBarStyleBlack;
WebViewController *webViewController = [[WebViewController alloc] init];
webViewController.urlString = #"http://files.austinapps.org/File%20Site";
[navigationController pushViewController:webViewController animated:NO];
[webViewController release];
[self.window setRootViewController:navigationController];
[self.window makeKeyAndVisible];
self.webViewController = webViewController;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(screenshotTaken)
name:UIApplicationUserDidTakeScreenshotNotification
object:application];
return YES;
}
- (void)screenshotTaken{
[self.webViewController.theWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://website.com/"]]];
}

NSNotificationCenter doesn't work in popup view(ios objC)

I'm working on a iPad project(xcode 7.21+iOS9) and NSNotificationCenter doesn't work.
When user open my app, the tab bar controller will appear.
- (void)viewWillAppear:(BOOL)animated {
if (false == [[MyClass sharedData] getLoginStatus])
{
LoginViewController *loginViewController = [[UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:#"myCustomPopoverLoginVC"];
loginViewController.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentViewController:loginViewController animated:YES completion:^{
}];
...
}
}
- (void)viewDidLoad
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(notificationLogin:) name:#"afterLogin" object:nil];
[super viewDidLoad];
...
}
-(void)notificationLogin:(NSNotification *)notification{
NSLog(#"OhOhOh");
}
In my loginView,
-(IBAction)login:(id)sender{
...
[[NSNotificationCenter defaultCenter] postNotificationName:#"afterLogin" object:nil];
...
}
First of all:
- (void)viewWillAppear:(BOOL)animated {
if (false == [[MyClass sharedData] getLoginStatus])
{
LoginViewController *loginViewController = [[UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:#"myCustomPopoverLoginVC"];
loginViewController.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentViewController:loginViewController animated:YES completion:^{
}];
...
}
}
- (void)viewDidLoad
{
// Add log here to check when its called
-------> NSLog("Add Observer");
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(notificationLogin:) name:#"afterLogin" object:nil];
[super viewDidLoad];
...
}
-(void)notificationLogin:(NSNotification *)notification{
NSLog(#"OhOhOh");
}
then add another log here :
-(IBAction)login:(id)sender{
...
-------> NSLog("Post notification");
[[NSNotificationCenter defaultCenter] postNotificationName:#"afterLogin" object:nil];
...
}
So you can check what called first. Then refer #Sandeep Kumar comment :))
NSNotificationCenter doesn't work in popup view(ios objC)

Recursive calls to viewDidLoad, crashing

in loggerViewController.m:
- (void)viewDidLoad
{
[super viewDidLoad];
UIView* mainView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
[self.view addSubview:mainView]; // <-- Problem is here
}
loggingViewController is an iVar of my appDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
.
.
loggingViewController = [[loggerViewController alloc] init];
[loggingViewController.view setBackgroundColor:[UIColor blueColor]];
// [loggingViewController loadView];
[self.view addSubview:loggingViewController.view];
}
I was expecting my AppDelegate to call loggingViewController, which in turn, set up it's own subviews inside and it would be done. But instead the viewDidLoad gets called recursively I don't understand why?
Try it like this,
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
loggingViewController = [[loggerViewController alloc] init];
self.window.rootViewController = loggingViewController;
[self.window makeKeyAndVisible];
}
The reason for recursive calling is that your self.view is nil and hence it is try to call again and again when you are trying to add it as a subview of appdelegate's view.
- (void)viewDidLoad
{
[super viewDidLoad];
UIView* mainView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
[self.view setBackgroundColor:[UIColor blueColor]];
[self.view addSubview:mainView];
}

Switch between two UIViewControllers programmatically

I have two xib files and for each it's own class which overrides UIViewController class. I have a button in first xib file which should take me to new UIViewController when it's pressed. I have managed to make that transition in different ways:
UIViewController* secondViewController = [[UIViewController alloc] initWithNibName:#"SecondViewControllerName" bundle:[NSBundle mainBundle]];
[self.view addSubview:secondViewController.view];
or
UIViewController* secondViewController = [[UIViewController alloc] initWithNibName:#"SecondViewControllerName" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:secondViewController animated:YES];
In second UIViewController xib file I have also one button which should take me back to first UIViewController. But I don't know how to navigate back to first UIViewController.
I tried:
[self.navigationController popViewControllerAnimated:YES];
for second way of navigating to second UIViewController, but I get unrecognized selector sent to instance error.
Many thanks in advance for any kind of help.
[edit #1: Adding source code]
Here's source code of first view controller .m file:
#import "ViewController.h"
#import "SecondViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (IBAction)switchToSecondPage:(id)sender
{
SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
[UIView beginAnimations:#"flipping view" context:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationTransition: UIViewAnimationTransitionCurlDown forView:self.view cache:YES];
[self.view addSubview:secondViewController.view];
[UIView commitAnimations];
}
#end
Here's source code of second view controller .m file:
#import "SecondViewController.h"
#interface SecondViewController ()
#end
#implementation SecondViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (IBAction)switchToFirstPage:(id)sender
{
[UIView beginAnimations:#"flipping view" context:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationTransition: UIViewAnimationTransitionCurlUp forView:self.view.superview cache:YES];
[self.view removeFromSuperview];
[UIView commitAnimations];
}
#end
Here's my AppDelegate.m file:
#import "AppDelegate.h"
#import "ViewController.h"
#implementation AppDelegate
#synthesize window = _window;
#synthesize viewController = _viewController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application
{
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
}
- (void)applicationWillTerminate:(UIApplication *)application
{
}
#end
Entire project is made as Single View Application and targeted for iOS 5.1 in XCode 4.3. I have found sample project which does the same thing I am having trouble with. Here's URL for that project: http://www.devx.com/wireless/Article/42476/0/page/2
It is just written for earlier iOS version. Is anyone able to see what am I doing wrong, since I am really out of ideas?
PS: With this animations in code, or without - same problem occurs, so animations aren't problem, I've checked that.
[edit #2: Adding error info]
Here's error description. In main method on return line as listed below:
int main(int argc, char *argv[])
{
#autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}
I get this error:
Thread 1: EXC_BAD_ACCESS (code=1, address=0xc00000008)
[edit #3: SOLUTION]
I would like to thank #btype for his solution below. I found out why code from edit #2 didn't work. Problem was doing this:
SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
INSIDE OF IBAction METHOD! Looks like ARC wiped my UIViewController as soon as end of scope was reached and that's why I was getting informations about sending messages to released instance. I made SecondViewController private field in my ViewController class and after that everything worked just fine.
If anyone thinks my explanation is wrong and knows what really bothered me, feel free to answer to this question and correct me.
Edit the code:
in AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
ViewController *viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController];
self.window.rootViewController = navController;
[self.window makeKeyAndVisible];
return YES;
}
FirstViewController.m
- (IBAction)switchToSecondPage:(id)sender
{
[self.navigationController pushViewController:secondViewController animated:YES];
}
SeconViewController.m
- (IBAction)switchToFirstPage:(id)sender
{
[self.navigationController popToRootViewControllerAnimated:YES];
}
This should help.

Resources