iPhone viewDidBecomeActive (or just resuming from background) questions - iphone

I've been researching this for a few hours and I'm still unsure if I am even heading in the right direction.
What I would like to do:
When my app is loaded, I need to determine if it's the first time the user has loaded the app that day.
How I was planning on doing it:
I made some labels to display the current day and last day that they used the app. (See sample code below).
-(void)pressStatus{
NSString * currentDay;
currentString = currentDay.text;
NSString * lastDay;
lastString = lastDay.text;
if([currentDay isEqualToString: lastDay]){
status.text = #"same";
}
else {
status.text = #"different";
[self autoReset];
}
So, it does what I need to do, but at the push of a button. Instead, I would like it to happen as soon as the app loads from the background. From what I read, I think I should use viewDidBecomeActive, but I'm not fully understanding it. I've never used the delegate as of yet and at this point everything I'm reading is confusing me more than the last.
Questions
Is viewDidBecomeActive the best way to do this?
If so, can I call the pressStatus function in the MainViewController from within applicationDidBecomeActive in the delegate?
Thanks in advance.

This web page has some very nice flow charts which describe app foregrounding etc.:
http://www.cocoanetics.com/2010/07/understanding-ios-4-backgrounding-and-delegate-messaging
There's no viewDidBecomeActive btw, are you thinking of something else?

Related

UIImagePickerController CameraDevice picks wrong device every other time (using ARC)

My app takes a video (using a custom overlay, in case that's relevant) where I have the cameraDevice set to front facing camera. 3 videos need to be taken, though the cameraDevice alternates between front and rear each time the UIImpagePickerController is called to take the video. On each occasion the
picker.cameraDevice = UIImagePickerControllerCameraDeviceFront;
is used within the code, though as mentioned the app ignores this command every other time.
I see this problem has been asked before, though I am using ARC so the previously suggested solutions of releasing the UIImagePickerController is not an option for me.
Thanks in advance, Jim.
You cannot release the picker in ARC but you can still create a brand new one like this:
picker = nil;
picker = [[UIImagePickerController alloc] init];
// configure picker
Notice that in Apple's sample code "Using UIImagePickerController to Select Pictures and Take Photos" they call a method each time which does exactly this.

iPhone app crashing while navigating

i am developing a rss reader for iPhone, and i have a list of NSDictionary with the name of the site and the url. When i click the DetailDisclosureButton it takes me to another screen with the feeds of the site(I haven't implemented the xlm parser, so its just brings me the screen). Here's the problem after i go back and forth specifically 5 time my app crashes, and it does not show any message on the log.
I have no idea what is happening, what could cause such an error?
thanks!
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
// Gets the site;
NSDictionary *site = [sites objectAtIndex:indexPath.row];
// instantiate a view to see the unread news.
ViewUnreadController *uController = [[ViewUnreadController alloc] init];
uController.title = [site objectForKey:#"site"];
uController.site = site;
// adds the view to NavigationControllers stack (just adds a back button)
[self pushView:uController withBackTitle:#"Signatures"];
[site release];
[uController release];
And an additional information i am using the iPhone simulator 4.0.
Ha! I realized that i was releasing the "site" variable (without retaining it), since it was pointing to an object in a NSMutableArray the reference count reached zero!
Certainly you are over-releasing/under-retainig something. But without seeing you code, nobody will be able to tell more.
NSZombieEnabled might give you some useful informations
Well, you haven't really given us any information, so it could be a huge number of things. Profile your app, post the crash log here as an update to your question. Is this seen on the simulator or a device? Which device or what settings in the simulator are you using?
Arbitrarily guessing, maybe you're making new UIControllers and views every time and you're running out of memory. But that's just a shot in the dark at a target you haven't told me where is, or if it's even there.

FBConnect for iPhone: sessionDidNotLogin, sessionDidLogout, session didLogin not called the second time I load a view

My problem is very similar to this question, however I am posting a new one, as the answer to the aforementioned does not seem to solve my problem.
I have a multiview application - the first view is where the user logs in to Facebook, and the second where he picks an image and uploads it there. The first time the app runs, everything works fine, however if I return to the login view and press logout, then any calls to sessionDidNotLogin, sessionDidLogout or session didLogin don't seem to work.
I found out that the first time, if I NSLog(#"%#",session.delegates); I have 2; my LoginViewController and the FBLoginButton. However, apart from that first time, the same log prints only the LoginViewController and not the FBLoginButton. I guess this is connected somehow, but I don't know how to solve it.
Do I have to manually add the FBLoginButton to the session delegates, or I'm doing something else wrong here?
Thank you for any help/suggestion.
I figured out the problem, thanks to a post from the Facebook developers forum.
Everytime a view was loaded, I was creating a new session instance (even though I thought that it would be a singleton and the library should have taken care of it).
So, in order to make this work:
fbSession = [FBSession session];
if (!fbSession) {
fbSession = [[FBSession sessionForApplication:kApiKey secret:kApiSecret delegate:self] retain];
} else {
[[fbSession delegates] addObject:self];
}

How to delay Default.png?

How can I delay the app loading to show the splash screen for longer?
You should let the app start as usual then make the first view that appears have the identical image on it as the splash screen. Start a timer and then replace that view with your real application root view after a few seconds.
Deliberately delaying the actual application launch is a big no-no.
UPDATE: No seriously, DON'T do this!
Or us the C function
sleep(9);
Putting this in applicationDidFinishLaunching: will cause you program to pause for 9 seconds, any other integer may be entered as well.
EDIT: I've learned a lot in the past year. Don't do this. The reason being that the springboard will automatically stop the app launching if it takes too long. That timing is poorly documented so even one second can result in the app failing.
This question is similar: splash screen like tap tap revenge 3
Basically, in your applicationDidFinishLaunching:, add an image view on top of other views containing your Default.png.
See the above discussion of why you probably should not delay your app load in this way. But if you happen to have a scenario where sleeping for short duration would be preferable to the overhead of switching out a view, use NSThread's sleepForTimeIntervale instead of sleep(). It's more framework friendly and you have more granular control over the sleep time:
[NSThread sleepForTimeInterval:0.75]
I had a situation where the client had to demo the launch image. So, this was my solution..
- (void)applicationDidBecomeActive:(UIApplication *)application
{
UIImageView *defaultImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Default#2x.png"]];
[self.window addSubview:defaultImageView];
sleep(2);
[defaultImageView removeFromSuperview];
[defaultImageView release];
/*
Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
*/
}
You can use it sleep method to get this result "
sleepForTimeInterval
", If you want to get it launch time, do like :
- (void) applicationDidFinishLaunching:(UIApplication*)application
{
[NSThread sleepForTimeInterval:8.0];
}
It will delay the launch by 8 seconds
Warning : But it is not recommended by apple, as it will the watchdod about long time for your app loading, It can kill your app.
But incase if you need it to get some specific screenshot or for some in-house use, you can use to solve for purpose but never in app submission.

Problem with applicationShouldTerminate on iPhone

I'm having a problem with applicationShouldTerminate.
What ever I do it seams that has no effect. Any help would be
appreciated.
I'm well versed in programing but this just gives me headache. Im going
over some basic tutorials for xcode , as I'm new to mac in general, and am currently looking at a simple flashlight app.
It exists but I would like to add a alert box here with option not to
quit.
(void)applicationWillTerminate:(UIApplication *)application
{
[application setIdleTimerDisabled:NO];
}
this has no effect, alert is closed even before its created.
(void)applicationWillTerminate:(UIApplication *)application
{
[application setIdleTimerDisabled:NO];
UIAlertView *alertTest = [[UIAlertView alloc]
initWithTitle:#"This is a Test"
message:#"This is the message contained
with a UIAlertView"
delegate:self
cancelButtonTitle:#"Button #1"
otherButtonTitles:nil];
[alertTest addButtonWithTitle:#"Button #2"];
[alertTest show];
[alertTest autorelease];
NSLog(#"Termination");
}
I did some reading online and found that it should be possible to do
this with
(NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication*)sender
But no mater where I put that declaration I get error: syntax error
before NSApplicationTerminateReply.
There is no syntax error except that xcode seems not to recognize
NSApplicationTerminateReply as valid input.
Any sample code would be greatly appreciated.
I know this is a non-answer, but hopefully I can be helpful:
Displaying a "Really quit?"-type alert like this, even if you can pull it off technically (and I'm not sure you can), is a bad idea and is likely to either cause rejection from the App Store or, at best, an inconsistent user experience because no other apps do this.
The convention with iPhone apps is to save state if necessary, then yield control (for termination) as quickly as possible when the user hits the home button or switches apps.
To ensure a consistent experience, Apple probably has an aggressive timer in place to restrict what you can do in applicationWillTerminate. And even if they don't have a technical measure in place, they probably have an App Store approval policy to ensure that applications quit immediately when they're asked to.
applicationShouldTerminate and NSApplication do not exist on the iPhone. You have to use UIApplication.
The alert view is never shown because the 'show' method does not block, and therefore, the end of 'applicationWillTerminate' is reached immediately after you create the alert view and try to show it. I believe this is by design. You can't really begin asynchronous operations in 'applicationWillTerminate'.
With regards to the applicationShouldTerminate error, in case anyone's curious, NSApplicationTerminateReply and NSApplication seem to be deprecated...even though the OP's method is exactly how it appears in the docs!
Defining your method as the below should build with no errors:
-(BOOL)applicationShouldTerminate :(UIApplication *)application
I think I found the answer to what I wanted to do but will need to check it when I get back home.
Some directions were found here
http://blog.minus-zero.org/
The iPhone 2.0 software was recently released, and with it came the
ability for users to download native apps (i.e., not web sites)
directly to their phones from within the iPhone UI or via iTunes.
Developers (anyone who pays Apple 59GBP for the privilege) can then
write their own apps and have them available for purchase in the App
Store.
One limitation of the Apple-sanctioned SDK is that only one
application is allowed to be running at a time. This presents a
problem for apps such as IM clients, music players and other programs
whose functionality relies on being able to run in the background.
Another example (courtesy of James) would be an app that takes
advantage of the iPhone 3G's GPS chip to create a log of all the
places you visit.
However, there is a neat trick that I discovered: your app will only
get terminated if you switch away from it, and hitting the iPhone's
power button while your app is in the foreground doesn't count as
switching away. The upshot of this is you can create apps which
continue to run while the iPhone is in your pocket - perfect for the
GPS example.
Achieving this is as simple as implementing two methods in your
UIApplication delegate - applicationWillResignActive: and
applicationDidBecomeActive:. Here's a simple example to demonstrate
the effect.
In your UIApplication delegate header file, add a new ivar: BOOL
activeApp. Then, in your implementation, add the following three
methods:
- (void)applicationWillResignActive:(UIApplication *)application {
NSLog(#"resigning active status...");
activeApp = NO;
[self performSelector:#selector(sayHello) withObject:nil afterDelay:1.0];
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
NSLog(#"becoming the active app...");
activeApp = YES;
}
- (void)sayHello {
NSLog(#"Hello!");
if (!activeApp)
[self performSelector:#selector(sayHello) withObject:nil afterDelay:1.0];
}