Reddas
Reddas

Reputation: 61

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:

I'm using:

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

Upvotes: 6

Views: 4899

Answers (5)

mehul jetani
mehul jetani

Reputation: 51

Solution To resolve this, I added the following line to my Podfile:

pod 'ZXingObjC', :git => 'https://github.com/zxingify/zxingify-objc'

This allowed me to successfully install the library.

Upvotes: 0

asheyla
asheyla

Reputation: 3325

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
}

Upvotes: 1

rustyMagnet
rustyMagnet

Reputation: 4095

I tried everything on this page to add ZXingObjC as a Pod. My goal was to generate an Aztec barcode.

I checked my Header Search Path. As Reddas said, I had to manually add "${PODS_ROOT}/Headers/Public/ZXingObjC". I also added ZXingObjC as an Embedded Binary (in the General Tab).

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.

No compile errors. But I can't declare a variable of 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"

This seemed to unclog whatever broke. It works now. Strangely, I can now even delete those entries and it works.

Upvotes: 0

Luca Davanzo
Luca Davanzo

Reputation: 21528

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()        
       ....
    }

}

Upvotes: 5

Reddas
Reddas

Reputation: 61

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.

EDIT: The installed framework have to be added to "Embedded Binaries" in General tab of the project.

Upvotes: 0

Related Questions