Implement auto log-off feature in Iphone - iphone

I am making an application which logs in using some username and password. Now when i am logged in succesfully I want my application to be logged out automatically if no interaction with the application found for 10-12 minutes.
Can anybody guide me how can i achieve this ??
Help with some code will be really appreciated.
Thanks

The Thing you want to implement is called SESSION MANAGEMENT.You have to subclass the UIApplication.
#interface MyUIApp : UIApplication {
}
In this class you have to reset the timer each time. Also you have to check if the application is responded or not with this.If the idleTimer is Exceeded then push the viewController to your login view.
- (UIResponder *)nextResponder {
[self resetIdleTimer];
return [super nextResponder];
}
Also you have to change the main class file with this:-
#import <UIKit/UIKit.h>
int main(int argc, char *argv[]) {
NSString* appClass = #"MyUIApp";
NSString* delegateClass = nil;
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, appClass, delegateClass);
[pool release];
return retVal;
}

Related

UIApplication delegate method is not called

I have a problem integrating Facebook SDK 3.2 with my app, my app goes to facebook app for approval and login, but when
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation;
needs to be called when facebook authorization is done, it simply skips it.
It works great when I force webView facebook login via modal viewcontroller, but when I want to use installed facebook app/safari it just doesn't work.
Any suggestions?
Thank you.
Make sure you put UIApplicationDelegate in interface declaration.
#interface AppDelegate : NSObject <UIApplicationDelegate>
Also verify its right App delegate class
int main(int argc, char *argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, #"AppDelegate");
[pool release];
return retVal;
}
Also add fb Id in plist.

EXC_BAD_ACCESS when opening modal view

I'm having a problem with my iPhone App, I'm getting EXC_BAD_ACCESS, I had some memory leaks, but these are now fixed, so I'm not sure whats going on. I realise that I haven't provide a lot of information, but I really don't know whats happening.
The initial screen opens up where I have a number of buttons. Tapping on the first button, which runs the following code and opens up a modal view:
-(IBAction)newWorkoutButton
{
newWorkoutViewController .loadedFromRootViewController = #"YES";
[self presentModalViewController:newWorkoutViewController animated:YES];
}
The screen freezes and the is in the code below:
#import <UIKit/UIKit.h>
#import <objc/runtime.h>
#import <CoreLocation/CoreLocation.h>
int main(int argc, char *argv[])
{
Method getDistanceFrom = class_getInstanceMethod([CLLocation class], #selector(getDistanceFrom:));
class_addMethod([CLLocation class], #selector(distanceFromLocation:), method_getImplementation(getDistanceFrom), method_getTypeEncoding(getDistanceFrom));
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, nil); // ERROR HAPPENING HERE
[pool release];
return retVal;
}
Like Aleks suggested you can try to find the zombie like this:
I find this alternative more convenient:
Click the "Run Button Dropdown"
From the list choose Profile
The program "Instruments" should open where you can also choose Zombies
Now you can interact with your app and try to cause the error
As soon as the error happens you should get a hint on when your object was released and therefore deallocated.
(source: dimzzy.com)

Problems in TTTableview class using Three20 in iPhone?

Now I am using Three20 in my apps. I have a problem, when I run the application with MainWindow.xib, the TTTableview class isn't called. But without MainWindow.xib it would call the TTTableview class. I don't know why it's happening. Please help me out.
Thanks.
In your AppDelegate code
delete the mainWindow.xib
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary
*)launchOptions {
if (![navigator restoreViewControllers]) {
[navigator openURLAction:[TTURLAction actionWithURLPath:kAppRootURLPath]];
}
}
In main.m
#import <UIKit/UIKit.h>
int main(int argc, char *argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, #"YOURAppDelegate");
[pool release];
return retVal;
}

When is the autorelease pool triggered

i have used autorelease throughout my app and would like to understand the behavior of the autorelease method. When is the default autorelease pool drained? is it based on a timer (every 30sec?) or have to be called manually? do I need to do anything to release variables that are flagged with autorelease?
There are (I'd say) 3 main instances when they are created and release:
1.Beginning and very end of your application life-cycle, written in main.m
int main(int argc, char *argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, nil);
[pool release];
return retVal;
}
2.Beginning and very end of each event (Done in the AppKit)
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
- (void)loadView
/* etc etc initialization stuff... */
[pool release];
3.Whenever you want (you can create your own pool and release it. [from apples memory management document])
– (id)findMatchingObject:anObject {
id match = nil;
while (match == nil) {
NSAutoreleasePool *subPool = [[NSAutoreleasePool alloc] init];
/* Do a search that creates a lot of temporary objects. */
match = [self expensiveSearchForObject:anObject];
if (match != nil) {
[match retain]; /* Keep match around. */
}
[subPool release];
}
return [match autorelease]; /* Let match go and return it. */
}
It is drained whenever the current run-loop finishes. That is when your method and the method calling your method and the method calling that method and so on is all done.
From the documentation:
The Application Kit creates an autorelease pool on the main thread at the beginning of every cycle of the event loop, and drains it at the end, thereby releasing any autoreleased objects generated while processing an event

iPhone - Preventing Sleep while using applicationMusicPlayer in MPMusicPlayerController

I am trying to disable the automated sleeping of iPhone for certain period in my app.
Used [[UIApplication sharedApplication] setIdleTimerDisabled:YES] which works fine as long as I play no music.
But when I play music the Idle Timer seems to get reactivated.
I have tried all kinds of tricks from NSTimer firing silent sounds every 10 second etc but nothing works.
Would welcome any suggestion or thoughts on making this happen.
The way to fix this is to first subclass UIApplication, override the setIdleTimerDisabled method and make it do nothing. Then, add a couple of your own methods that you'll call from your application instead of using the normal setter. By doing this you will ignore all messages that might change the idle timer aside from the custom method calls you make yourself. Here's how you do it:
Edit your main.m file to use a custom UIApplication subclass:
#import <UIKit/UIKit.h>
int main(int argc, char *argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSString* appClass = #"CustomUIApplicationSubclass";
NSString* delegateClass = nil;
int retVal = UIApplicationMain(argc, argv, appClass, delegateClass);
[pool release];
return retVal;
}
Then define your UIApplication subclass:
#interface CustomUIApplicationSubclass : UIApplication {
}
- (void)disableIdleTimer;
- (void)enableIdleTimer;
#end
#implementation CustomUIApplicationSubclass
- (void)setIdleTimerDisabled:(BOOL)disabled
{
// do nothing! take that stupid ipod controller!
}
- (void)enableIdleTimer
{
[super setIdleTimerDisabled:NO];
}
- (void)disableIdleTimer
{
[super setIdleTimerDisabled:YES];
}
#end
This will force the iPod controller to use your custom UIApplication instance which does no longer does anything when the normal setIdleTimerDisabled method is called.