presentViewController is not working when the first time application launch - ios

I am facing this weird problem. I have a simple piece of code that is supposed to work fine but it doesn't work when first time launching the app. It works perfectly fine when I run the application second time.
And by the way the following code is used to open the application and display a viewController when i tap the today view widget:
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
OverviewDetailVC *overViewDetail = (OverviewDetailVC *)initViewController;
overViewDetail.productDetail = [self.managedObjectContext executeFetchRequest:FetchRequest error:nil];
overViewDetail.productName = [overViewDetail.productDetail[0] valueForKey:#"generic"];
[self.window.rootViewController presentViewController:overViewDetail animated:YES completion:NULL];
}

Try this:
(BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter] postNotificationName:#"secondshowPushNotification" object:nil];
});
}
FirstView controller:
(void)viewDidLoad
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(showCompanyFromPushNotification:) name:#"secondshowPushNotification" object:nil];
}
-(void)showCompanyFromPushNotification:(NSNotification*)notification
{
[self.rootController presentViewController:secondViewController animated:YES completion:nil];
}
// it open's secondViewController.

Related

"Unable to monitor event loop" crash

I add breakpoint and find that it block in the method that
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
// init local data
[[PDKeychainBindings sharedKeychainBindings] setObject:#"0" forKey:kNEED_GESTURE_LOGIN];
// register notification to notice switching RootVC
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(switchRootControllerWithType:)
name:kNoti_SwitchRootView
object:nil];
// init the third part SDK
[self setupShareSDK];
[self setupBugly];
[self setupUMeng];
[self setupIQKeyBoard];
[self setupZhugeIOWithOptions:launchOptions];
[self setupTrusfort];
// register push service
[self setupJPushWithOptions:launchOptions];
[self dealWithRemoteNotification:launchOptions];
// set local flag
[KXUserIntroManager setupFlag];
if (self.remoteNotification) {
if ([AccountStateManager isLogin])
[self.loginNavigationController showGesturePDViewController];
self.window.rootViewController = self.loginNavigationController;
} else {
self.window.rootViewController = self.launchController;
}
return YES;
}
i also try the way in stackOverFlow ,Add the following code to the above method
NSArray *args = [NSProcessInfo processInfo].arguments;
for(NSString *arg in args){
if ([arg isEqualToString:#"NoAnimations"]) {
[UIView setAnimationsEnabled:false];
}
}
Image link
it is the detail of didFinishLaunching method. it just init some data and create a notification
While this is not a direct answer to your question, it may well be that your issue is symptomatic of the very evident fact that you are doing much too much work, on the main thread, in didFinishLaunchingWithOptions. Your main job in this method is to get out of the way and let the runtime launch the app. You are doing the exact opposite of that.

Methods not called after openURL function ONLY if app is not active

After visiting a share url on a website, viewers are prompted to open the link in my application. If the application is running, it works perfectly.
However, if the application is closed, the viewDidLoad and viewWillAppear methods are not called on my ViewController, so I am not able to open the desired ViewController.
Does anyone know how to allow to get the viewDidLoad function to run if the app is launched from the openURL function?
I currently have:
AppDelegate:
-(BOOL) application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
if (!url) { return NO; }
NSString *party_url = [NSString stringWithFormat:#"%#%#/", PARTYURL, url.lastPathComponent];
[[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:#"WebViewNotification" object:party_url]];
return YES;
}
ViewController:
- (void)viewDidLoad {
[super viewDidLoad];
appDelegate = [AppDelegate getDelegate];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(webViewNotification:) name:#"WebViewNotification" object:nil];
}
- (void)webViewNotification:(NSNotification *)notification {
NSString *url = [notification object];
PartyViewController *partyViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"PartyViewController"];
partyViewController.partyUrl = url;
[self.navigationController pushViewController:partyViewController animated:YES];
}
Again, this works fine if the app was previously opened. However, if it is closed, this does not work. Thank you!
When your app launched from a link without any other operation you don't have a presented ViewController contains this code in your views stacks. so this code will never been executed. consider directly present partyViewController in your CustomTabBarController.

Distinct when local notification was tapped and when received

I have app that send local notifications.
So I have two scenarios.
Application is in foreground, notification center is dragged out. Notification is recived. Notification center is being hidden.
Application is in foreground, notification center is dragged out. Notification is recived. Notification is selected.
My code to handel notifications
AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UILocalNotification* notification = launchOptions[UIApplicationLaunchOptionsLocalNotificationKey];
//When App is launched from notification
[self setupLocalNotification:notification.userInfo];
}
-(void) application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
NSLog(#"USER INFO %#",notification.userInfo);
[self setupLocalNotification:notification.userInfo];
}
-(void)setupLocalNotification:(NSDictionary*)userInfo {
UIApplicationState state = [[UIApplication sharedApplication] applicationState];
if ( state == UIApplicationStateInactive || state == UIApplicationStateBackground){
[[NSUserDefaults standardUserDefaults] setObject:userInfo forKey:#"localNotification"];
//This is for situation when I'm deep in navigation controller and this also trigger viewWillAppear in ViewController
[[((MainPanelViewController*)[[self window] rootViewController]) startNC] popToRootViewControllerAnimated:false];
} else {
//something
}
}
** MainPanelViewController**
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
//This is for situation when currently in ViewController
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(beacomeActive) name:UIApplicationDidBecomeActiveNotification object:nil];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self checkLocalNotification];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil];
}
- (BOOL) checkLocalNotification{
NSDictionary* userInfo = [[NSUserDefaults standardUserDefaults] objectForKey:#"localNotification"];
if (userInfo != nil) {
NSLog(#"Notification");
[self performSegueWithIdentifier:#"ShowNotification" sender:self];
return true;
}
return false;
}
Problem
I don't know how to distinct those two situations. Because every time notification is received when App is inactive then method performSegueWithIdentifier is being called.

AVCamPreviewView initialization is slow and sometimes causes crash after closing / reopening view - using AVCam example project

I'm using the AVCam example project, the latest version from the start of this month (Feb 2014). I have added flash selection functionality and removed the recording feature but I don't think that has any relevance to the issue.
When changing view, and reopening the AVCam view a number of times in succession the app either crashes, or it takes a long time for the preview view to initialize (~15 seconds). This only occurs sometimes.
I assumed the issue relates to clean up when changing view, but the example has what looks to be thorough clean up:
- (void)viewDidDisappear:(BOOL)animated
{
dispatch_async([self sessionQueue], ^{
[[self session] stopRunning];
[[NSNotificationCenter defaultCenter] removeObserver:self name:AVCaptureDeviceSubjectAreaDidChangeNotification object:[[self videoDeviceInput] device]];
[[NSNotificationCenter defaultCenter] removeObserver:[self runtimeErrorHandlingObserver]];
[self removeObserver:self forKeyPath:#"sessionRunningAndDeviceAuthorized" context:SessionRunningAndDeviceAuthorizedContext];
[self removeObserver:self forKeyPath:#"stillImageOutput.capturingStillImage" context:CapturingStillImageContext];
[self removeObserver:self forKeyPath:#"movieFileOutput.recording" context:RecordingContext];
});
}
Here is my code for changing the view (sending captured image in at the same time) (I'm loading the view using a storyboard modal action):
-(void)dealWithNewImage:(UIImage*)imageIn {
[self saveCamState];
//change view and send us the image
UIStoryboard *storyboard = self.storyboard;
DrawingController *drawView = (DrawingController *)[storyboard instantiateViewControllerWithIdentifier:#"DrawingView"];
drawView.imageIn = imageIn;
[self presentViewController:drawView animated:NO completion:nil];
}
I have no idea what is causing the crash and why sometimes the camera preview takes ~15 seconds to display.
Thanks in advance for any help!
Adding the following, within the viewDidDisappear method, after [[self session] stopRunning]; fixed the issue for me:
for(AVCaptureInput *input in captureSession.inputs) {
[captureSession removeInput:input];
}
for(AVCaptureOutput *output in captureSession.outputs) {
[captureSession removeOutput:output];
}

Erase fields when application goes back to foreground

I'm a very beginner in iOS developpment. What I what to do is to delete the content of 2 fields (login and password) when application moves to foregroung after being in background.
To be clear: if a user put the application in foreground and is on the login screen, fields corrsponding to login and password should be empty.
What I have done: I have add a listener to the AppDelegate file that is detecting well background/foreground actions. Here is the code:
- (void)applicationWillEnterForeground:(UIApplication *)application {
#try {
UINavigationController *navigationController = (UINavigationController*)self.window.rootViewController;
UIViewController *cont = [[navigationController viewControllers] objectAtIndex:[navigationController viewControllers].count - 1];
if ([cont isKindOfClass:[LoginScreenController class]]){
NSLog(#"[AppDelegate] ok, we're on login screen");
}
else {
NSLog(#"[AppDelegate] No, we're not");
}
}
#catch(NSException *exp)
{
NSLog(#"[AppDelegate] Fail: %#",exp);
}
}
But when executing the code, I reach an issue linked to BaseRootView... First, do I proceed the appropriate way and then, how to do what I want to do? That's to say, how to erease the fields (I have a function to do this in the LoginController class, so how to call it prperly?)
Thanks !
Put your code in
- (void)applicationDidBecomeActive:(UIApplication *)application
You should use this method instead of
- (void)applicationWillEnterForeground:(UIApplication *)application
when you update user interface this is what this method is for.
Hope this help.
// EXTENDED
Can you try put this code to UIApplicationDidBecomeActiveNotification:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(updateUI)
name:UIApplicationDidBecomeActiveNotification
object:nil];
And add this code to AppDelegate:
-(void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil];
}
and add:
-(void)updateUI
{
//Add code to update ui
}
You can do this by posting a notification. Here in the place of yourFunctionName write your function name which is to erase the fields
In login view controller write this following code.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
// Custom initialization
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(yourFunctionName) name:#"removeText" object:nil];
}
return self;
}
Now post a notification in applicationWillEnterForeground method.
- (void)applicationWillEnterForeground:(UIApplication *)application
{
[[NSNotificationCenter defaultCenter] postNotificationName:#"removeText" object:nil];
}
Hope this helps you.

Resources