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

Zha*_*ang 3 iphone iphone-privateapi

我需要打电话

[[MKLocationManager sharedLocationManager] _applyChinaLocationShift:newLocation]
Run Code Online (Sandbox Code Playgroud)

在我的iOS应用程序中.

我相信MKLocationManager是一个私有类,似乎没有MapKit/MKLocationManager.h文件.

我不是针对App Store.我有什么办法可以使用私有API吗?

更新于2011-6-23

我真的需要答案,还是我可以解开iOS SDK?

我的声誉几乎就是100.请帮我.

Luk*_*ath 10

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

首先,除非你只调用一次,否则我将用一个辅助方法包装代码:

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

您现在可以使用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];
}
Run Code Online (Sandbox Code Playgroud)

我没有运行上面的代码,但它应该做的伎俩.如果由于某种原因,@selector()呼叫不起作用(我认为它们应该),那么你可以用NSSelectorFromString()呼叫替换它们.