Implementing a ViewController for Android for a shared application. - xamarin

Im trying to do this for a shared application in Xamarin. In the iOS section is looks like:
var window = UIApplication.SharedApplication.KeyWindow;
var vc = window.RootViewController;
var user = await auth0.LoginAsync(vc, withRefreshToken: true);
but for android I have no idea how to implement the view controller 'vc' to pass to LoginAsync().

You can always refer to https://auth0.com/docs/quickstart/native/xamarin. In Android referring this will give the current context you are in. this context is very much similar to window.RootViewController for iOS. So all you need to do is calling
var user = auth0.LoginAsync(this, withRefreshToken: true);
If you want to show the login in other window for android, get the context and assign it to a Android.App.Application.Context type variable and replace this with the variable in the previously mentioned code.

Related

SwiftUI macOS app with `App` protocol deep linking opens new app instance

This is in a SwiftUI macOS app using the new App protocol and #main.
Usage flow:
User launches app and clicks a button which opens a particular webpage
Webpage eventually redirects to the app's URL scheme, opening the app and invoking onOpenURL(_:)
Expected behaviour:
The deep link is sent to the existing, currently open app instance
Actual behaviour:
A new app instance is launched, causing two instances of the app to be active
Note: There isn't really any code to add since the problem is just dependent on adding a URL scheme to the app and having a webpage go to it.
onOpenURL(_:) is not actually launching a new app instance, it is creating a new window within the existing instance. The documentation would suggest this only happens on macOS (because iOS only supports a single window).
You need to use the .handlesExternalEvents(preferring:allowing:) modifier on a higher order view. Calling handlesExternalEvents will override the default behaviour which is what is creating the new window in your app on macOS. Something like:
#main
struct myApp: App {
var body: some Scene {
WindowGroup {
ContentView()
.handlesExternalEvents(preferring: ["myscheme"], allowing: ["myscheme"])
}
}
}
An then in a child view (e.g. ContentView()):
var body: some View {
VStack {
// your UI
}
.onOpenUrl{ url in
// do something with the deep link url
}
}

Disable default alert when Bluetooth is Turned OFF in iOS

I have integrated SerialIO RFID SDK and using Bluetooth LE Plugin to check Bluetooth Status in my Xamarin.iOS app.
Each time when I launch the app, it keeps showing me the alert as soon as any of the code from above SDK or plugin is invoked. I don't want to show this alert. Instead, I want to notify user about this, in some other way.
I found this way to disable this alert, but Where and How to
write this in Xamarin, is my Question.
I searched a lot for Xamarin Specific solution and didn't find any.
I tried following in AppDelegate.cs but none of them worked. May be I am not sure how to write this.
// Way : 1
var cbCentralInitOptions = new CBCentralInitOptions();
cbCentralInitOptions.ShowPowerAlert = false;
var cbCentralManager = new CBCentralManager(this, null, cbCentralInitOptions);
// Way : 2
//NSDictionary options = new NSDictionary();
//options.SetValueForKey(NSNumber.FromNUInt(0), CBCentralManager.OptionShowPowerAlertKey);
//var cbCentralManager = new CBCentralManager(this, null, options);
Please help me to get this default alert suppressed. Thanks.

Custom video page for xamarin ios

I create some project that consists from videoplayer. For Xamarin android I implemented through messaging center.
On android:
Page(xamarin forms) -> (click button and sending message to MainActivity) -> calling MyVideoActivity via intent
With android all works perfectly.
But I am newbie with ios and xamarin ios also.
And I don't know is this real to implement the same in xamarin ios through messaging center ? Also I would like use .xib file(with ui, taken from Xcode). I am not sure how to implement it. I would like to do it through messaging center -> calling custom ViewController with my .xib file. Is it real?
p.s. I've found some example with PageRenderer, but don't investigate it. I am not sure that it will be suitable for me.
Call the following methods in your MessagingCenter.Subscribe lambda:
XIB-based ViewController:
var vc = new XIBBasedViewController();
// Set any public properties / call methods on your `XIBBasedViewController`
InvokeOnMainThread(() => UIApplication.SharedApplication.Windows[0].RootViewController.PresentViewController(vc, true, null));
Storyboard-based ViewController:
var storyboard = UIStoryboard.FromName("StoryBoardFileNameMinusExtension", null);
var vc = storyboard.InstantiateViewController("AStoryBoardIDAssignedToYourViewController") as AViewControllerSubClass;
// Set any public properties / call methods on your `AViewControllerSubClass`
InvokeOnMainThread(() => UIApplication.SharedApplication.Windows[0].RootViewController.PresentViewController(vc, true, null));
Note: A good place to setup your messaging center subscribes is in the FinishedLaunching of the AppDelegate class.

