How change the global tint color in Xamarin forms of the iOS application? - c#

How can I change the global tint color of my iOS app in a Xamarin forms project (C#)?

The global iOS tint color can be set via UIApplication.KeyWindow when it becomes available, the earliest after the application finished launching:
using Foundation;
using UIKit;
namespace MyApp.iOS
{
[Register("AppDelegate")]
public class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
{
public override bool FinishedLaunching(UIApplication uiApplication, NSDictionary launchOptions)
{
global::Xamarin.Forms.Forms.Init();
LoadApplication(new App());
var result = base.FinishedLaunching(uiApplication, launchOptions);
uiApplication.KeyWindow.TintColor = UIColor.Blue;
return result;
}
}
}

Add following code in your applications finished launching method in Xamarin.iOS Project.
Boolean result = base.FinishedLaunching(app, options);
UIApplication.SharedApplication.KeyWindow.TintColor = UIColor.Green;
return result;

Related

Xamarin Forms Multi-App - MacOS project not showing Xamarin forms

Edit: Added link to project I created:
https://drive.google.com/file/d/1A4XGKdxeC22_wqtNEtenLxYbgCp3I_dA/view?usp=sharing
I have a very basic multi-platform application created I Visual Studio 2019 (Windows 10) with 4 projects:
Source Project
Android
iOS
UWP
I added the MacOS project following these instructions:
https://learn.microsoft.com/en-us/xamarin/xamarin-forms/platform/other/mac
The application compiles but loads a blank form and does not show any of the Xamarin forms as per the UWP, iOS and Android apps.
I next created an empty Multi-Platform application natively on a Mac - added the MacOS as per the above instructions and get the same outcome, a blank form loading.
The code I am using in the Mac project is the link above.
// AppDelelegate.cs
using AppKit;
using Foundation;
using Xamarin.Forms;
using Xamarin.Forms.Platform.MacOS;
using pbTrader;
namespace pbTrader.MacOS
{
[Register("AppDelegate")]
public class AppDelegate : FormsApplicationDelegate
{
NSWindow window;
public AppDelegate()
{
var style = NSWindowStyle.Closable | NSWindowStyle.Resizable | NSWindowStyle.Titled;
var rect = new CoreGraphics.CGRect(200, 1000, 1024, 768);
window = new NSWindow(rect, style, NSBackingStore.Buffered, false);
window.Title = "Xamarin.Forms on Mac!"; // choose your own Title here
window.TitleVisibility = NSWindowTitleVisibility.Hidden;
}
public override NSWindow MainWindow
{
get { return window; }
}
public override void DidFinishLaunching(NSNotification notification)
{
Forms.Init();
LoadApplication(new App());
base.DidFinishLaunching(notification);
}
public override void WillTerminate(NSNotification notification)
{
// Insert code here to tear down your application
}
}
}
// Main.cs
using AppKit;
namespace pbTrader.MacOS
{
static class MainClass
{
static void Main(string[] args)
{
NSApplication.Init();
NSApplication.SharedApplication.Delegate = new AppDelegate(); // add this line
NSApplication.Main(args);
}
}
}

Xamarin Android iOS FirebasePushNotificationPlugin not receive message but token receves

i'm using xamarin iOS FirebasePushNotificationPlugin for cloud messages, but i'm receiving the token but i can not receive any notification. CrossFirebasePushNotification.Current.OnNotificationReceived even not firing.
here is how my appDelegate looks likes.
using System;
using System.Collections.Generic;
using System.Linq;
using Foundation;
using Plugin.FirebasePushNotification;
using UIKit;
namespace onlinefirebase.iOS
{
// The UIApplicationDelegate for the application. This class is responsible for launching the
// User Interface of the application, as well as listening (and optionally responding) to
// application events from iOS.
[Register("AppDelegate")]
public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
{
//
// This method is invoked when the application has loaded and is ready to run. In this
// method you should instantiate the window, load the UI into it and then make the window
// visible.
//
// You have 17 seconds to return from this method, or iOS will terminate your application.
//
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
global::Xamarin.Forms.Forms.Init();
LoadApplication(new App());
FirebasePushNotificationManager.Initialize(options, true);
FirebasePushNotificationManager.CurrentNotificationPresentationOption =
UserNotifications.UNNotificationPresentationOptions.Alert |
UserNotifications.UNNotificationPresentationOptions.Badge;
return base.FinishedLaunching(app, options);
}
public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
{
FirebasePushNotificationManager.DidRegisterRemoteNotifications(deviceToken);
}
public override void FailedToRegisterForRemoteNotifications(UIApplication application, NSError error)
{
FirebasePushNotificationManager.RemoteNotificationRegistrationFailed(error);
}
// To receive notifications in foregroung on iOS 9 and below.
// To receive notifications in background in any iOS version
public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> completionHandler)
{
// If you are receiving a notification message while your app is in the background,
// this callback will not be fired 'till the user taps on the notification launching the application.
// If you disable method swizzling, you'll need to call this method.
// This lets FCM track message delivery and analytics, which is performed
// automatically with method swizzling enabled.
FirebasePushNotificationManager.DidReceiveMessage(userInfo);
// Do your magic to handle the notification data
System.Console.WriteLine(userInfo);
completionHandler(UIBackgroundFetchResult.NewData);
}
}
}
Here is how i receive the notification
using System;
using Plugin.FirebasePushNotification;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace onlinefirebase
{
public partial class App : Application
{
public App()
{
InitializeComponent();
MainPage = new MainPage();
CrossFirebasePushNotification.Current.OnTokenRefresh += (s, p) =>
{
System.Diagnostics.Debug.WriteLine($"TOKEN : {p.Token}");
};
CrossFirebasePushNotification.Current.OnNotificationReceived += (s, p) =>
{
System.Diagnostics.Debug.WriteLine("Received");
};
}
protected override void OnStart()
{
}
protected override void OnSleep()
{
}
protected override void OnResume()
{
}
}
}
I have verified
. Enabled Background Mode -> Remote Notifications
. Info.plist -> "FirebaseAppDelegateProxyEnabled" boolean -> No
I'm successfully getting the token, but i can not receive the notification.
Issue is based on Apple key, apple key should be upload to firebase cloud panel and it'll work

