如何在 iOS 中使用 MKLocationManager(私有 API)
我需要调用
[[MKLocationManager sharedLocationManager] _applyChinaLocationShift:newLocation]
我的 iOS 应用程序。
我相信 MKLocationManager 是一个私有类,并且似乎没有 MapKit/MKLocationManager.h 文件。
我的目标不是 App Store。有什么办法可以使用这个私有 API 吗?
更新于2011-6-23
我真的需要答案,或者我可以反编译iOS SDK吗?
100声望几乎就是我的全部了。请帮我。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果上面的答案不适合您,这可能是因为整个类是私有的(包括它的标头)。这是使用一些运行时技巧的替代方法;您必须确保签名正确,但我们可以使用一些防御性编码来避免崩溃。
首先,除非您只调用一次,否则我会将代码包装在辅助方法中:
您现在可以使用
NSClassFromString
来获取对该类和performSelector
的引用来执行该方法。为了安全起见,我们可以尝试首先确保该方法存在:我还没有运行上面的代码,但它应该可以解决问题。如果由于某种原因
@selector()
调用不起作用(我认为它们应该),那么您可以用NSSelectorFromString()
调用替换它们。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:
You can now use
NSClassFromString
to obtain a reference to the class andperformSelector
to perform the method. We can try and make sure the method exists first to be on the safe side: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 withNSSelectorFromString()
calls instead.您可以简单地自己创建方法描述,本质上是在 MKLocationManager 上创建您自己的类别。通过定义私有方法的外观,可以使其可调用。但您必须确定它的签名,因为如果您关闭,那么您的应用程序就会崩溃。
这个类别可以放在它自己的 .h 文件中,或者如果您只在 @implementation 正上方的一个地方使用它。
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.