Unable to integrate ZXingObjC in a iOS Swift Project
Asked Answered
P

5

6

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?

Pimento answered 3/6, 2015 at 8:47 Comment(1)
At the Other Linker Flags, I have $(inherited) -ObjC -l"Pods-ZXingObjC" -framework "AVFoundation" -framework "CoreGraphics" -framework "CoreMedia" -framework "CoreVideo" -framework "ImageIO" -framework "QuartzCore" and works well.Crescentic
P
5

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

}
Pylle answered 16/10, 2015 at 11:2 Comment(0)
T
1

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
}
Theine answered 19/7, 2017 at 9:50 Comment(0)
P
0

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.

Pimento answered 3/6, 2015 at 11:1 Comment(0)
I
0

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.

Iapetus answered 23/12, 2015 at 11:8 Comment(0)
T
0

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.

Trapshooting answered 19/9 at 4:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.