You MUST call Rg.Plugins.Popup.Popup.Init(); prior to using it

System.InvalidOperationException:
You MUST call Rg.Plugins.Popup.Popup.Init(); prior to using it.
i already put Rg.Plugins.Popup.Popup.Init() in appdelegate.cs in IOS
but it still give me same error
what should i do for it?
Simply, you need to call Rg.Plugins.Popup.Popup.Init(); on startup
Andriod Example
namespace HelloXamarinFormsWorld.Android
{
[Activity(Label = "HelloXamarinFormsWorld", MainLauncher = true,
ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// !!!!!! here !!!!!
Rg.Plugins.Popup.Popup.Init(this, bundle);
Xamarin.Forms.Forms.Init(this, bundle);
LoadApplication (new App ());
}
}
}
IOS Example
[Register("AppDelegate")]
public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
{
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
// !!!!!! here !!!!!
Rg.Plugins.Popup.Popup.Init();
global::Xamarin.Forms.Forms.Init ();
LoadApplication (new App ());
return base.FinishedLaunching (app, options);
}
}
Please read the documentation here
https://github.com/rotorgames/Rg.Plugins.Popup/wiki/Getting-started

webview app done in C# for Android using Xamarin for visual studio cannot open whatsapp: url shows net::ERR_UNKNOWN_URL_SCHEME error

I have a webview app done in C# for Android using Xamarin for visual studio. This is supposed to post a message to whatsapp when users click on a button but it shows an error net::ERR_UNKNOWN_URL_SCHEME, please help. the url in question starts with whatsapp://
I have checked this and this one but none of them fits in my code
Here is my full code. am very new to C# for android
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Webkit;
using Android.Widget;
using Android.OS;
namespace IwI
{
[Activity(Label = "IwI", MainLauncher = true, Theme = "#android:style/Theme.NoTitleBar")]
public class MainActivity : Activity
{
WebView web_view;
protected override void OnCreate(Bundle bundle)
{
RequestWindowFeature(WindowFeatures.NoTitle);
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
web_view = FindViewById<WebView>(Resource.Id.webview);
web_view.Settings.JavaScriptEnabled = true;
web_view.SetWebViewClient(new IwIClient());
web_view.LoadUrl("https://jacidd.com/iwi");
}
public class IwIClient : WebViewClient
{
public override bool ShouldOverrideUrlLoading(WebView view, IWebResourceRequest request)
{
view.LoadUrl(request.Url.ToString());
return false;
}
}
public override bool OnKeyDown(Android.Views.Keycode keyCode, Android.Views.KeyEvent e)
{
if (keyCode == Keycode.Back && web_view.CanGoBack())
{
web_view.GoBack();
return true;
}
return base.OnKeyDown(keyCode, e);
}
}
}
Based off of your replies to my comments, you could try changing your ShouldOverrideUrlLoading method to the one below:
public bool ShouldOverrideUrlLoading(WebView view, string url)
{
if (url != null && url.startsWith("whatsapp://"))
{
view.Context.StartActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
return true;
}
else
{
return false;
}
}
It's also worth taking a look at this answer on a different question, it also might help you solve the problem.

Xamarin Android RunOnUiThread() not working after new OnCreate() call

I have a simple app on an android 6.0 API 23 smartphone. The app has a bluetooth barcode scanner attached. the Bluetooth class has a async methode which passes the input string.
Here is my main activity class:
using Android.App;
using Android.OS;
using Android.Widget;
namespace TestOnCreate
{
[Activity(Label = "TestOnCreate", MainLauncher = true, Icon = "#drawable/icon")]
public class MainActivity : Activity
{
private Bluetooth bluetooth = null;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
StartBluetooth();
}
private void StartBluetooth()
{
try
{
bluetooth = new Bluetooth(ProcessBluetoothInput);
bluetooth.StartBluetooth("00:19:01:47:0E:70");
}
catch (System.Exception exc)
{
System.Console.WriteLine(exc);
}
}
public void ProcessBluetoothInput(string text)
{
try
{
RunOnUiThread(() =>
{
FindViewById<TextView>(Resource.Id.textView1).Text = text;
});
}
catch (System.Exception exc)
{
System.Console.WriteLine(exc);
}
}
}
}
the scanner is working and send valid information to the textView1.
Now when OnCreate() is called a second time (if the screen is rotated or the phone was on standby) the connection is lost. ProcessBluetoothInput(string text)
is still called but RunOnUiThread is not working.
What am I doing wrong?
What is the correct why to initialize my varables if OnCreate is called so often?
You can prevent the Activity restarts due to orientation changes.
...an application can also prevent an Activity from being restarted when the orientation changes by setting ConfigurationChanges in the ActivityAttribute as follows:
ConfigurationChanges=Android.Content.PM.ConfigChanges.Orientation | Android.Content.PM.ConfigChanges.ScreenSize
Example:
[Activity(Label = "TestOnCreate", MainLauncher = true, Icon = "#drawable/icon", ConfigurationChanges=Android.Content.PM.ConfigChanges.Orientation | Android.Content.PM.ConfigChanges.ScreenSize)]
public class MainActivity : Activity
{
~~~
}
re: Preventing Activity Restart

Categories