How to check one condition in two scenarios when app enters in foreground in swift

58 Views Asked by At

I have two image upload buttons

1st in profile pic upload button i am using UIImagePickerController 2nd in file and image upload button i am using HSAttachmentPicker library

code: when i open camera and and dismiss and comeback to app then i dont want to call applicationWillEnterForeground > guard let user = getGeneralInfo?.result?.user else { return }.

so i used isCameraMode variable to check when i open camera but in my second condition image and file upload button using HSAttachmentPicker here i couldn't check when i open camera because i have used library so in this condition when i open camera and comeback to app then my applicationWillEnterForeground hits and my "getGeneralInfoService()" also refreshes, how to handle this. please do guide me

var isCameraMode = false

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)

if !isCameraMode {
    getGeneralInfoService()
}
}

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
  
    if !isCameraMode {
        NotificationCenter.default.addObserver(
            self,
            selector: #selector(applicationWillEnterForeground(_:)),
            name: UIApplication.willEnterForegroundNotification,
            object: nil)
    }
    isCameraMode = false
}

@objc func applicationWillEnterForeground(_ notification: NSNotification) {
    if !isCameraMode {
        
        guard let user = getGeneralInfo?.result?.user else { return }
        if user.identity_verification_status == "V"{
            tabIndex = 2
            paymentTextLabel.text = "Your ID has been verified"
        }
    }
}

func openCamera() {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerController.SourceType.camera) {
    self.isCameraMode = true
    imagePickerEdit.delegate = self
    imagePickerEdit.sourceType = UIImagePickerController.SourceType.camera
    imagePickerEdit.allowsEditing = false
    self.present(imagePickerEdit, animated: true, completion: nil)
} 
//works fine
@IBAction func profilePicBtn(_ sender: UIButton) {
    chooser = .profile
    let alert = UIAlertController(title: "Choose Image", message: nil, preferredStyle: .actionSheet)
    alert.addAction(UIAlertAction(title: "Camera", style: .default, handler: { _ in
        self.openCamera()
    }))
    
    alert.addAction(UIAlertAction(title: "Gallery", style: .default, handler: { _ in
        self.openGallery()
    }))
    
    alert.addAction(UIAlertAction.init(title: "Cancel", style: .cancel, handler: nil))
    self.present(alert, animated: true, completion: nil)
}


//issue: this is file and image upload button here when i open camera "applicationWillEnterForeground" hits and service call calls
@IBAction func insuranceBtn(_ sender: UIButton) {
    picker.delegate = self
    picker.showAttachmentMenu()
}


}
0

There are 0 best solutions below