if you have not used foam before, you will be astonished at how it spreads and sticks.
You will need lots and lots of newspaper or disposable dustsheets on the floor. You will need a box of 100 disposable gloves (preferably nitrile). As soon as a spec of foam gets on your glove you will have to throw it away and put on a fresh one. If you tread on spilled foam it will stick to your shoe, and to the floor, carpet or pavement.
You will need disposable overalls, or something you were going to throw away
You will need to cover all exposed, skin and hair.
Expose the crack as best you can by removing loose trim. Brush and hoover it out. Wash it out with a garden sprayer with warm water and WUL. It does not matter if the gap is damp as long as there is no pooling water.
I strongly recommend the use of cling film on walls, windowsills and glass, taped at the edges.
Tape the wall beside and under the crack, both inside and outside the house.
Put tape over the crack, with pencil-holes in it every foot or so. The foam has an injecting nozzle. Put this through the hole deep into the gap, starting t the bottom. You will see it expand, pushing against the tape (which will burst off if not very secure). Wait until it has stopped expanding, then inject through the next, higher hole. Continue until you have injected all. Then go outside and inject through the outside holes.
It is possible to tack or screw a wooden batten, wrapped in clingfilm, against the tape to prevent it bursting out, and to form a straight and flat surface for the foam to mould against. If you don't wrap the batten in clingfilm, it will be firmly stuck to the wall.
Have a garden sprayer handy for spills, drips and bursts. Water makes the foam form a non-sticky crust.
After it has all set (give it an hour or two) pull away your tapes and film. Spay it with water in case it is still sticky where not exposed to the air. When you are sure it is fully hard you can trim it with a serrated knife, or even sandpaper. It will degrade in sunlight, so you can use a dark silicone sealant (which will also keep out rain) on the outside, or you can paint it. Indoors you can use sealant, or filler, or paint.
When it is fully hard, you can use a knife or sandpaper to get it off your skin or eyeballs. It will stay on your fingernails, but you might be able to chip it off after a week or two.
You can get a can of Foam Eater from places that sell foam.
Once you have started a can, you have to use it all or throw it away. It will harden and clog the nozzle in a matter of minutes if you stop for tea. There may be a plastic keeper by the nozzle that you can plug open end of the injector tube onto.