3

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.

5
  • 2
    Include it normally, but check if the function is available before using. Possibly a duplicate of: stackoverflow.com/questions/33038137/… Commented Dec 21, 2016 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? Commented Dec 21, 2016 at 14:12
  • I need something that will tell me if framework exists during run time. If framework exists, it will run code. Commented Dec 21, 2016 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. Commented Dec 21, 2016 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? stackoverflow.com/questions/55077857/… Commented Mar 9, 2019 at 20:20

1 Answer 1

3
#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
}
Sign up to request clarification or add additional context in comments.

2 Comments

While this code snippet may be the solution, including an explanation 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.
Hello, can I do that with frameworks written in swift? we have been struggling for a week.. here is our problem, can you help? stackoverflow.com/questions/55077857/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.