How to pass arguments of optional dependencies as parameters in an object function with c framework

I am developing an objective c-structure that will be used by other developers.

In this structure I would like to use if available classes from other frameworks, if available.

For example, at the moment I am using AdSupport.framework (if available - associated with the application developer) with the following approach:

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

However, now I want the arguments of the public functions of my structure to include optional dependency classes, and I cannot do this.

For instance:

I want to have a function:

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

but CoreLocation.framework will not necessarily be related and may not be available to the application. How can I follow a similar approach using AdSupport.framework above?

I suggested that I could do something like this:

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

or

 + (void) sendLocation: (id) myLoc; 

or

 + (void) sendLocation: (Class) myLoc; 

and then somehow get the coordinates, but could not achieve this. The last option (class) seems to be compiling, but I cannot find a way to extract the parameters ..

Can anyone help with this?

+5
source share
1 answer

A short example with MapKit (will not be associated with your application unless you request it)

Title:

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

Implementation File:

 #import "MyTestInterface.h" #import <MapKit/MapKit.h> @implementation + (void)printMapViewDescription:(MKMapView *)mapView { if ((NSClassFromString(@"MKMapView")) { NSLog(@"%@", mapView); } else { NSLog(@"MapKit not available"); } } @end 

So you are referring to the internal headers. This will only work if you provide binary files or use only apple frameworks. If you provide the source code and want to interact with third-party frameworks, you need to work with the function performSelector, NSInvocation or objc-runtime on anonymous objects (id).

EDIT:

Example with NSInvocation

Title:

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

Implementation File:

 #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 
0
source

Source: https://habr.com/ru/post/1245636/


All Articles