How to use MKLocationManager (a Private API) in iOS - iphone

I need to call
[[MKLocationManager sharedLocationManager] _applyChinaLocationShift:newLocation]
in my iOS app.
I believe MKLocationManager is a private class, and there does not seem to have a MapKit/MKLocationManager.h file.
I'm not targeting App Store. It's there any way I can use that private API?
Update at 2011-6-23
I really need the answer, or could I de-complie the iOS SDK?
100 reputation is almost all I have. Please help me.

If the above answer isn't working for you, this may be because the entire class is private (including it's header). Here's an alternative approach using some runtime trickery; you must be sure that the signature is correct but we can use some defensive coding to avoid a crash.
First, unless you are calling this just once, I'd wrap up the code in a helper method:
// in some header file, you may want to give the method a prefix too
CLLocation *ApplyLocationManagerChinaLocationShift(CLLocation *newLocation);
You can now use NSClassFromString to obtain a reference to the class and performSelector to perform the method. We can try and make sure the method exists first to be on the safe side:
CLLocation *ApplyLocationManagerChinaLocationShift(CLLocation *newLocation)
{
id sharedLocationManager = [NSClassFromString(#"MKLocationManager") performSelector:#selector(sharedLocationManager)];
SEL theSelector = #selector(_applyChinaLocationShift:);
// this will ensure sharedLocationManager is non-nil and responds appropriately
if (![sharedLocationManager respondsToSelector:theSelector]) {
return nil; // fail silently - check this in the caller
}
return [sharedLocationManager performSelector:theSelector withObject:newLocation];
}
I haven't run the above code but it should do the trick. If for some reason the #selector() calls do not work (I think they should), then you can replace them with NSSelectorFromString() calls instead.

You can simply create the method description yourself, essentially creating your own category on MKLocationManager. By defining how the private method looks you make it callable. But you must be certain about it's signature, because if you are off then your app will just crash.
This category could be put in it's own .h file or if you only use it in one place right above the #implementation.
#interface MKLocationManager (china)
- (CLLocation *)_applyChinaLocationShift:(CLLocation *)newLocation;
#end

Related

vs [mpk5 weaponAttachments]

I'm able to make the method for the call [self weaponAttachments:mpk5] but I don't like having to call self. I think [mpk5 weaponAttachments] is more natural and is easier to read.
The problem I'm having is I need to pass in the weapon (mpk5) in order to use it, which I can do with the first method but not with the second one. Does this mean that I need to subclass NSDictionary in order to be able to use a statement like [mpk5 weaponAttachments]? If so, how do I get ahold of the caller "mpk5" so that I can use it inside the method?
EDIT
I apologize for not putting this in the first time but my objective is to have [mpk5 weaponAttachments] return an NSDictionary or NSArray. Right now I have NSDictionary *attachments = [self weaponAttachments:mpk5]; which works but it just doesn't seem like the best approach.
So firstly, your two calls are a little mixed up:
[self weaponAttachments:mpk5] calls the weaponAttachments method, passing in the variable mpk5.
But [mpk5 weaponAttachments] is either asking the mpk5 object to return the weaponAttachments property or is asking the mpk5 object to run a method called weaponAttachments (I'm simplifying here - it's always a method, but if you're using properties you probably won't realise this as Objective-C will create them for you).
These are fundamentally different things.
On to the brunt of your question:
I don't like having to call self
...unfortunately, if you're working in an object-oriented language you're going to have to get used to this. Say I have a class called mySimpleClass, and a method inside that class called doSomething. Writing this:
[mySimpleClass doSomething] would be what we call a static method. Whereas calling [self doSomething] from within an instance of mySimpleClass would be an instance method.
If you're unsure of the difference between static and instance methods you should probably step back and take a look at some of the basic guides out there.

Aspect-Oriented Objective-C Library?

Is there any Aspect-Oriented Objective-C library that I could perhaps use for iPhone development?
There is an old project called AspectCocoa, this might be what you are searching for.
Otherwise Í would suggest rolling your own. Either proxy based AOP by subclassing NSProxy for a change. Or you could do some method swizzling with the quite cool Obj-C run-time function method_exchangeImplementations().
But unless you are looking for a fun exercise, ask yourself what you want to achieve, and if there is an existing perfectly working Objective-C way to do it.
Check out my article about a possible solution:
http://codeshaker.blogspot.com/2012/01/aop-delivered.html
The base idea is to make a hook into the message sending mechanism and force it to the message forwarding route:
So A brief explanation about how it works:
At registration of a method call of a specific class it creates a method wrapper (AOPMethod) object and stores every information in it
about that specific method along with the block that will be used upon
interception.
Changes the implementation of the method to _objc_msgForward or _objc_msgForward_stret respectively using method_setImplementation. This is the point where we route message sending to the forwarding
mechanism. The next time the message is called on the base class, it
will return the _objc_msgForward implementation as if it not found the
implementation. So it starts to resolve it by going through the
message forwarding steps. Nice.
We add the forwardingTargetForSelector: method to the base class using class_addMethod to point to our implementation in the AOPAspect
class. Also we add the original method implementation and selector
(with an extended name to prevent conflicts between classes) to our
AOPAspect instance.
In the forwardingTargetForSelector: method we give back our AOPAspect instance. With this we route the message forwarding from the
base object to our AOPAspect object.
This forwardingTargetForSelector: method will be called again on AOPAspect as we don't have that selector implemented. This case we
return nil, so message forwarding steps further and will check for the
methodSignatureForSelector: and forwardInvocation: methods on
AOPAspect.
In methodSignatureForSelector: we gave back the correct message signature that is already stored in a dictionary in a method wrapper
object.
At the time it arrives to our implementation of forwardInvocation: in AOPAspect we have a fully configured NSInvocation instance and the
only thing we have to do is to change the selector to the extended
version we added to AOPAspect class. Here we can run the blocks
registered for the given method before/after or even instead of the
method call. And of course we can run the original method by calling
[anInvocation invoke].
For simplicity, we just pass the NSInvocation object to the blocks registered for the method, so they can access all arguments and the
return value as well through the getArgument:atIndex: and
getReturnValue: methods.
And that's it. It works with all kind of return types, argument types
and any variation of arguments.
You can find the concrete example on the above link. Please feel free to use it.
The question is old but I discovered this project today and it might be helpful to someone in the future.
https://github.com/steipete/Aspects
Also you might want to check out the library at https://github.com/moszi/AOP-in-Objective-C which is a very simple NSProxy subclass allowing you to intercept the beginning and the end of the method calls.
With this you can even create a proxy class for you objects to make sure messages sent to your object are serialized over one single thread, regardless of the invoking thread.
All still interested people should take a look at https://github.com/mgebele/MGAOP
This seems to be a new project with future potential.
Check out this one https://github.com/pvantrepote/FlexOC
It's an alpha version and uses (for now) the Proxy implementation. It does also dependency injections.
Another one is Aspect Objective-C: https://github.com/tomdalling/AspectObjectiveC
With Objective-C i would suggest to go with the here much used Category- and Delegate-Pattern. These can be more useful than AOP.
Don't try and solve your problems with solutions you learned for other languages.
I made some rudimentary aop pre and post process function on an NSObject category
#implementation NSObject (AOP)
- (void)preprocess:(SEL)sel with:(void (^)(id obj, id param))implementingBlock{
Method m1 = class_getInstanceMethod(self.class, sel);
IMP imp1 = method_getImplementation(m1);
SEL replacement = sel_registerName( [[[NSString stringWithUTF8String:sel_getName(sel)] stringByAppendingString:#"pre"] cStringUsingEncoding:NSUTF8StringEncoding]);
class_addMethod(self.class,replacement, imp1, nil);
method_setImplementation(m1, imp_implementationWithBlock(^(id x, id param){
implementingBlock(x,param);
[x performSelector:replacement withObject:param];
}));
}
- (void)postprocess:(SEL)sel with:(void (^)(id obj, id param))implementingBlock{
Method m1 = class_getInstanceMethod(self.class, sel);
IMP imp1 = method_getImplementation(m1);
SEL replacement = sel_registerName( [[[NSString stringWithUTF8String:sel_getName(sel)] stringByAppendingString:#"post"] cStringUsingEncoding:NSUTF8StringEncoding]);
class_addMethod(self.class,replacement, imp1, nil);
method_setImplementation(m1, imp_implementationWithBlock(^(id x, id param){
[x performSelector:replacement withObject:param];
implementingBlock(x,param);
}));
}
#end
I'm working on a real (it is more than method-swizzling) AOP-Framework for Objective-C. An alpha will be released soon. You can listen to my german presentation on the Macoun'09 conference here:
http://www.macoun.de/video2009ts6.php
If you're still interested in AOP for Objective-C you can send me a mail to negm-awad#cocoading.de or simply visit this site:
aspective-c.com/index.html
in a few weeks. There will be an english version (yup, not translated by me ;-)) of the site and the manual in a few weeks.
https://github.com/eleme/Stinger
Stinger is a high-efficiency library with great compatibility, for aop in Objective-C, using libffi.

Objective-C: call a method you just created

Simple question, as I am coming from another programming language. In Objective-C, lets say in a controller class I want to separate certain code into its own method, how do I call that method let's say, from viewLoad. As an example, let's say I create a method:
(void)checkIfInputCorrect
{
NSLog(#"text");
}
Now, i wanted to have in a delegate method, call this method. I tried [self checkIfInputCorrect] and get a warning saying Controller may not respond to -CheckIf...
I thought something like checkIfInputCorrect() would work that gives an error as well.
Basically how do you call a method?
Add this to your .h file
- (void)checkIfInputCorrect;
Call it with:
[self checkIfInputCorrect];
You need to list the method in the interface (ideal) or list the method implementation before the calling method (less ideal) so that the compiler can know that the class responds to the selector before it compiles the calling line.
To paraphrase Martin,
In your .m file, make sure your method -checkIfInputCorrect is placed so that it's physically above the method that has the line: [self checkIfInputCorrect];

What's the easiest way to determine inside a method if it has been called the first time since app start?

I have a method where I do some startup animations. The method gets called many times during usage of the app, but on it's first call it needs to do some special things in addition.
Are Singletons the way to go? Maybe there is also an better way, instead of measuring how many times this method was called, and storing that in an ivar.
- (void)someMethod {
static BOOL hasBeenCalledBefore = NO;
if (!hasBeenCalledBefore) {
// perform setup
hasBeenCalledBefore = YES;
}
// do other stuff
}
Extra work may be required if you're using threads, but that's the basic idea.
Why isn't that initialization code in the constructor? Maybe you need to factor that method out into it's own class which uses the constructor to handle the init block you mention.
An amendment to chuck's answer (pretty much correct)
His works and answers your question, but another option you could use (assuming it didn't need access to any of the variables being passed to that method) would be to take the code out of your method and put it in a static initializer. It will only be executed when the class is first loaded and will isolate what is essentially completely different pieces of code.
If you want it called for every new class, use Chuck's answer but with a member variable, or use a class initializer.

Call back style

I am writing an iPhone application which in numerous places needs to perform non HTTP or FTP networking of a very simple request response type.
I've wrapped all this up into a SimpleQuery class that integrates with the run loop.
SimpleQuery *lookup = [[SimpleQuery alloc] init];
[lookup setDelegate:self];
[lookup doQueryToHost:queryServer port:queryPort query:queryString ];
As you can see the calling object sets itself as a delegate. When the results are complete it then calls a method on the delegate with the results.
[delegate simpleQueryResult:resultString simpleQuery:self];
I am now in a position where I have a user of SimpleQuery that has two types of query so I need to extend SimpleQuery to support this.
I can think of two sensible ways of doing this.
Firstly passing a selector into doQueryString, or a seperate doQueryStringWithSelector.
[lookup doQueryToHost:queryServer port:queryPort query:queryString selector:#SEL ];
Secondly passing a tag into doQueryString so that when the delegate is called it can query the tag, as the simpleQuery is passed, to find out what the results are for.
[lookup doQueryToHost:queryServer port:queryPort query:queryString withTag:tag ];
I'm just wondering which is best from a coding style perspective, the first seems simpler but tagging seems more in keeping with the iPhone SDK and Interface Builder
An option which is used commonly in Apple's code (for example, in UIControl) is to provide both a target object and a selector. This works only if there is a single callback, and is more appropriate than a delegate in that case. (If there are multiple callbacks, then you'll probably have to go with a delegate and the tag approach.)
If you go this route, then you do away with the delegate altogether and instead have a method with a signature like this:
doQueryToHost:(id)queryServer port:(int)queryPort query:(NSString*)queryString target:(id)target action:(SEL)action
Note that "action" is typically preferred over "selector" in methods arguments in this case. The query would simply call the selector on the target when done. This would allow your clients to have multiple selectors, and also multiple target objects; this can help clean up code because you don't need to shove everything into a single delegate object.
If you want to go with your tag route, you should call it "context", which is what Apple uses (for example, in addObserver:forKeyPath:options:context).
There's a third option that's a common pattern in the kits, which is to use #protocols.
For example:
#protocol QueryCompleteHandlerProtocol
- (void)queryType1Complete:(int)intStuff;
- (void)queryType2Complete:(float)floatStuff;
#end
What this does is declare a set of method calls that an object adopting the protocol has to conform to (the compiler will actually enforce this).
So your SimpleQuery object will hold on to something like the delegate pointer, which you might declare like this among the ivars:
NSObject<QueryCompleteHandlerProtocol> *callback;
What this tells the compiler is that callback is an object that descends from NSObject and adopts the QueryCompleteHandlerProtocol protocol. Sometimes you see this written as:
id<QueryCompleteHandlerProtocol> callback;
When you want to call the callback there's nothing special about them, SimpleQuery's methods will just call:
[callback queryType1Complete:1];
[callback queryType2Complete:2.0];
Finally you client for the procotol class will declare itself as adopting the protocol:
#interface MyClass : NSObject<QueryCompleteHandlerProtocol>
...
#end
And will set itself as the callback with some code like:
[lookup setCallback:self];
This is where the compiler checks that MyClass conforms to QueryCompleteHandlerProtocol, meaning it has implemented queryType1Complete: and queryType2Complete:.
I'm not sure I understand the problem here. Can't SimpleQuery's user just set another delegate object for the second query, or branch on the simpleQuery: parameter? That's a basic part of the delegate pattern, just like having two UIActionSheets for one view controller.