2016-03-23 35 views
5

他の同僚の開発者が使用する目的のフレームワークを開発しています。オプションの依存関係の引数を目的関数のフレームワーク関数のパラメータとして渡す方法

このフレームワークでは、他のフレームワークの利用可能なクラスが利用可能な場合、必要に応じて使用したいと思います。

は例えば、(利用可能な場合 - アプリ開発者によってリンクされ)私はAdSupport.frameworkを使用しています現時点では次のようなアプローチを:今、しかし

if (NSClassFromString(@"ASIdentifierManager")) { 
     NSString adString = [[[NSClassFromString(@"ASIdentifierManager") sharedManager] advertisingIdentifier] UUIDString]; 
} 

、私は公共の関数の引数を持ちたいです私のフレームワークにオプションの依存関係のクラスを含めることができ、私はそれを行うことができません。例えば

+ (void) sendLocation: (CLLocation *) myLoc; 

しかしCoreLocation.frameworkは、必要に応じてリンクし、アプリに多分利用できませんされます。

私は機能を持っていると思います。上記のAdSupport.frameworkで同様のアプローチを実行するにはどうすればよいですか?

私はこのような何か行うことができます仮定:

+ (void) sendLocation: (NSClassFromString(@"CLLocation") *) myLoc; 

または

+ (void) sendLocation: (id) myLoc; 

または

+ (void) sendLocation: (Class) myLoc; 

をして、座標が、それを達成することができませんでした何とか取り出します。最後のオプション(クラス)はコン​​パイルされているようですが、パラメータを抽出する方法が見つかりません。

誰でも助けてもらえますか?

答えて

0

MapKit(あなたがそれを要求しない限り、あなたのアプリケーションにリンクされません)

ヘッダーとショート例:

@class MKMapView; 
@interface MyTestInterface : NSObject 
+ (void)printMapViewDescription:(MKMapView *)mapView; 
@end 

実装ファイル:

#import "MyTestInterface.h" 
#import <MapKit/MapKit.h> 

@implementation 

+ (void)printMapViewDescription:(MKMapView *)mapView { 
    if ((NSClassFromString(@"MKMapView")) { 
     NSLog(@"%@", mapView); 
    } else { 
     NSLog(@"MapKit not available"); 
    } 
} 

@end 

ですから、ヘッダにリンクに内部。これは、バイナリを提供する場合、またはリンゴフレームワークのみを使用する場合にのみ機能します。ソースコードを提供し、サードパーティのフレームワークと対話したい場合は、performSelector、NSInvocation、または匿名オブジェクト(id)のobjc-runtimeで作業する必要があります。

EDIT:

@class MKMapView; 
@interface MyTestInterface : NSObject 
+ (void)printMapViewFrame:(MKMapView *)mapView; 
@end 

実装ファイル:

#import "MyTestInterface.h" 

@implementation 

+ (void) printMapViewFrame:(id)mapView { 
    if ([mapView respondsToSelector:@selector(frame)]) { 
     NSMethodSignature *sig = [mapView methodSignatureForSelector:@selector(frame)]; 
     if (sig) { 
      NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:sig]; 
      [invocation setTarget: mapView]; 
      [invocation setSelector:@selector(frame)]; 
      [invocation invoke]; 
      CGRect rect; 
      [invocation getReturnValue:&rect]; 
      NSLog(@"%@", NSStringFromCGRect(rect)); 
     } 
    } 
} 

@end 
NSInvocation

ヘッダーと

関連する問題