簡體   English   中英

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

[英]How to use MKLocationManager (a Private API) in iOS

我需要打電話

[[MKLocationManager sharedLocationManager] _applyChinaLocationShift:newLocation]

在我的iOS應用程序中。

我相信MKLocationManager是一個私有類,似乎沒有MapKit / MKLocationManager.h文件。

我不是針對App Store。 我有什么辦法可以使用私有API嗎?

更新於2011-6-23

我真的需要答案,還是我可以解開iOS SDK?

我的聲譽幾乎就是100。 請幫我。

如果上述答案對您不起作用,這可能是因為整個類都是私有的(包括它的標題)。 這是使用一些運行時技巧的替代方法; 你必須確保簽名是正確的,但我們可以使用一些防御性編碼來避免崩潰。

首先,除非你只調用一次,否則我將用一個輔助方法包裝代碼:

// 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()調用替換它們。

您可以自己創建方法描述,實際上是在MKLocationManager上創建自己的類別。 通過定義私有方法的外觀,可以使其可調用。 但你必須確定它的簽名,因為如果你關閉,那么你的應用程序將崩潰。

此類別可以放在它自己的.h文件中,或者只在@implementation正上方的一個地方使用它。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM