2

I'm working on a Xcode project with Objective C that has optional frameworks which means that some code is executed from that particular framework only if it is linked/exists in project. The current framework that I'm using is PassKit. How can I achieve this? Is there some macro that can tell is framework linked or it is not? Something like this, but it doesn't work:

#if __has_include("PassKit.h") && __has_include(<PassKit.h>)
#import <PassKit/PassKit.h>
#endif

- (void)viewDidAppear:(BOOL)animated {
     if ([PKPassLibrary class]) {
         UIAlertController *alert = [UIAlertController
                                alertControllerWithTitle:@"PassKit Test"
                                message:@"PassKit is linked."
                                preferredStyle:UIAlertControllerStyleAlert];

      UIAlertAction* okButton = [UIAlertAction
                               actionWithTitle:@"Ok"
                               style:UIAlertActionStyleDefault
                               handler:^(UIAlertAction * action) {
                                   NSLog(@"Ok pressed...");
                               }];

       [alert addAction:okButton];

       [self presentViewController:alert animated:YES completion:nil];
   }}

Also I have set Link Frameworks Automatically to NO in my target. Thanks for your answers and help.

MattCodes
  • 489
  • 8
  • 21
  • 1
    Include it normally, but check if the function is available before using. Possibly a duplicate of: http://stackoverflow.com/questions/33038137/xcode-and-optional-frameworks – Sealos Dec 21 '16 at 13:56
  • I still don't get it how I can use this... Is `weak_import` in this `function extern int MyWeakLinkedFunction() __attribute__((weak_import));` parameter for framework name? How can I connect this with PassKit? – MattCodes Dec 21 '16 at 14:12
  • I need something that will tell me if framework exists during run time. If framework exists, it will run code. – MattCodes Dec 21 '16 at 14:37
  • I found a solution. I've implemented this method: `- (BOOL)checkIfFrameworkIsLinked:(NSString *)className { BOOL isFrameworkLinked = (NSClassFromString(className) != nil); return isFrameworkLinked; }` and it works fine. – MattCodes Dec 21 '16 at 14:55
  • Hello, can I do that with frameworks written in swift? we have been struggling for a week.. here is our problem, can you help? https://stackoverflow.com/questions/55077857/if-canimport-does-not-find-frameworks-with-cocoapods – Viktor Vostrikov Mar 09 '19 at 20:20

1 Answers1

2
#if __has_include(<PassKit/PassKit.h>) || __has_include("PassKit.h")
#define XXXPKTrigger
#endif

- (void)viewDidAppear:(BOOL)animated {
#ifdef XXXPKTrigger
    UIAlertController *alert = [UIAlertController
                                alertControllerWithTitle:@"PassKit Test"
                                message:@"PassKit is linked."
                                preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction* okButton = [UIAlertAction
                               actionWithTitle:@"Ok"
                               style:UIAlertActionStyleDefault
                               handler:^(UIAlertAction * action) {
                                   NSLog(@"Ok pressed...");
                               }];

    [alert addAction:okButton];
    [self presentViewController:alert animated:YES completion:nil];
#else
// do another logic
#endif
}
Zhiping Yang
  • 379
  • 3
  • 7
  • While this code snippet may be the solution, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-‌​code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – peacetype Jan 19 '18 at 02:45
  • Hello, can I do that with frameworks written in swift? we have been struggling for a week.. here is our problem, can you help? https://stackoverflow.com/questions/55077857/if-canimport-does-not-find-frameworks-with-cocoapods – Viktor Vostrikov Mar 09 '19 at 20:20