'QR Scanner won't work on iOS devices (Unity)

Please let me start off by saying I have only been programming for about 2 years now. I am somewhat new to it. I am also more of an Android developer than an Apple developer.

Recently, I have been programming a scavenger hunt game for my job, and the QR system works beautifully on Android. iOS, well, not so much. The camera was rotated and inverted. Upgrading the Unity version did help with it being inverted, and I have some code that can rotate the camera on just iOS. The camera comes up, but it cannot read the QR codes.

To explain the code a little bit, if the camera is disabled, not allowed, etc. it will fall back to a Password system. If the camera is working and allowed, it will turn on.

If anyone that understands iOS can explain this to me, that would be awesome.

Here is the code:

    using System.Collections.Generic;
    using System;
    using UnityEngine;
    using UnityEngine.UI;
    using ZXing;
     
    public class QRScanner : MonoBehaviour
    {
        WebCamTexture webcamTexture;
        string QrCode = string.Empty;
        public GameObject QRCodeScanner;
        public GameObject PasswordPanel;
        public Text ScanInfo;
        public RawImage RawImage;
        public Button Scan;
        public Button PassPanelClose;
        public GameObject FishMenuButton;
        public Text AddFishButtonText;
        public System_Fish System_Fish;
        public string output = "";
        public string stack = "";
     
        void Start()
        {
            PassPanelClose.onClick.AddListener(() => {
                PasswordPanel.SetActive(false);
            });
     
            Scan.onClick.AddListener(() => {
                if (QRCodeScanner.activeInHierarchy)
                {
                    AddFishButtonText.text = "+";
                    QRCodeScanner.SetActive(false);
                    return;
                }
                bool hasRearCamera = false;
                WebCamDevice[] devices = WebCamTexture.devices;
                // No webcam device was found, revert to fallback method
                if (devices.Length == 0)
                {
                    PasswordPanel.SetActive(true);
                } else
                {
                    foreach (WebCamDevice cam in devices)
                    {
                        if (!cam.isFrontFacing)
                        {
                            hasRearCamera = true;
                            break;
                        }
                    }
                }
     
     
                // Webcam was found but is not a rear facing camera, revert to fallback method
                if (!hasRearCamera)
                {
                    PasswordPanel.SetActive(true);
                } else
                {
                    StartCoroutine(GetQRCode());
                }
            });
        }
     
        IEnumerator GetQRCode()
        {
            AddFishButtonText.text = "X";
            FishMenuButton.SetActive(false);
            QRCodeScanner.SetActive(true);
            // Rear facing webcam was found
            var renderer = RawImage;
            webcamTexture = new WebCamTexture(1129, 512);
            renderer.material.mainTexture = webcamTexture;
     
            IBarcodeReader barCodeReader = new BarcodeReader();
            webcamTexture.Play();
     
            if (Application.platform == RuntimePlatform.IPhonePlayer)
            {
                this.RawImage.rectTransform.sizeDelta = new Vector2(1920, 1080 * this.webcamTexture.width / (float)this.webcamTexture.height);
                this.RawImage.rectTransform.localScale = new Vector3(-1, 1, 1);
                this.RawImage.rectTransform.rotation = Quaternion.Euler(0, 0, 180);
            }
     
            var snap = new Texture2D(webcamTexture.width, webcamTexture.height, TextureFormat.ARGB32, false);
            while (string.IsNullOrEmpty(QrCode) && QRCodeScanner.activeInHierarchy)
            {
                try
                {
                    snap.SetPixels32(webcamTexture.GetPixels32());
                    var Result = barCodeReader.Decode(snap.GetRawTextureData(), webcamTexture.width, webcamTexture.height, RGBLuminanceSource.BitmapFormat.ARGB32);
                    if (Result != null)
                    {
                        QrCode = Result.Text;
                        System_Fish.CollectFish(QrCode);
                        if (!string.IsNullOrEmpty(QrCode))
                        {
                            QrCode = string.Empty;
                            break;
                        }
                    }
                }
                // Catch any issues with webcam not functioning correctly
                catch (Exception ex)
                {
                    Debug.LogWarning(ex.Message);
                }
                yield return null;
            }
            webcamTexture.Stop();
            QRCodeScanner.SetActive(false);
            FishMenuButton.SetActive(true);
            AddFishButtonText.text = "+";
            QrCode = string.Empty;
        }
    }


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source