Objective-c-Keyboard input - ios

I believe ive looked at every article related to keyboard input, but still cant get it to work. ALl i want is a output, using NSLog everytime i hit a key, in the app or not. Im currently using xcode 5. Ive tried many snippets of code such as
[NSEvent addGlobalMonitorForEventsMatchingMask:NSKeyDownMask handler:^(NSEvent *event)
NSLog(#"%#",event.characters);
and im not sure where to put his code. Do i put it in the main function like this
#import <Foundation/Foundation.h>
#import <appkit/NSEvent.h>
int main(int argc, char * argV[]) {
#autoreleasepool{
[NSEvent addGlobalMonitorForEventsMatchingMask:NSKeyDownMask handler:^(NSEvent *event)
NSLog(#"%#",event.characters);
}
}
Clearly im new to objective-C, and i dont plan on continuing with it unless i can get keyboard input to work. Ive tried tutorials, snippets from keyloggers, and the mac dev forums. Thanks.

Your code is pretty close but you're getting hung up on block syntax. Blocks are very powerful and useful, but the syntax is truly awful and I still struggle with it 2 years after starting to work with them. Blocks are much less readable than C function pointers, and that's saying a lot.
It should look like this:
int main(int argc, char * argV[])
{
#autoreleasepool
{
[NSEvent addGlobalMonitorForEventsMatchingMask: NSKeyDownMask
handler: ^(NSEvent *event)
{
NSLog(#"%#",event.characters);
}
];
}
}
I put all the opening and closing braces on separate lines for clarity. A block needs to be enclosed in braces, and you were missing braces, as well as the closing bracket on your call to addGlobalMonitorForEventsMatchingMask:handler:
BTW, it's very unusual to change the main() function on a Mac or iOS program. Usually you leave that alone, and then set up an application object, set up a delegate, and put your custom code in the app delegate.
You should start of using the normal Cocoa design patterns for applications, and not try to toss them.
If you want to work with main() in C style you should think about creating a command line tool intend of an interactive application. You're going to set yourself up for failure if you don't follow the standard patterns. There's just too many ways to get things wrong.
Your main is missing the housekeeping needed to create a working iOS or Mac application

Related

Is there a way to see every time NSLog is called and the content of the NSLog?

Ideally I'd like to setup a selector to see every time NSLog is called in my app, and to get the output of that NSLog. NSLog might be called by an SDK I'm using and I'd like to be able to see the output of those logs from within my app. Is that possible?
What you want is a Symbolic breakpoint.
Navigate to the Breakpoints tab in Xcode and use the + button in the bottom left corner to add one.
When the breakpoint is added, it should automatically come up with a window that allows you to enter the conditions on which you want it to pause execution. You can use NSLog as the symbol.
As for calling a custom function of your own, you can attempt to do that by adding an action in that breakpoint and utilise the debugger's ability to call into your own code by passing in the arguments that are available to the debugger e.g. $arg1.
So I dont think it is possible to simply replace every instance of NSLog with something else... but you could do something like this:
#define NSLog MyLog
void MyLog(NSString *format, ...) NS_FORMAT_FUNCTION(1,2);
void MyLog(NSString *format, ...)
{
va_list list;
va_start(list,format);
NSLogv(format,list);
va_end(list);
va_start(list,format);
vfprintf(stderr,[format UTF8String], list);
va_end(list);
}
int main(int argc, const char * argv[]) {
NSLog(#"hello %s\n","world");
return 0;
}
My example uses 2 va_lists to log with an NSLog like NSLogv and vfprintf to log to an open FILE * stream (stderr in this case)... you could also probably more simply format an NSString (as vfprintf can't print NS objects)...
I tried a little bit to try to re-assign NSLog with dlsym which would be process wide, rather than just when the macro is expanded... I couldn't get it to work but I am not a dynamic linker expert.

Access to iOS SDK constant via name (reflection)

Inside of iOS SDK, lots of constants defined by Apple can be found looking something like this:
extern const CFStringRef kSomeReallyNiceConstant
__OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_8_0);
If I check for presence of this constant standard way:
if (NULL == &kSomeReallyNiceConstant)
I am pretty much referencing it and in order for my code to compile properly, I need at least iOS SDK 8.0 or higher in this case.
When it comes to objects and methods, reflection approach works nicely with usage of NSClassFromString, respondsToSelector and performSelector.
Is there a chance to use some kind of reflection (access to string constant by name) in attempt to get it's value if it exists (or none if it doesn't)?
I know that I can use macros to check for iOS version and execute different code paths based on that information, but I don't want to use that approach.
I managed to do this with pointer:
#include <dlfcn.h>
// ...
int *pointer = dlsym(RTLD_SELF, "kSomeReallyNiceConstant");
if (pointer) {
NSLog(#"Thing exists!");
} else {
NSLog(#"Nope, doesn't exist!");
}
but I am not sure if this is something that would cause app rejection. Do you maybe know?
Regardless of this pointer approach, I'm curious to hear if there's any other way to achieve this?
Nothing better than suggested solution found on this topic.
#include <dlfcn.h>
// ...
int *pointer = dlsym(RTLD_SELF, "kSomeReallyNiceConstant");
if (pointer) {
NSLog(#"Thing exists!");
} else {
NSLog(#"Nope, doesn't exist!");
}

console I/O in iOS

I just started doing some competitive programmning in objective C. There's a problem which I'm facing, most sites start with main.m
int main(int argc, char * argv[]) {
#autoreleasepool {
}
}
I somehow learned how to get input and use it. But today I tried a differnt work where
void main()
{
}
when I tried scanf("%s",str) though I could read the first line of input. But I don't know how to start and how to get rest of lines.
I want to lean more about console input/output in iOS. Any help will be much appreciated?
What you need is a bit of a tutorial
If you right click any data structure for instance there'l be a first option "print description" another option with your code : NSLog(name of variable); for e.g for an array : NSLog(#"%#",myArray);
Lastly,Typing po myarray in the right half of the console (under lldb) would print out myArray's contents
Hope this helps! Cheers

XCode 6 verificationController.m issues

I am using VerificationController.m provided by Raywenderlich for validating receipts for in ap purchase. It is working fine for XCode5 but in XCode6 it is giving number of errors. probably due to C++ code like:
Missing Code for Method declaration
#end must appear in objective-c
context Conflicting types for 'checkReiptSecurity'
can anyone tell me what is needed to be done ?
Edit : Here are errors screenshot
Have you fixed this? I was running in to the exact same problem so I'll leave my fix here for anyone that comes looking. It turns out in newer versions of Xcode you aren't allowed to put C/C++ code in objective-C context anymore. So I moved the declarations for unsigned int iTS_intermediate_der_len, unsigned char iTS_intermediate_der[], char* base64_encode(const void* buf, size_t size), and void * base64_decode(const char* s, size_t * data_len) to the top of the file, above the #implementation tag.
Have you downloaded sample code? I have downloaded sample code and its working fine at my side. It seems that you have missed or added an extra braket } or { in your code.
May be this happened when you was trying to comment this code [UIDevice currentDevice].uniqueIdentifier; because originally this line produce an error.

Simple method call in Xcode 5 and passed value won't change

I'm a noob to Xcode and am reading the Big Nerd Ranch book and its asking me to do example programs in simple C to get me familiar with the language, however its asked me to create a program that calls a method from main and passes an int and does a square calculation and the printf to defog screen. Here is the program:-
#include <stdio.h>
void doTheMath(int numberToSquare)
{
int numberSquared = numberToSquare * numberToSquare;
printf("%d squared is %d\n",numberToSquare,numberSquared);
}
int main(int argc, const char * argv[])
{
doTheMath(5);
return 0;
}
As you can see I am passing the value 5 to the method and it prints 25 on screen when i run the code. IF I then change 5 to 15 to get it to write out a different value, it doesn't. It still writes out 5 squared, not 25 squared.
In debug and step through the value is wrong and isn't changed.
I've closed the project and Xcode and still it doesn't work all of the time and then sometimes it does reflect the changed value.
The project Type is an OSX application / command-line tool. The project is stored on my NAS.
Your code is correct and when I test it in Xcode and change the 5 to 15 and run it, I get the correct answer returned. Make sure you save your file after you make the change, and you could also try to clean your project (Product > Clean).
Sometimes Xcode just does strange things . . .

Resources