简体   繁体   English

无法在iOS Swift项目中集成ZXingObjC

[英]Unable to integrate ZXingObjC in a iOS Swift Project

Im working on an iOS project, which shows the customer number in a barcode. 我正在开发一个iOS项目,它在条形码中显示客户编号。 I had installed the framework ZXingObjC with CocoaPods, described in GitHub . 我在GitHub中描述了CocoaPods安装了框架ZXingObjC

I can compile my Project without errors. 我可以毫无错误地编译我的项目。 I can also use the classes of ZXingObjC in my Objective-C classes, without errors. 我也可以在Objective-C类中使用ZXingObjC类,没有错误。 After than, I have added the import Command #import <ZXingObjC/ZXingObjC.h> to my bridging header file, like my other custom objective-c classes, without compile errors. 之后,我将import命令#import <ZXingObjC/ZXingObjC.h>到我的桥接头文件中,就像我的其他自定义objective-c类一样,没有编译错误。 (I had testet the header file by destroying some import statements and got the expected file not found exception.) (我通过销毁一些import语句来测试头文件并获得了预期的文件未找到异常。)

But now, I can't use any class of ZXingObjC in my swift classes. 但现在,我不能在我的swift类中使用任何类的ZXingObjC I only got the following compile error: Use of undeclared type '...' . 我只得到以下编译错误: Use of undeclared type '...' The Xcode autocomplete is not working, too. Xcode自动完成功能也不起作用。

eg 例如

var test : ZXMultiFormatWriter?
>> Use of undeclared type 'ZXMultiFormatWriter'

I tried: 我试过了:

  • setup new project, same issue 设置新项目,同样的问题
  • checked header search path: $(SRCROOT)/Pods/Headers/Public/Adjust 检查标题搜索路径: $(SRCROOT)/Pods/Headers/Public/Adjust
  • reinstalled the ZXingObjC framework 重新安装了ZXingObjC框架
  • checked build settings: Enable Modules: YES 检查构建设置: Enable Modules: YES
  • checked build settings: Other Linker Flags: $(inherited) -ObjC -framework "ZXingObjC" 检查构建设置: Other Linker Flags: $(inherited) -ObjC -framework "ZXingObjC"
  • checked linked binaries in the build phases: framework is added 检查构建阶段中的链接二进制文件:添加框架
  • checked import statement in the bridging header file ( #import <ZXingObjC/ZXingObjC.h> and #import "ZXingObjC/ZXingObjC.h" -- no difference) 在桥接头文件中检查了import语句( #import <ZXingObjC/ZXingObjC.h>#import "ZXingObjC/ZXingObjC.h" - 没有区别)
  • Windows style: restarting Xcode and Mac ;-) Windows风格:重启Xcode和Mac ;-)

I'm using: 我正在使用:

  • Xcode: 6.3.2 Xcode:6.3.2
  • CocoaPods: 0.37.2 CocoaPods:0.37.2
  • Project Deployment target: iOS 8.0 项目部署目标:iOS 8.0
  • SDK: 8.3 SDK:8.3

Does anyone know the problem? 有谁知道这个问题? Can anyone help? 有人可以帮忙吗? How can I make the ZXingObjC framework available in swift? 如何在swift中提供ZXingObjC框架?

Actually it is an easy issue: 实际上这是一个简单的问题:

Podfile Podfile

source 'https://github.com/CocoaPods/Specs.git'

platform :ios, '8.0'
use_frameworks!

pod 'ZXingObjC', '~> 3.1'

So, on terminal: 所以,在终端上:

cd workspace
pod install

Then, once opened project on Xcode, you have to edit bridging-header adding ZXingObj: 然后,一旦在Xcode上打开项目,你必须编辑添加ZXingObj的桥接头:

#import <ZXingObjC/ZXingObjC.h>

Finally, in your swift classes that uses ZXingObjC, you have to import ZXingObjC. 最后,在使用ZXingObjC的swift类中,您必须导入ZXingObjC。

import ZXingObjC

class ZXingObjCWrapper {

    func encode() {
       let writer = ZXMultiFormatWriter.writer()        
       ....
    }

}

The rest of the code for when you need to set an UIImage with this bar code: 当您需要使用此条形码设置UIImage时的其余代码:

func generateDataMatrixQRCode(from string: String) -> UIImage? {
    do {
        let writer = ZXMultiFormatWriter()
        let hints = ZXEncodeHints() as ZXEncodeHints
        let result = try writer.encode(string, format: kBarcodeFormatDataMatrix, width: 1000, height: 1000, hints: hints)

        if let imageRef = ZXImage.init(matrix: result) {
            if let image = imageRef.cgimage {
                return UIImage.init(cgImage: image)
            }
        }
    }
    catch {
        print(error)
    }
    return nil
}

The header search path was not correct in my project. 标题搜索路径在我的项目中不正确。 The right values are: 正确的价值观是:

$(inherited)
"${PODS_ROOT}/Headers/Public"
"${PODS_ROOT}/Headers/Public/ZXingObjC"

The second and third lines were not added by installation with CocoaPods. 第二行和第三行未通过CocoaPods安装添加。

EDIT: The installed framework have to be added to "Embedded Binaries" in General tab of the project. 编辑:必须将已安装的框架添加到项目的“常规”选项卡中的“嵌入式二进制文件”中。

I tried everything on this page to add ZXingObjC as a Pod. 我尝试了此页面上的所有内容,将ZXingObjC添加为Pod。 My goal was to generate an Aztec barcode. 我的目标是生成Aztec条形码。

I checked my Header Search Path. 我检查了我的标题搜索路径。 As Reddas said, I had to manually add "${PODS_ROOT}/Headers/Public/ZXingObjC". 正如Reddas所说,我不得不手动添加“$ {PODS_ROOT} / Headers / Public / ZXingObjC”。 I also added ZXingObjC as an Embedded Binary (in the General Tab). 我还将ZXingObjC添加为嵌入式二进制文件(在“常规”选项卡中)。

I checked my bridging file & all was good. 我检查了我的桥接文件,一切都很好。 I checked my view controllers where I wanted to generate the barcode. 我检查了我想要生成条形码的视图控制器。 The import ZXingObjC was there. 进口ZXingObjC就在那里。

No compile errors. 没有编译错误。 But I can't declare a variable of ZXingObjC. 但我无法声明ZXingObjC的变量。

No luck. 没运气。 Any more suggestions? 还有什么建议吗?

EDIT - I went into the Targets, Build Settings and searched for Header Search Paths. 编辑 - 我进入了目标,构建设置并搜索了标题搜索路径。 I added in BOTH "${PODS_ROOT}/Headers/ Public /ZXingObjC" and "${PODS_ROOT}/Headers/ Private /ZXingObjC" 我添加了“$ {PODS_ROOT} / Headers / Public / ZXingObjC”和“$ {PODS_ROOT} / Headers / Private / ZXingObjC”

This seemed to unclog whatever broke. 这似乎疏通了任何破裂。 It works now. 它现在有效。 Strangely, I can now even delete those entries and it works. 奇怪的是,我现在甚至可以删除这些条目并且它有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM