How can we initiate the launch of a UWP app using angular code and a URI from a browser, while passing user information to it?
In order to achieve this scenario, you can follow the guidance provided in Handle URI activation. Register your app with a URI scheme and when you launch the specific URL, you can retrieve the path and parameters from the app's OnActivated
method.
For example:
<a href="testapp:login?userid=1234&password=7894">Launch</a>
App OnActivated
:
public static Dictionary<string, string> ParseQueryString(Uri url)
{
if (string.IsNullOrWhiteSpace(url.Query))
{
return new Dictionary<string, string>();
}
var dic = url.Query.Substring(1)
.Split(new char[] { '&' }, StringSplitOptions.RemoveEmptyEntries)
.Select(param => param.Split(new char[] { '=' }, 2, StringSplitOptions.RemoveEmptyEntries))
.GroupBy(part => part[0], part => part.Length > 1 ? part[1] : string.Empty)
.ToDictionary(group => group.Key, group => string.Join(",", group));
return dic;
}
protected override void OnActivated(IActivatedEventArgs e)
{
// set up frame
Frame rootFrame = Window.Current.Content as Frame;
if (rootFrame == null)
{
// Create a Frame to act as the navigation context and navigate to the first page
rootFrame = new Frame();
rootFrame.NavigationFailed += OnNavigationFailed;
Window.Current.Content = rootFrame;
}
Type deepLinkPageType = typeof(MainPage);
if (e.Kind == ActivationKind.Protocol)
{
var protocolArgs = (ProtocolActivatedEventArgs)e;
var dict = ParseQueryString(protocolArgs.Uri);
switch (protocolArgs.Uri.AbsolutePath)
{
case "":
break;
case "login":
//navigate to login page with above dict parameter
break;
}
}
if (rootFrame.Content == null)
{
// Default navigation
rootFrame.Navigate(deepLinkPageType, e);
}
// Ensure the current window is active
Window.Current.Activate();
}