如何在 iOS 中使用 MKLocationManager(私有 API)

发布于 2024-11-16 01:05:37 字数 363 浏览 4 评论 0原文

我需要调用

[[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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

離人涙 2024-11-23 01:05:37

如果上面的答案不适合您,这可能是因为整个类是私有的(包括它的标头)。这是使用一些运行时技巧的替代方法;您必须确保签名正确,但我们可以使用一些防御性编码来避免崩溃。

首先,除非您只调用一次,否则我会将代码包装在辅助方法中:

// in some header file, you may want to give the method a prefix too
CLLocation *ApplyLocationManagerChinaLocationShift(CLLocation *newLocation);

您现在可以使用 NSClassFromString 来获取对该类和 performSelector 的引用来执行该方法。为了安全起见,我们可以尝试首先确保该方法存在:

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];
}

我还没有运行上面的代码,但它应该可以解决问题。如果由于某种原因 @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:

// 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.

梦屿孤独相伴 2024-11-23 01:05:37

您可以简单地自己创建方法描述,本质上是在 MKLocationManager 上创建您自己的类别。通过定义私有方法的外观,可以使其可调用。但您必须确定它的签名,因为如果您关闭,那么您的应用程序就会崩溃。

这个类别可以放在它自己的 .h 文件中,或者如果您只在 @implementation 正上方的一个地方使用它。

@interface MKLocationManager (china)
- (CLLocation *)_applyChinaLocationShift:(CLLocation *)newLocation;
@end

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文