Preface: I have little experience with C integration in iOS, feel free to correct me on any misinterpretations I have about this.
I have a project that has a custom 2-tier "SDK" both written in C. The coreSDK makes method calls to the deviceSDK which communicates with the ios framework to execute hardware actions (ie. enable camera) or retrieve device information (ie. path to NSDocumentDirectory).
Unfortunately, due to the infrastructure, both SDK file extensions use (.h) and (.c) and cannot be changed to (.m) as some sources recommended. According to what I've read, I can create C-callbacks to the ObjC methods but that's only really viable for singletons, and the C-callback needs to be within the scope of the (.m) file.
I have tried creating a wrapper class in ObjC such that it has a C-callback from which the deviceSDK calls to, but when including the header(.h) for that (.m), the compiler seems to crash. If my understanding is correct, it's because the C-compiler cannot interpret the ObjC language contained in the (.m).
I believe theoretically is possible to write iOS apps in pure C with ObjC runtime, but ideally would rather not go down that route as what I've seen is absurdly complicated.
An example workflow
- ViewController.m method calls to coreSDK method (.c)
- coreSDK method calls to deviceSDK method (.c)
- deviceSDK needs to retrieve (for example) NSDocumentDirectory (or enable camera)
How can I achieve this?
Code examples would be most comprehensive and appreciated.
Thanks!
These are some(not all) references I've already looked into...
- How to write ios app purely in c
- Pure C function calling Objective-C method
- C function calling objective C functions
- Using native objective-c method for C callbacks
- Callback methods from C to Objective-C
- Mixing C functions in an Objective-C class
- How to call an Objective-C method from a C method
- How to use pure C files in an objective-C project
Edit: 2016-07-21
To clarify the issue, I'm having trouble making calls from C methods in (.c) files to ObjC methods in (.m) files, or figuring out an alternative means to retrieve information such as (for example) NSDocumentsDirectory in ObjC (.m files) and passing back to C in (.c) files.
Example Code: Of course it's incorrect but it's along the lines of my expectations or what I'm hoping to achieve.
//GPS.h
#include "GPSWrapper.h"
STATUS_Code GPSInitialize(void);
//GPS.c
#include "GPS.h"
STATUS_Code GPSInitialize(void) {
GPS_cCallback();
}
//GPSWrapper.h
#import <Foundation/Foundation.h>
@interface GPSWrapper: NSObject
- (void) testObjC;
@end
//GPSWrapper.m
#import "GPSWrapper.h"
static id refToSelf = nil;
@implementation GPSWrapper
- (void) testObjC {
// just an example of a ObjC method call
NSString *documentsDirectory = [paths objectAtIndex:0];
NSLog(documentsDirectory);
}
@end
static void GPS_cCallback() {
[refToSelf testObjC];
}