Unlocking screen logic upon session creation

I need to run tests and my devices (both Android and iOS) will be locked at the moment of testing. By locking I mean that they will have a pincode set. This is something I cannot avoid, security related reasons. So I have to deal with this.

##Sending keyboard events
I could play a little with ADB and could see that actually ADB (in the case of Android) offers me the possibility to do something like this:

adb shell input keyevent KEYCODE_NUMPAD_1
adb shell input keyevent KEYCODE_NUMPAD_2
adb shell input keyevent KEYCODE_NUMPAD_2
adb shell input keyevent KEYCODE_NUMPAD_1
adb shell input keyevent KEYCODE_NUMPAD_ENTER

This unlocked my Android device. For iOS I could not really find an equivalent.

##Unlocking in Appium code
I would like to add this logic in Appium. So basically when a new session is requested and error 33 occurs (Could not unlock device) I would like to use ADB from Appium to send those commands. The same for iOS. I do not want you to explain me with detailed ocde how to achieve this, but could please tell me where in the code I should act for this? Just a few hints thanks.