Simple as that.
I'm making an app where the user has to scan a QR code (which basically is a base64 encoded string), is there any way to verify that the string is encoded in base64 before decode and follow the flow of the application?
My code would be responsible for that is:
func captureOutput(captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [AnyObject]!, fromConnection connection: AVCaptureConnection!) {
if metadataObjects == nil || metadataObjects.count == 0 {
qrCodeFrameView?.frame = CGRectZero
return
}
let metadataObj = metadataObjects[0] as AVMetadataMachineReadableCodeObject
if metadataObj.type == AVMetadataObjectTypeQRCode {
let barCodeObject = videoPreviewLayer?.transformedMetadataObjectForMetadataObject(metadataObj as AVMetadataMachineReadableCodeObject) as AVMetadataMachineReadableCodeObject
qrCodeFrameView?.frame = barCodeObject.bounds
if metadataObj.stringValue != nil /* Verify that string is base 64 before continue */ {
let decodedData = NSData(base64EncodedString: metadataObj.stringValue, options: NSDataBase64DecodingOptions.IgnoreUnknownCharacters)
let decodedString = NSString(data: decodedData!, encoding: NSUTF8StringEncoding)
println(decodedString)
}
/* More code */
}
}