Wednesday 18 December 2019

Android 8: Cleartext HTTP traffic not permitted

To apply these various answers to Xamarin.Android, you can use class and assembly level Attributes vs. manually editing the AndroidManifest.xml



Internet permission of course is needed (duh..):




[assembly: UsesPermission(Android.Manifest.Permission.Internet)]


Note: Typically assembly level attributes are added to your AssemblyInfo.cs file, but any file, below the using and above the namespace works.



Then on your Application subclass (create one if needed), you can add NetworkSecurityConfig with a reference to an Resources/xml/ZZZZ.xml file:



#if DEBUG
[Application(AllowBackup = false, Debuggable = true, NetworkSecurityConfig = "@xml/network_security_config")]
#else

[Application(AllowBackup = true, Debuggable = false, NetworkSecurityConfig = "@xml/network_security_config"))]
#endif
public class App : Application
{
public App(IntPtr javaReference, Android.Runtime.JniHandleOwnership transfer) : base(javaReference, transfer) { }
public App() { }

public override void OnCreate()
{
base.OnCreate();

}
}


Create a file in the Resources/xml folder (create the xml folder if needed).



Example xml/network_security_config file, adjust as needed (see other answers)







www.example.com
notsecure.com
xxx.xxx.xxx




You can also use the UsesCleartextTraffic parameter on the ApplicationAttribute:




#if DEBUG
[Application(AllowBackup = false, Debuggable = true, UsesCleartextTraffic = true)]
#else
[Application(AllowBackup = true, Debuggable = false, UsesCleartextTraffic = true))]
#endif

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...