上記の回答がうまくいかない場合は、クラス全体がプライベート (ヘッダーを含む) である可能性があります。これは、実行時のトリックを使用した別のアプローチです。署名が正しいことを確認する必要がありますが、防御的なコーディングを使用してクラッシュを回避できます。
まず、これを 1 回だけ呼び出す場合を除き、コードをヘルパー メソッドにまとめます。
// 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()
代わりに呼び出しに置き換えることができます。