The problem is iPhone still thinks the handsfree is plugged in so it turns off the speaker.
My iPhone has got this issue as well. I fixed by keep plugging and unplugging until it worked.
That probably got the dust out from the jack.
If that does not fix, let’s try..
You could probably use alcohol to do this as well.
Warning: do not drink Windex
Next, create ScriptNameYouWant and put the commands you want.
Don’t forget to start the file with shebang #!/bin/sh to execute with Bourne shell.
This can be other. It depends on what you will execute.
The file will look like..
The commands to execute
and the last command
Save and make it executable..
sudo chmod +x ScriptNameYouWant
and restart your mac. See what you fuckup your Mac lol