如果在Xcode中添加/链接框架,如何包含头文件?

时间:2016-12-21 13:50:02

标签: ios objective-c xcode frameworks

我正在使用Objective C开发一个Xcode项目,该项目具有可选框架,这意味着只有在项目中链接/存在时,才会从该特定框架执行某些代码。我目前使用的框架是PassKit。我怎样才能做到这一点?是否有一些宏可以告诉框架链接或不是?像这样的东西,但它不起作用:

#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];
   }}

此外,我已在目标中将自动链接框架设置为。感谢您的回答和帮助。

1 个答案:

答案 0 :(得分:0)

#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
}