Is there a way to determine in code if an SPFx web part is running in the SharePoint App rather than in SharePoint in the browser?
I had answered a similar question on stackoverflow where a user wanted to see if the SPFx App is executed from the Teams Client or the Browser. You can find the answer and the details of the test here.
Basically, what I did was fetch an abstracted library, which calls the native APIs that hold OS-specific information and have that information shown on the screen. The library that I used was react-platform-js.
As you will notice from my mini-PoC, you get different OS information when viewing the SPFx App from the browser and something different when in the context of Teams Client. The same should apply to the Sharepoint App, not to mention, that based on the returned OS-Info Object you can determine in code if the SPFx App is executed in the context of the SP App or the Browser.
I was hoping there would be something native to SPFx for this. It's possible there is but I haven't found it yet.
If there wasn't something native to SPFx, then I was hoping there was a solution that didn't require an external library. Using the answer posted by @jimas13 as inspiration, I took a look at using
navigator.userAgent. My tests indicate that the value of
navigator.userAgent includes the text "SharePoint for" when the web part is running in the SharePoint app. On my Android phone
navigator.userAgent included "SharePoint for Android" and on my iPad
navigator.userAgent included "SharePoint for iOS".
I'm going to use this until I find that it's unreliable or I find a better solution. I'll post a comment here in either case.