Calling Obj-C method from C - ios

I've got a library written in C, where I need to push a call to a method written in Obj-c.
I don't want to modify the original code too much, so I decided to create a "bridge" class to handle the calls between C and ObjC:
DRMBridge.h
#ifndef DRMBridge_h
#define DRMBridge_h
#include "DRMBridgeObjC.h"
void bridge_test();
#endif
DRMBridge.c
#import "DRMBridge.h"
void bridge_test() {
ctest();
}
Above is compiled as C
Now here's my objc code:
DRMBridgeObjC.h
#ifndef DRMBridgeObjC_h
#define DRMBridgeObjC_h
#import <Foundation/Foundation.h>
#interface DRMBridgeObjC : NSObject
+(void) test;
#end
void ctest();
#endif
DRMBridgeObjC.m
#import "DRMBridgeObjC.h"
#implementation DRMBridgeObjC : NSObject
+(void) test {
NSLog(#"OH YEAH!");
}
#end
void ctest() {
[DRMBridgeObjC test];
}
Quite simple.
In the C library I want to call my code from I've added:
#include "DRMBridge.h"
into the .h file and
bridge_test();
in .c file.
Now the best part, when I compile I get:
In file included from /Users/krystian/projects/mdb-reader-lib/reader-Include/ios/MDBReader/Class/DRM/DRMBridge.c:5:
In file included from /Users/krystian/projects/mdb-reader-lib/reader-Include/ios/MDBReader/Class/DRM/DRMBridge.h:8:
In file included from /Users/krystian/projects/mdb-reader-lib/reader-Include/ios/MDBReader/Class/DRM/DRMBridgeObjC.h:12:
In file included from /Applications/Xcode64.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator8.4.sdk/System/Library/Frameworks/Foundation.framework/Headers/Foundation.h:8:
/Applications/Xcode64.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator8.4.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:400:1: error: expected identifier or '('
#class NSString, Protocol;
^
[...]
I went looking, and found this: ios - Parse Issues in NSObjCRuntime, NSZone, and NSObject
However my pch file looks like this:
#import <Availability.h>
#ifndef __IPHONE_5_0
#warning "This project uses features only available in iOS SDK 5.0 and later."
#endif
#ifdef __OBJC__
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#endif
I think the issue I have is that the C compiler tries to compile my ObjC file first, instead of the other way around.
I've tried changing the order of files inside Compile Sources to make sure that my .m file is above .c file, but still no go.
Something else I have found which made me lost:
when I follow this answer: https://stackoverflow.com/a/20461780/487605 and change the library itself to .m, and call [DRMBridgeObjC test] code from there - it works.... Compiles fine, no errors given, works fine.
This implies to me, that there's something screwed up with my DRMBridge, but what?
Thanks
Krystian

You've seen the solution already. Use #ifdef __ OBJC__ in your Objective-C header file so that only the plain C bits are compiled when including the file from C

DRMBridge.h
#ifndef DRMBridge_h
#define DRMBridge_h
#include <CoreFoundation/CoreFoundation.h> //or put in .pch
CF_EXPORT void bridge_test();
#endif
DRMBridge.m
#import "DRMBridge.h"
#import "DRMBridgeObjC.h"
void bridge_test() {
ctest(); //or [DRMBridgeObjC test]; if you like
}
Thats all. And you can include DRMBridge.h in whatever you like: in .c, in .m, in .cpp.

Related

Xcode 8 (Swift): core.hpp header must be compiled as C++

I am using the OpenCV iOS Framework in a project. I followed the necessary steps to include the framework into the project.
The Project is written using Swift 3.
One of my classes that contains the core functionality of the App is written in Objective-C++. I included the header of the class in my Bridge-header file but when trying to run the project I get the following error:
error core.hpp header must be compiled as C++
After researching online and on SO, the most common solution presented was to create a Wrapper class that would be imported in the bridge header. However, after following this article I face the same problem.
The Header file of my class looks like this:
#if TARGET_OS_IPHONE
#import <UIKit/UIKit.h>
#define IMAGE_CLASS UIImage
#elif TARGET_OS_MAC
#import <Cocoa/Cocoa.h>
#define IMAGE_CLASS NSImage
#endif
#import <AGGeometryKit/AGKQuad.h>
#import <stdio.h>
#import <opencv2/opencv.hpp>
#import <Foundation/Foundation.h>
typedef void (^DebugBlock)(cv::Mat current_image, std::string current_image_name);
#interface ImageScanner : NSObject
/**
* Singleton for access to the scanner.
*
* #return Shared scanner.
*/
+ (instancetype)sharedScanner;
Does anyone have an idea what I might be missing?
Thank you in advance!
G.
had the same problem.. solved by importing any file that use openCV in the wrapperClass.mm file ...
SO Answer here

Preventing duplicate entry messages

I have two classes that are pretty much identical but one is made for OS X and the other one is made for iOS. Then I have a header file like this:
#ifdef __MAC_OS_X_VERSION_MIN_REQUIRED
#import "ClassForOSX.h"
#else
#import "ClassForIOS.h"
#endif
The problem is because both classes are included in the project, xcode compiles both and I have a lot of duplicate entries on the errors, because methods have the same name on both classes.
Is there a way to prevent a class from compiling, even if it is included on the project, unless the class is meant for the target?
Yes I know I can include/exclude classes from targets but I am dealing with hundreds of classes in both sides and multiple targets and that would generate a lot of include/exclude operations that will eventually go wrong. I was wondering if there is some solution in code that can just allow a class to compile if the target is right.
Use targets is best for your case. But You can do it in code
note add or remove __MAC_OS_X_VERSION_MIN_REQUIRED at Preprocessor Macros at target->Build Settings
ClassForIOS.h file
#ifndef __MAC_OS_X_VERSION_MIN_REQUIRED
#import <Foundation/Foundation.h>
#interface ClassForIOS : NSObject
// interface
#end
#endif
ClassForIOS.m file
#ifndef __MAC_OS_X_VERSION_MIN_REQUIRED
#import "ClassForIOS.h"
#implementation ClassForIOS
// implementation code
#end
#endif
ClassForOSX.h file
#ifdef __MAC_OS_X_VERSION_MIN_REQUIRED
#import <Foundation/Foundation.h>
#interface ClassForOSX : NSObject
// interface
#end
#endif
ClassForOSX.m file
#ifdef __MAC_OS_X_VERSION_MIN_REQUIRED
#import "ClassForOSX.h"
#implementation ClassForOSX
// implementation code
#end
#endif

cannot find interface declaration for 'NSObject', superclass of 'GPXType'

I have done some research on that issue , but I have not found anything similar just yet.
I am using iOS GPX framework to draw the path on map using GPX file. I have import iOS GPX.framework on my project. but I have face an issue.
Please Guide me, If anyone has any advice...
Just modify the header file, add this line on top of the file
#import <Foundation/Foundation.h>
Seems they thought that you will have a PCH file, where Foundation and UIKit will be imported, but Xcode 6 removed PCH default support, so the problem came. (See my previous answer)
Finally I have solved my problem
I have import #import < UIKit/UIKit.h> and change my Xcode 6 Architectures $(ARCHS_STANDARD_32_BIT).
Thanks so much Guys.
You haven't imported the header file #import ...
When compiling for both iOS and OsX, I had similar issue that I have resolved by importing TargetConditionals.h. The final thing looks like this:
#import <TargetConditionals.h>
#if TARGET_IPHONE_SIMULATOR || TARGET_OS_IPHONE
#import <UIKit/UIKit.h>
#elif TARGET_OS_MAC
#import <Cocoa/Cocoa.h>
#endif
#interface MyClass : NSObject
#if TARGET_IPHONE_SIMULATOR || TARGET_OS_IPHONE
// Define UIKit methods
#elif TARGET_OS_MAC
// Define Cocoa methods
#endif
- (void)reloadRegisteredComponents;
#end

How can I declare a config.h file, add it to my App-Prefix.pch, and then use it globally in my app?

I want to create a Config.h file to house all my static const strings that should be global to my application.
I've created a new Config.h file, but there are a few things I'm unaware of.
1) How do I declare variables. A or B?
A)
#define hotelURLString4 = #"http://blah.herokuapp.com/api/v1/hotels/";
B)
static NSString * const hotelURLString2 = #"http://blah.herokuapp.com/api/v1/hotels/";
2) I can't seem to use this file. If I try to import the Config.h directly into a file of mine, I get a "Config.h file not found" error in xcode. If I include it in my AppName-Prefix.pch up at the top via...
#import <Availability.h>
#import "Config.h"
#ifndef __IPHONE_5_0
#warning "This project uses features only available in iOS SDK 5.0 and later."
#endif
#ifdef __OBJC__
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#endif
That does not seem to work either. How do I do this.
3) How do I access the variable declared/defined. Do I call Config.hotelURLString, or [Config hotelURLString]... or how do I access it?
===================================================================
============================ UPDATE ===============================
1) I created my header like this... am I not doing something correctly, because I tried again and it won't work either.
2) This is my AppName-Prefix.pch file.
#import <Availability.h>
#import "Config.h"
#import "MyHeader.h"
#ifndef __IPHONE_5_0
#warning "This project uses features only available in iOS SDK 5.0 and later."
#endif
#ifdef __OBJC__
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#endif
3) Here is my Project Navigator
To answer question 1, method B is probably better, although you should name your variable more like:
kHoteUrlString2
And preferably something more descriptive then just the numbers, but the main point here is the lower case k and the uppercase first letter. This is a C/ObjC naming convention for constants.
As for question 2, it sounds like your file isn't actually in the project. Some more details are needed to answer this part of the question, and I'll update my answer if the question has more details added.
As for question 3, you use the variable exactly as you would as if you had declared it at the top of whatever file you're using. Objective-C doesn't have namespaces.

typedef in header file not getting picked up by xcode

I have a typedef in A.h file, and I import that A.h file in B.h that makes use of the same typedef.
For the longest time everything worked fine, but now it breaks complaining that it doesn't recognize the typedef and gives "Expect type" error in xcode, even tho xcode highlights the typedef in the second file, meaning that it knows they are defined else where.
if I manually redefine that typedef then the error goes away. but I get a warning that I am redefining a typedef and that it is a C11 feature.
I tried deleting the derived data, but the error stays. It looks like xcode is trying to look up the typedef before inserting the import files. Any clue? I'm using xcode 4.6
//WebService.h
#import <Foundation/Foundation.h>
#import "AppDelegate.h"
#interface WebService : NSObject
typedef void(^loginUserWithUserNameandPaswordCompletion)(NSDictionary *, NSError*);
+(void)createUserWithName:(NSString*)name
andEmail:(NSString*)email
andPassword:(NSString*)password
onCompletion:(loginUserWithUserNameandPaswordCompletion) complete;
#end
//DataCenter.h
#import <Foundation/Foundation.h>
#import "WebService.h"
#import "AppDelegate.h"
typedef void(^loginUserWithUserNameandPaswordCompletion)(NSDictionary *, NSError*);
#interface DataCenter : NSObject
+(void) logInUserWithEmail:(NSString*) email
andPassword:(NSString*)password
onCompletion:(loginUserWithUserNameandPaswordCompletion)complete;
#end
Try putting typedef before your #interface in WebService.h.
used #class as mentioned by gberginc

Resources