How do you launch a web authentication window within Unity for a UWP app? - c#

I'm trying to use the Twitter Service from the UWPCommunityToolkit, which I have running as a standalone UWP (Universal Windows Platform) app, but when I import it into Unity (2017.2.0f3) as a library, it doesn't open the authentication window.
This is what shows up on the working standalone UWP app:
When in unity, it seems to go through the setup code, but not run this line properly:
var result = await WebAuthenticationBroker.AuthenticateAsync(WebAuthenticationOptions.None, startUri, endUri);
https://github.com/Microsoft/UWPCommunityToolkit/blob/master/Microsoft.Toolkit.Uwp.Services/Services/Twitter/TwitterDataProvider.cs#L293
It just returns WebAuthenticationStatus.UserCancel, whereas on the standalone UWP, it'd return WebAuthenticationStatus.Success. Note, it also returns WebAuthenticationStatus.UserCancel on the standalone UWP app when the user clicks the close button on the pop up.
Is it possible to run WebAuthenticationBroker.AuthenticateAsync (https://learn.microsoft.com/en-us/windows/uwp/security/web-authentication-broker) in Unity? Are there any other ways to do web authentication on the Windows platform using Unity?
I've also tried TwitterKit, but unfortunately, it doesn't have support for Windows UWP (only iOS and Android).
Thanks!

LINQ to Twitter supports UWP. It has a UniversalAuthorizer that works like this:
private async void TweetButton_Click(object sender, RoutedEventArgs e)
{
var authorizer = new UniversalAuthorizer
{
CredentialStore = new InMemoryCredentialStore
{
ConsumerKey = "",
ConsumerSecret = ""
}
};
await authorizer.AuthorizeAsync();
var ctx = new TwitterContext(authorizer);
string userInput = tweetText.Text;
Status tweet = await ctx.TweetAsync(userInput);
ResponseTextBlock.Text = tweet.Text;
await new MessageDialog("You Tweeted: " + tweet.Text, "Success!").ShowAsync();
}
Check out the Samples folder for a complete listing: https://github.com/JoeMayo/LinqToTwitter

Related

UWP app not launching with one Launcher.LaunchUriAsync override

I have an wpf app that I want to use to open another wpf app.
Call them image viewer (Gallery) and image storage (AppToAppCommunication).
Image storage app shows a list of available images.
Then, on ListBox selection changed I have this code:
private async void lbImages_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var options = new LauncherOptions();
options.ContentType = "image/jpg";
var filePath = Environment.CurrentDirectory + "\\Images\\" +
((ListBoxItem)((ListBox)sender).SelectedItem).Content;
var file = await StorageFile.GetFileFromPathAsync(filePath);
var token = SharedStorageAccessManager.AddFile(file);
ValueSet inputData = new ValueSet();
inputData.Add("Token", token);
Uri uri = new Uri("myprotocol:");
//await Launcher.LaunchUriAsync(uri); // #works
//await Launcher.LaunchUriAsync(uri, options, inputData); // #doesn't
}
Now, when I use the line at #works, it works, with no image shown, as expected.
But, when instead I use line at #desn't, it gives me this error:
Now I tried clicking OK on the popup and running a new VS instance an waiting for it to load, but it never does.
I've tried running both applications from the same solution and I can get to a breakpoint in override void OnActivated in image viewer app (Gallery) when using the #works line in image storage app (AppToAppCommunication), but I can't get to that same breakpoint when using the #doesn't line.
I don't know what the problem is and I am not able to debug it.
How could I go about finding a way to debug this properly?

uwp close browser programmatically

I'm creating a simple news feed, it i want it can open the browser to show the details of the news, but i don't know how to close the browser , here is the code of how I open the browser,anyone can teach me how to off the browser using uwp? '
public async void test()
{
RootObject mynews = await NewsProxy.GetNews();
string website = mynews.articles[i].url;
var uriWeb = new Uri(websites);
var success = await Windows.System.Launcher.LaunchUriAsync(uriWeb);
if (success)
{
//Uri launched
}
else
{
// uri launch failed
}
}
var success = await Windows.System.Launcher.LaunchUriAsync(uriWeb);
Your code is actually telling the OS to Launch the given url and the OS in turn launches the default browser with the given URL. So you are not actually launching the browser.
In order to have full control over the browser behavior you can implement your own WebView and then use the url to navigate your WebView.
webView1.Navigate("http://www.contoso.com");
(MSDN Documentation for WebView)

