2

I have built an iOS SDK that is available on cocoapods, and as a manual download for people that don't want to use cocoapods. When you install the SDK via cocoapods, it automatically adds the "-ObjC" value into build settings Other Linker Flags.

I am fairly confident that there is no way to write to the build settings programmatically (although please correct me if I'm wrong because that would be great). However, I am wondering if there is a way to read build settings programmatically.

I would like to read from build settings within my SDK, then if the user has not added "-ObjC" in other linker flags, present an error message telling them to do so.

I have already specified the need for adding "-ObjC" to Other Linker Flags in my SDK documentation, but I have had a few users that have missed this section, so I'm looking to notify them of their error in Xcode.

arc4randall
  • 3,275
  • 3
  • 27
  • 40
  • There is probably a runtime check you can do, e.g. have some random method in an object file that you never call, then try to dynamically load it. If that fails, they probably forgot -objc and you can assert or log or whatever. – i_am_jorf Mar 28 '16 at 19:26
  • You can check my answer here: http://stackoverflow.com/a/39853547/5165668. It answers your "if there is a way to read build settings programmatically". However, it requires using a Run Script and I am not sure whether you are able to add a Run Script to some project from within your static library (please reply if you know how to). – OlDor Oct 04 '16 at 13:41

1 Answers1

2

I don't think you can read the settings file, but there is a neat trick:

You can add a category method to a class and check if it exists. The reason is that an unused category method won't be linked without the Obj-C flag.

So you can do something like

@interface NSString (ObjCFlagTesting)
-(void) myLibName_TestLinkerFlag;
@end

@implementation NSString (ObjCFlagTesting)
-(void) myLibName_TestLinkerFlag {}
@end

just to get an empty selector in there, and then see if it's available at runtime somewhere else:

-(BOOL) isObjCFlagSet
{
   return [[NSString alloc] init] respondsToSelector: @selector(myLibName_TestLinkerFlag)];
}
mszaro
  • 1,362
  • 1
  • 13
  • 25
  • Wow this works great, thanks! I'm going to do this during the initialization phase of my SDK. Would you recommend logging an NSError, and just waiting for Xcode to throw the Exception right after, or should I just throw my own exception first? I'm leaning towards throwing my own because the Xcode one only says it can't find the category method, and doesn't say anything about how to resolve the issue. – arc4randall Mar 29 '16 at 13:53
  • 1
    Up to you. Personally I never throw exceptions in my SDKs as this can crash the host app, which is probably the worst possible outcome. You could always log an error and then try not to do any work afterwards. – mszaro Mar 29 '16 at 18:54