Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to integrate ZXingObjC in a iOS Swift Project

Im working on an iOS project, which shows the customer number in a barcode. I had installed the framework ZXingObjC with CocoaPods, described in GitHub.

I can compile my Project without errors. I can also use the classes of ZXingObjC in my Objective-C classes, without errors. 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. (I had testet the header file by destroying some import statements and got the expected file not found exception.)

But now, I can't use any class of ZXingObjC in my swift classes. I only got the following compile error: Use of undeclared type '...'. The Xcode autocomplete is not working, too.

e.g.

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

I tried:

  • setup new project, same issue
  • checked header search path: $(SRCROOT)/Pods/Headers/Public/Adjust
  • reinstalled the ZXingObjC framework
  • checked build settings: Enable Modules: YES
  • checked build settings: 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)
  • Windows style: restarting Xcode and Mac ;-)

I'm using:

  • Xcode: 6.3.2
  • CocoaPods: 0.37.2
  • Project Deployment target: iOS 8.0
  • SDK: 8.3

Does anyone know the problem? Can anyone help? How can I make the ZXingObjC framework available in swift?

like image 642
Reddas Avatar asked Jun 03 '15 08:06

Reddas


2 Answers

Actually it is an easy issue:

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:

#import <ZXingObjC/ZXingObjC.h>

Finally, in your swift classes that uses ZXingObjC, you have to import ZXingObjC.

import ZXingObjC

class ZXingObjCWrapper {

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

}
like image 94
Luca Davanzo Avatar answered Oct 03 '22 19:10

Luca Davanzo


The rest of the code for when you need to set an UIImage with this bar code:

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
}
like image 30
asheyla Avatar answered Oct 03 '22 19:10

asheyla