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:
$(SRCROOT)/Pods/Headers/Public/Adjust
Enable Modules: YES
Other Linker Flags: $(inherited) -ObjC
-framework "ZXingObjC"
#import
<ZXingObjC/ZXingObjC.h>
and #import "ZXingObjC/ZXingObjC.h"
-- no
difference)I'm using:
Does anyone know the problem? Can anyone help? How can I make the ZXingObjC framework available in swift?
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()
....
}
}
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With