how to detect idle user in iphone-sdk - iphone

In my Application I want to call Logout Function if user is idle for certain amount of time how to accomplish
this answer doesn't work for me
iPhone: Detecting user inactivity/idle time since last screen touch
if i subclass my app delegate class from UIApplication and implement
- (void)sendEvent:(UIEvent *)event
It gives me error
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'There can only be one UIApplication instance.'
I can't find the other instance of UIApplication in my application
so far i have done
instead of
#interface IdleAppDelegate : NSObject <UIApplicationDelegate> {
I have changed it to
#interface IdleAppDelegate : UIApplication<UIApplicationDelegate> {
and in the main instead of
int retVal = UIApplicationMain(argc, argv, nil, nil);
I've changed it to
int retVal = UIApplicationMain(argc, argv, #"IdleAppDelegate", #"IdleAppDelegate");
Is there anything remaining to do?
I'm getting the above error... am I missing something...?
Please Help
Thanks

Your application class is also an application delegate class - that's bad. UIApplicationMain() will create an instance of your custom application subclass, which will then try to an instance of its delegate - which is also an instance of your custom application subclass. You should separate these concerns - yes your custom app subclass needs to subclass UIApplication, but your app delegate should be a separate class that subclasses NSObject.

Try this out
-(void)applicationWillResignActive:(UIApplication *)application
{
NSLog(#"Application not Active");
// FETCH THE CURRENT TIME
}

Related

block delegate methods of class in iphone

I am having a problem I am working on a class which is subclass of UITextField.
Which will be used in many classes further.
But I don't want to let user to use it's delegate methods in any way.
Is there any way to do this ?
Override setDelegate: so that it throws an exception or logs an instruction on what to do. That way your API users will know what's actually going on.
-(void) setDelegate: (id <UITextFieldDelegate>) delegate
{
NSLog(#"*** Use the blocks API instead of calling %s", __PRETTY_FUNCTION__);
[self doesNotRecognizeSelector: _cmd];
}
Override the -setDelegate: method such that it never actually sets a delegate. You can just provide an empty method that fails to call super:
-(void) setDelegate:(id<UITextFieldDelegate>) delegate
{
// this method intentionally empty to prevent a delegate from ever being set
}

Simple functions giving me a crash on Objective-C

I'm coding a simple calculator and I want to make an unique function for all 4 arithmetic buttons. I got here:
-(IBAction)simbolo1:(UIButton*)sender{
if (isNumeric(campo1.text)) {
NSString *str=campo1.text;
campo2.text=str;
int S=1;
NSString *cmp1 = [NSString stringWithFormat:#"%d", S];
sinal.text=cmp1;
campo1.text=#"";
} else {
campo1.text=#"Only numeric values";
}
}
But, for some reason, I cant get this to work. Everytime I click in the button, I get a crash.
So, I check where really was the error and I deleted the whole code:
-(void)simbolo1:(UIButton*)sender{
campo1.text=#"Only numeric values";
}
Those lines of code appear to gave me the same error as before
I'm getting a 'green :marker' on this file:
#import <UIKit/UIKit.h>
int main(int argc, char *argv[]){
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, nil); <--------- This line right here
[pool release];
return retVal;
}
EDIT---------------
On the debud console im getting this:
2011-09-30 09:17:14.319 Teste iPhone[28432:fb03] Applications are expected to have a root view controller at the end of application launch
2011-09-30 09:17:21.714 Teste iPhone[28432:fb03] -[Teste_iPhoneAppDelegate simbolo1]: unrecognized selector sent to instance 0x6b7c2d0
2011-09-30 09:17:21.715 Teste iPhone[28432:fb03] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Teste_iPhoneAppDelegate simbolo1]: unrecognized selector sent to instance 0x6b7c2d0'
*** First throw call stack:
(0x166d062 0x17fed0a 0x166ecfd 0x15d3f10 0x15d3cf2 0x166eed9 0x16eb2 0x16e4a 0xbc3e6 0xbc8af 0xbbb6e 0x3c2a0 0x3c4c6 0x22c74 0x16399 0x1557fa9 0x16411d5 0x15a6042 0x15a492a 0x15a3dd4 0x15a3ceb 0x1556879 0x155693e 0x1438b 0x272d 0x26a5)
terminate called throwing an exception(gdb)
There is not enough information to help you. You need to look in the debugger console and show us the error and the stack.
However, I'm going to take a wild guess and say that you didn't connect the campo1 IBOutlet to its UIView in Interface Builder, and that it's nil. To fix, edit the view in Interface Builder (or in Xcode 4, just click on it), and drag the little circle next to the campo1 outlet (in the Connections tab) onto the UIView component that you want it to correspond to.
If that's not it, the error isn't here, but is probably caused by something else in your program.
[EDIT after seeing console]: Your connections look misconfigured. Why would it try to send the simbolo1 selector to Teste_iPhoneAppDelegate? It should send it to your ViewController. Did you play around with the connections (specifically, the delegate one) in Interface Builder -- or the class?
to set text in UITextField use
[campo1 setText:#"Only numeric values"];
and also don't forget to link IBOutlet UITextField* campo1; to proper UITextField in XCode IB

Strange memory address resulting in BAD_ACCESS

I'm trying to wrap my head around a strange problem in my iPad app. It's a pretty simple app, only one root view controller with a little board game inside.
Sometimes, seemingly at random, the app freezes and I get a BAD_ACESS on a delegate reference I use in one of my classes. I've been solving BAD_ACCESS-problems for a long time, but this is very strange. The object the delegate is referring to is the root view controller, and that should never b released. I put a log line in the -(void)dealloc method and that never occurs. I even tried to over retain the object but it still disappears.
Even if I run the app in the profiler with NSZombie detection on, the profiler just stops when this happens. Doesn't show any results whatsoever.
Another strange thing I noticed was the memory address. If I log it like NSLog(#"%p", delegate); i get "0x1" as a result. A nil pointer is 0x0 so testing for if(delegate) does return true even though the object has vanished. And even if the object itself was deallocated, the memory address would still be intact?
The problem only occurs after some use, between like 15 and 45 sec.
Doed someone know how to tackle this problem? I'd be greatly thankful.
This is how the delegate is assigned. The _delegate is the root view controller which is always active.
-(id)initWithDelegate:(NSObject <TheGameDelegate>*)_delegate level:(int)_level;
{
self = [super init];
if(self != nil)
{
delegate = [_delegate retain];
...
Here is where it crashes:
-(void)countdown:(NSTimer*)timer;
{
time -= 1;
if(delegate) // this is always true
{
NSLog(#"%p", delegate); // this prints a normal memory address until right before crash when it prints "0x1"
[delegate theGameTick:self]; // accessing deleagte gives BAD_ACCESS
}
...
Thanks in advance!
imo, your delegate implementation is weird.Retaining delegates is not the good idea. Try adding to the header:
#property (nonatomic, assign) id delegate and then the method would look like
-(id)initWithDelegate:(id)_delegate level:(int)_level;
{
self = [super init];
if(self != nil)
{
self.delegate = _delegate;
...}
as well as in further methods you reference to self.delegate.(you can add the required protocol as well, i skipped it for clearer code)

objective c : hand over delegate to other class

I am here confronted with a certain problem. I have a protocol which states a method, that returns the datasource for my tableviews. The datasources are generated by one class, for 3 tableviews. If you tap on one cell, you get to the next tableview with a different source and so on (I think you get the point).
Everything works fine for the first tableview, but as I hand over the deletage to the next tableview I still do not get the datasource for the second. Do I have to release the delegate at a certain point? And if I have to, how do I get it back, when the navigationbarbuttonitem is tapped on...?
Tell me if you have any ideas.
EDIT:
if ([Where isEqualToString:#"System"])
{
if ([exchangeDelegate respondsToSelector:#selector(getNewDataSourceForSystem:)])
{
[exchangeDelegate getNewDataSourceForSystem: [controlDelegate setBranchNavigation:What]];
}
}
else if ([Where isEqualToString:#"User"])
{
if ([exchangeDelegate respondsToSelector:#selector(getNewDataSourceForUser:)])
{
[exchangeDelegate getNewDataSourceForUser: [controlDelegate setLeafNavigation:What]];
}
}
if ([exchangeDelegate respondsToSelector:#selector(getNewDataSourceForCostumer:)])
{
[exchangeDelegate getNewDataSourceForCostumer: [controlDelegate setRootNavigation]];
}
each respondToSelector goes to a different class.
EDIT:
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Costumers getNewDataSourceForSystem:]: unrecognized selector sent to instance 0x8a3b0e0'
Thats what I get as an exception when I leave out respondsToSelector:#selector.
Have you called -[UITableView reloadData] to inform it about invalidation of its current state?
And have you overloaded you delegate setter method in order to also fetch and set the new datasource and delegate as needed? Probably something like this:
-(void)setDelegate:(id<MYDelagate>)delegate;
{
myTableView.dataSource = [delegate tableViewDataSource];
myTableView.delegate = [delegate tableViewDelegate];
_delegate = delegate;
}
So, as it seems, using a singleton is the proper way to store my data for my views:
I created a singleton.
The singleton holds my 3 different data-arrays.
The delegate sends the new arrays to the singleton.
I am fetching the data from the views via the singleton.
As simple as it is ... thank you guys for your inspiration :-)

Issues with Singleton in objective C

I am trying to implement a singleton, it is acting as a stub DAO and I need different areas of the application to be able to read and write to it. The first class that uses it can do so without any issue using my sharedSingleton class level constructor, however when I attempt to access this from another class in the exact same way I get a EXC_BAD_ACCESS error and the debug line in the 1st line of the method I am calling on the singleton is never hit.
+(DAOController *) sharedSingleton
{
static DAOController *sharedSingleton;
#synchronized(self)
{
if (!sharedSingleton)
sharedSingleton = [[DAOController alloc] init];
return sharedSingleton;
}
}
-(id) init
{
if (self = [super init])
{
[self initDictionary];
}
return self;
}
I make the exact same call twice both in viewDidLoad
DAOController *daoController = [DAOController sharedSingleton];
self.teams = [daoController getTeamsForPlayer];
But in the 2nd it throws an exception or a EXC_BAD_ACCESS
2011-04-28 18:31:22.403 IScore[5637:207] -[NSKeyValueIvarSetter getTeamsForPlayer]: unrecognized selector sent to instance 0xa707220
2011-04-28 18:31:22.435 IScore[5637:207] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSKeyValueIvarSetter getTeamsForPlayer]: unrecognized selector sent to instance 0xa707220'
Call stack at first throw:
The method simply does
-(NSMutableArray*) getTeamsForPlayer
{
NSMutableArray *teamsForPlayer = [[[NSMutableArray alloc] init] autorelease];
Team *team1 = [self.teams objectForKey:[NSNumber numberWithInt:1]];
[teamsForPlayer addObject:team1];
[team1 release];
return teamsForPlayer;
}
If I change the 2nd instance to non shared I can run the method without issue
DAOController *daoController = [[DAOController alloc]init];
Any assistance would be appreciated. Singleton pattern was taken from last entry on What should my Objective-C singleton look like?
Looks like your singleton has been deallocated and that another instance took its address.
You should check your code to find how this is possible. (you should never retain / release that singleton)
That's why I strongly suggest using Matt Gallagher's cocoawithlove singleton macro that you can download there, which is super easy and concise to use :
SYNTHESIZE_SINGLETON_FOR_CLASS(MyClassName);
It is a perfect singleton implementation, that avoid such issues by deallocating accidently your singleton, which looks like to be your problem. It is based on Apple recommandations, which overrides release, retainCount and such to protect your singleton.