Debug and deploy your flutter app with an iPhone on Linux
Published: 2021-10-14 11:21:56

Freedom, what a liberating feeling, it may take a few years, a centery or just a few steps to attain it. It's the feeling I've been yearning off as an aspiring ios developper for many many years, and thanks to many increadible people, I finally attained it and hope you will as well 😌.

Enough bla bla. What we're gonna go through in this post is how to debug your flutter app, and non-flutter ios ones if you want, with your iphone on linux. 


## Concept

This approach essentially consists in installing a VM with macos and XCode, passthrough your usb controller to be able to use your iphone and then ssh into. If you know how to do all this already look a little more at the bottom as there a few setup to do in order to make you able to debug any app.


## Ok, it has some caveats

First of all this will require approximatively 70 Gb, yes 70, at first at least. This is because we're installing macos in its entirety plus XCode, so storage is the first inconvenience of this approach. This can become overcome in a certain measure through a certain number of tricks and tips. 
**DISCLAIMER: you won't be downloading 70 Gb, just 30~ :3**

Second caveats is that since we're running a VM, this is very processor and battery intensive, so basically using this approach on the move on a laptop wouldn't be very efficient.

Basically if you have enough storage and have a power plug near you or are on desktop this approach should be a delight ✨.

## The incredible people mentioned prior:

* [Miko](https://github.com/mikob)
* [Reinis](https://github.com/reinismu)
* [Sick.codes](http://sick.codes), the author of docker-osx multi-talent automator.
* Dortania stuff
* Kholia

##  iPhone passthrough

This step is already very detailed in [my repo](https://github.com/Silfalion/Iphone_docker_osx_passthrough). It consists of:
- Downloading the OSX VM.
- Making your usb controller available to the VM.
- Adding the necessary instructions to the docker container launch script.
- launching the VM and that's all.


##  Setting up our environement for flutter


Now that you can detect your iDevice on macos, we can now fetch somehow our Flutter/Swift project, and for that we have multiple options:

  - If your project is on Github you can clone it into your macos VM through the command line(ssh below) or the GUI. Then you commit, push/pull on macos or vice versa to sync changes.
  - secure copy into the VM, like so:

```
scp -P 50922 -r /folder/you/want/to/transfer username_VM@localhost:/folder/you/to/put/it/in
```

The third option would be to share the project from your host to your guest and that theoritically allow any changes made to one to reflect on the other. I unfortunately couldn't get this to work at the time of writing this article.

The sharing works and the folder appears on both systems but the problem that kept occuring was that the shared folder wasn't given enough permissions from the host. I'm sure this can be easily fixed with more research but couldn't allocate it more time.

## SSHIng into the VM

One of the most powerful aspects of this trick is that you can even run a remote MacOS VM and be able to develop almost as if it's your system. This is possible through SSH.

We'll basically SSH into our VM through VsCode. We'll first activate this option in the VM by activating remote login:

1. Go into settings.
2. Select remote control.
3. Check remote login, enter password if asked.
4. Done!

The second step is setting the link(ports) on the host, very simple allowed thanks to Sick.codes by adding this line in the docker script we use to launch the VM:

```bash 
    -p 50922:10022 \
```

Now we can connect through vscode:

  - Open vscode and type CTRL+SHIFT+P
  - Type ssh and select "Remote-SSH: Add New SSH Host"
  - Make sure the VM is connected and press enter, enter your macos password then press enter again. **DISCLAIMER** this may be different if you have a remote VM set or changed the VM password previously.
  -  Vscode will now open a new window that will be connected to your VM. 
  -  Navigate to or create your flutter project whereever you like. Just like you'd do on your host, but all through vscode.
  
You should now have your project in front of you and your iPhone recognized as a device, you can check that by running:

```
flutter devices
```

in the vscode terminal.

I noticed that there are some issues if the right permissions are not given to the project, so I just give it all permissions through ```chmod -R 777```. I'm not working with security sensitive data at the moment but in most cases you should revaluate the permissions you give and be careful about it.


## Setting up xcode

XCode needs your account so that it can sign your apps and deploy them to your device. 

To do that go to xcode on your VM and open the file called .workspce under ios/runner in your flutter project. Select runner in the file tree on the left then singing and capabilities in the bar in the middle top of the screen. Choose your team or create one by connecting your apple id then hit sign in. This is necessary to generate the certificates for your apps and would be needed even for a normal mac setup.

## Finale

Now go back to your vm connected vscode window on your host and run this last line:

```
security unlock-sign in
```

This is necessary because otherwise macos will bother you by opening a prompt to enter your password. Look more into if you need to increase your security.

You should now be able to compile and deploy your project to your iPhone.

## Bonus

To economize some CPU and GPU power, you can launch your VM without a display output, so just delete this line from the script:

```
-e "DISPLAY=${DISPLAY:-:0.0}" \
```

## Help

This is quite a mixed up mess of possibly poorly written information so if you need clarification with anything or improvements, direct your issues here please: 

https://github.com/Silfalion/Iphone_docker_osx_passthrough

Just make sure you've read the article thoroughly before positng.

Happy coding :smile:!