How to read key codes of USB Keyboard with Media Keys - iphone

Is it possible to read key codes of non-standard keys using an special iOS API or via the generic TextInput field? I have a multimedia keyboard with special buttons & like to make my iPad app aware of these key codes.
I know iOS can use some of them already (i.e. volume up/down, next/prev track).

You might want to try subclassing UIApplication and overriding sendEvent: to see what events pass through there. Given that UIEvent doesn't document keyboard event support on iOS (as opposed to NSEvent in AppKit), I'm skeptical that you'll see keyboard events... but it's worth a try. I don't have a BT keyboard to try it out myself.
To use a UIApplication subclass, edit main.m and pass your subclass as the third argument to UIApplicationMain:
int main(int argc, char *argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, YourApplicationSubclass, nil);
[pool release];
return retVal;
}
This strategy isn't uncommon in desktop Cocoa apps and is also explained here:
http://cocoawithlove.com/2009/05/intercepting-status-bar-touches-on.html

Related

Create calendar event from inside my application

I know that there already are lots of questions and even useful answers concerning this question on the web. I tried to add a calendar event to the iPhone calendar from inside my application. I used this code, which actually worked:
EKEventStore *es = [[EKEventStore alloc] init];
EKEventEditViewController *controller = [[EKEventEditViewController alloc] init];
controller.eventStore = es;
controller.editViewDelegate = self;
[self presentModalViewController:controller animated:YES];
The only thing was that I could not release the calendar controller, which is because I should have said:
[Controller release]
or something
But my main.m is set to autorelease:
int main(int argc, char *argv[])
{
#autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([...AppDelegate class]));
}
}
and if I manually release I get an error, do I have to change something in the main.m?
In your target's build settings, if you see Objective-C Automatic Reference Counting then you are using ARC:
And if you are using ARC then you are not responsible to release object by your self.
I strongly recommend to read more about ARC, you can start from here, this is the most important thing you should consider if you want to build a real application.
As I understand from the comments, it might be you are using ARC. In order to check that, go on your project tab, select Build Settings and type in the search bar
Automatic Reference Counting
If it's set to you YES, you don't need to release the object.
EDIT
It looks like there have been a misunderstanding regarding the word release. Release as you mentioned it (calling release on an object) means to decrease the object reference counter.
Dismissing a modal view controller is a complete different thing. In order to do that,
on the cancel button delegate method, you have to invoke:
[yourViewControllerInstance dismissModalViewControllerAnimated:YES];
That's the method you are searching for.

Sometimes my project works fine, sometimes it shows following error in the line mentioned in the code below

#import <UIKit/UIKit.h>
int main(int argc, char *argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, nil);//Program received signal: EXC_BAD_Access"
[pool release];
return retVal;
}
You’re accessing bad memory somewhere. Most probably, you're trying to reference a pointer to an object that's been released already, and the debugger has had to roll back to the bottom of the stack. Usually, a hint to the real reason for a crash like this will appear just slightly before "Program received signal: EXC_BAD_Access" in the log.
Good luck.
EXC_BAD_Access means you're overreleasing an object. Run the app using Instruments Object Alloc with Zombie detection enabled to find the culprit.
Your application going to crash that's why you are getting error at this point,
you can check where application going to crash by selecting iOs Simulator 4.3 and lower version.
This may help you to debug the problem.
iOS 5.0 and later never tell where is the issue for that you must have to run you app in lower version then 5.0.

iPhone SDK "clear" function not working-button

I have a question relating to a button I made for my app. I am fairly new to iPhone development so please stay with me. When the "clear" button is clicked, it is programmed to reset the text fields. The other "calculate" function works fine. The app runs perfectly until I press "clear". Then the app will completely freeze. The following is displayed in Xcode.The commented out is the error I get, I think it is some sort of breaker:
int main(int argc, char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, nil);
//Above error reads "Thread 1: Program received signal: "SIGABRT"
[pool release];
return retVal;
}
I feel like this is a simple mistake but I am unfamiliar with mobile development and just started it. Any help would be greatly appreciated!!
When this occurs, it usually means Xcode couldn't figure out where something went wrong. The best thing to do is to set a breakpoint in the first line of your -clearButtonWasPushed: method (or equivalent). Run it step by step, and then when it crashes to your main.m code, you'll know which line caused it. You might have overreleased something by mistake...
Alternatively, UITextField has a built in clear button, which you can activate programatically or in IB:
textField.clearButtonMode = UITextFieldViewModeWhileEditing;

Memory leaks in an iPhone app

I have the following memory leaks in my code. What does it mean? How can I fix this?
#import <UIKit/UIKit.h>
int main(int argc, char *argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
**int retVal = UIApplicationMain(argc, argv, nil, nil);**
[pool release];
return retVal;
}
Thanks in Advance.
That code is from your main.m file. It seems odd that this part of your code would leak, if at all??
How did you find this leak?
Are you using the simulator or a real device?
If using the simulator you can sometimes have leaks that are not leaks at all, it is always better to test these kinds of things on a real device (which you have not specified). Double check all of your release, retains etc in your code. You might just spot something you have not released. (in xcode 4 use the assistant editor I find it better to spot these kinds of things alt+cmd+enter).
Your question otherwise is hard to answer, you might want to edit it with how you found it and in what environment.
Hope some of this helps
[EDIT] saw you tagged this with cocos2D (what version are you using of that?) there are some reported issues elsewhere on SO with memory leaks using older versions of cocos2D

iPhone app using different language than the one set in OS / device?

Is it possible to have my app running in a different language than the one that is set in the OS? I want to have a language switch in my app’s setting menu, where the user can e.g. select german for the app, while his system is running in english.
From what I’ve already read, it seems it’s not possible without having my own localization mechanism…
What do you think?
it can be done easily although it took me weeks to work out how ;-)
I have an iPhone app called iDEX which is a Romanian dictionary. On the iPad, Romanian is not available as a UI language, so I wanted to give the user the option of selecting Romanian in the application settings, as the device settings do not include it.
What you have to do is internationalize the app and localize it as usual. Then, in the main method, set the AppleLanguages key of the standard NSUserDefaults to the language and country of your choice, like so
int main(int argc, char *argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[NSArray arrayWithObject:#"ro_RO"] forKey:#"AppleLanguages"];
int retVal = UIApplicationMain(argc, argv, nil, nil);
[pool drain];
return retVal;
}
It is imperative that you set the AppleLanguages key before you run UIApplicationMain(), because it is at that point that the UIKit framework is loaded and the default language determined.
It also important that you specify both language and country (eg, #"ro_RO" and not just #"ro") otherwise your app might crash when setting the key.
By doing this, all calls to NSBundle that support localization will look for the language that you have programatically established, as opposed to the one set on the device.
Hope that helps...
You need to implement your custom resource loading mechanism to achieve that. For example you will no longer be able to use NSLocalizedString macro or [NSBundle pathForResource:]. You will always have to specify paths including the localization (as in de.lproj/MyStrings.strings instead of just MyStrings.strings). I would suggest against doing this unless absolutely necessary.