Open Native iOS app from Xamarin.Forms

I have an native iOS sample app installed in my iPad and also my sample xamarin.Forms App installed. I want to open native iOS app from Xamarin.forms when clicked on button. I have set URL schema in my native iOS app in info.plist.
How do I open native app from xamarin.forms on click of a button. I already tried
var request = Device.OnPlatform(
string.Format("native app url"),
string.Format("native app url"),
string.Format("native app url"));
Device.OpenUri(new Uri(request));
but its not working.
It's basically how you are doing it.
var url = Device.OnPlatform(iOSUrl, androidUrl, winPhoneUrl);
Device.OpenUri(new Uri(request));
Be aware that each platform have some requirements in the way to url is configured and parameters are sent.
Let say to get the maps app open in each platform you would use these urls schemes:
// iOS doesn't like %s or spaces in their URLs, so manually replace spaces with +s
var iOSUrl = string.Format("http://maps.apple.com/maps?q={0}&sll={1}", name.Replace(' ', '+'), loc);
var androidUrl = string.Format("geo:0,0?q={0}({1})", string.IsNullOrWhiteSpace(addr) ? loc : addr, name);
var winPhoneUrl = $"bingmaps:?cp={loc}&q={name}";
var url = Device.OnPlatform(iOSUrl, androidUrl, winPhoneUrl);
Also you cannot open any arbitrary app unless you know its Url scheme.
Anyway, if the Device.OpenUri() doesn't fulfill your requirements you are good to create your own implementations using the native APIs.
Please go through the below link and attached photo
Link:-launch-an-app-from-within-another-iphone
I am using below code in xamarin forms
NSUrl request = new NSUrl("Camera://");
var canOpen = UIApplication.SharedApplication.CanOpenUrl(new NSUrl(request.ToString()));
if (!canOpen)
{ Task.FromResult(false); }
else
{
Task.FromResult(UIApplication.SharedApplication.OpenUrl(new NSUrl(request.ToString())));
}

Xamarin.forms OnAppLinkRequestReceived

I'm trying to use https://developer.xamarin.com/api/member/Xamarin.Forms.Application.OnAppLinkRequestReceived/p/System.Uri/ but can't seem to get it to be called in any way.
Did anyone ever tried to implement this new method?
I've tried several ways to implement different deeplinks, all of them open the app fine so they're correctly configured but that method never gets called.
Thanks.
This is how I set it up in Android.
Put this above your MainActivity.cs
[IntentFilter(new[] { Android.Content.Intent.ActionView },
Categories = new[]
{
Android.Content.Intent.CategoryDefault,
Android.Content.Intent.CategoryBrowsable
},
DataScheme = "http",
DataPathPrefix = "/tesla/",
DataHost = "exrin.net")]
This registers the activity when the app is installed. Change the URL to your desired URL.
For Android only (no need to do this with iOS) you need to also install the Nuget Xamarin Forms AppLinks
In your OnCreate make sure you do this after your Xamarin Forms Init
AndroidAppLinks.Init(this);
Then when you load the URL in a browser (in my example http://exrin.net/tesla) you will get this:
Then if you open the app it will enter here with the full URL as the URI parameter. This is in the App.cs (Xamarin.Forms.Application)
protected override void OnAppLinkRequestReceived(Uri uri)
{
base.OnAppLinkRequestReceived(uri);
}
You can then decode the URI as you see fit to move to the specific page in your app that the URL relates to/
More details at Xamarin Forms AppLinks
Also, your MainActivity must be derived from FormsAppCompatActivity for it to work. Once I changed it to that from FormsApplicationActivity, it started working.

Resources