Windows Phone 8.1 : how to Launch an Installed app From My app

I have tried the following links
How do I launch other app from my own app in Windows Phone 8.1?
Call an external app on windows phone 8.1 runtime(not silverlight) xaml c# application
but nothing is working for me as i simply wants to launch one app for e.g. angry birds from my app using a button click.
Please Help
If the app has a registered URI, then you should be able to use Launcher.LaunchUriAsync for this. You can also find some help, here at MSDN - How to launch the default app for a URI.
Otherwise I don't think it's possible.
It works for me.
setup your app which launch from other.
add this in WMAppManifest.xaml between < /token> and < ScreenResolutions>
<Extensions>
<Protocol Name="alsdkcs" NavUriFragment="encodedLaunchUri=%s" TaskID="_default" />
</Extensions>
add a class "AssociationUriMapper " and override the function "MapUri()"
class AssociationUriMapper : UriMapperBase{
private string tempUri;
public override Uri MapUri(Uri uri){
tempUri = System.Net.HttpUtility.UrlDecode(uri.ToString());
if (tempUri.Contains("alsdkcs:MainPage?id=")){
int idIndex = tempUri.IndexOf("id=") + 3; //3 is the length of "id="
string id = tempUri.Substring(idIndex);
return new Uri("/MainPage.xaml?id=" + id, UriKind.Relative);
}
return uri;
}
}
and call them from other app.xaml.cs at this section of InitializePhoneApplication() function
RootFrame.UriMapper = new AssociationUriMapper(); // add this line only between RootFrame.Navigated += CompleteInitializePhoneApplication; and RootFrame.NavigationFailed += RootFrame_NavigationFailed;
finally call fron other app to launch an app
var success = await Windows.System.Launcher.LaunchUriAsync(new System.Uri("alsdkcs:MainPage?id="+5));
if (success){
// URI launched
}
else{
// URI launch failed
}
where "alsdkcs" is protocol name.
more details see msdn

How to load javascript in windows 8

I am looking for a way to implement Internet explorer - a desktop version in my Winodws 8 app . This is caused by my c# application- i need to run javascript page but its impossible in metro version of IE. Is there any method to do this?
I `ll describe my problem: when i want to load html of DOM page(rendered by javascript) i cant because javascript can run (I use WebView). I tried to open this page using Desktop version and it worked perfectly. Unfortunately Metro IE10 cant show the content and my WebView too.
Thanks for help.
You can sure inject in the c# webview a javascript file:
This is the action for a button:
private async void Lamp_Click(object sender, RoutedEventArgs e)
{
Uri uri = new Uri("ms-appx:///js/injected.js");
StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(uri);
string text = await FileIO.ReadTextAsync(file); //reading content as string
var script = "(function(){" + text + "})()";
}
try
{
var result = wv.InvokeScript("eval", new string[] { script });
}
catch (Exception) { }
wv is you id name for the webview element in the xaml

Xamarin C# for Android : Searching for an App and path

GOAL : I need to find if an app is installed on a device AND find it's path..
I see using PackageManager, you can do this in general but I would like to refine it.
I know if you use com.google.chrome you can find chrome explorer installed,
but this fails when you look simply for Chrome.
On some devices Chrome (and other apps, like Opera, Mini,etc) is not installed as com.google.chrome.
So how would one find an app without the com.google and just use Chrome as the search criteria ?
Simple, just combine PackageManager and ApplicationInfo, and then check if app name contain your search string. Here is some example code:
var searchQuery = "chrome";
var flag = PackageInfoFlags.Activities;
var apps = PackageManager.GetInstalledApplications(flag);
foreach(var app in apps)
{
try
{
var appInfo = PackageManager.GetApplicationInfo(app.PackageName, 0);
var appLabel = PackageManager.GetApplicationLabel(appInfo);
if (appLabel.ToLower().Contains(searchQuery.ToLower()))
{
var builder = new AlertDialog.Builder(this);
builder.SetTitle("Found it!");
builder.SetMessage(appLabel + " installed at: " + app.SourceDir);
builder.Show();
}
}
catch (PackageManager.NameNotFoundException e) { continue; }
}

Categories