A while after I deploy my code to iOS the phone hangs up - xamarin

Is there some way I can track what's happening with Xamarin? I do a debug with a target of my phone and then later it hangs up. I can't do anything, can't shut it down with the button on the side and the only way I can get the phone to work again is by pressing the button on the side and the home button. Running on iPhone 6s Plus.
Here is some code that I suspect might be causing a problem. Would also like to know if anyone can see anything that might cause the problem with the code:
public partial class App : Application
{
public static DataManager db;
private static Stopwatch stopWatch = new Stopwatch();
private const int defaultTimespan = 1;
public App()
{
InitializeComponent();
}
public static DataManager DB
{
get
{
if (db == null)
{
db = new DataManager();
}
return db;
}
}
protected override void OnStart()
{
App.DB.InitData();
MainPage = new Japanese.MainPage();
if (!stopWatch.IsRunning)
stopWatch.Start();
Device.StartTimer(new TimeSpan(0, 0, 1), () =>
{
if (stopWatch.IsRunning && stopWatch.Elapsed.Minutes >= defaultTimespan)
{
Debug.WriteLine("Checking database");
PointChecker.CheckScore();
stopWatch.Restart();
}
return true;
});
}
protected override void OnSleep()
{
Debug.WriteLine("OnSleep");
stopWatch.Reset();
}
protected override void OnResume()
{
Debug.WriteLine("OnResume");
// deductPoints();
stopWatch.Start();
}
}

iOS requires that everything is setup, with 17 seconds, on the initial first load. This means that you must set the MainPage in your App constructor, you can't set it in OnStart.
Or, you can place MainPage = new ContentPage(); in your App constructor, then it will be replaced in OnStart. However, you must set the MainPage, when it's constructing the Application.
Android and UWP I think, give you some freedom, and you can set it in OnStart, but definitely not iOS.

My iPhones are hangs up when I have debugger connected to running app and that connection is interrupted. For example, if you unplug lightning cable while Visual Studio is debugging - the phone will hangs.
So try to start your application from phone(without debugger attached) and check your datacable.

Related

Xamarin UWP : app in front/rear (method unexisting ?)

I have a app in xamarin that listen a folder and do some action when a file is created.
But the thing is that I need to be able to :
bring my app in front when a file is created
push my app in rear when acyion are finished
I have a beginning of response with
Window.Current.Activate();
but that only work if my app is not minimize (app not in first view)
I've already tried this :
IList<AppDiagnosticInfo> infos = await AppDiagnosticInfo.RequestInfoForAppAsync();
IList<AppResourceGroupInfo> resourceInfos = infos[0].GetResourceGroups();
await resourceInfos[0].StartSuspendAsync();
but visual studio tell me
AppResourceGroupInfo don't have a definition for StartSuspendAsync()
but documentation mention it AppResourceGroupInfo.StartSuspendAsync Method
someone have an idea ?
UPDATE [2020-04-29 16:48] :
This is a pcl project
the intention is to use dependency service to have diferent comportement in function of platform (already implemented just need the front/rear
interface in pcl
public interface IWindowManager
{
void Minimize();
void Maximize();
}
in uwp
class WindowManager : IWindowManager
{
public async void Maximize()
{
try
{
Window.Current.Activate();
}
catch(Exception ex)
{
DependencyService.Get<IErrorLogger>().LogError(ex);
}
}
public async void Minimize()
{
IList<AppDiagnosticInfo> infos = await AppDiagnosticInfo.RequestInfoForAppAsync();
IList<AppResourceGroupInfo> resourceInfos = infos[0].GetResourceGroups();
resourceInfos[0].StartSuspendAsync();
}
}
and as I say StartSuspendAsync() encounter some problem
AppResourceGroupInfo don't have a definition for StartSuspendAsync()
Derive from official document, StartSuspendAsync() available in the Windows 10 update 1803 (build 17134). So please edit the UWP project mini version to 17134.
but that only work if my app is not minimize (app not in first view)
For making the app foreground, you could use the following code.
IEnumerable<AppListEntry> appListEntries = await Package.Current.GetAppListEntriesAsync();
await appListEntries.First().LaunchAsync();
Update
If above does not work, please try use register a protocol for UWP app and launch it with Windows.System.Launcher.LaunchUriAsync method.
public async void Maximize()
{
try
{
await Windows.System.Launcher.LaunchUriAsync(new Uri("testapp:"));
}
catch (Exception ex)
{
Debug.Write(ex);
}
}

Requesting Android permissions in a class (Xamarin)

I'm trying to request a permission at runtime for my app. I use a service provider to talk between the portable class and Android.
I start by calling this code on button press in the PCL:
using (new Busy(this))
{
var locationHelper = scope.Resolve<ILocationHelper>();
locationHelper.GetLocation(this);
}
This calls my Android level service:
public class AndroidLocationHelper : ILocationHelper, ILocationListener
{
readonly string[] PermissionsLocation =
{
Manifest.Permission.AccessCoarseLocation
};
const int RequestLocationId = 0;
public void GetLocation(SearchViewModel viewModel)
{
try
{
const string permission = Manifest.Permission.AccessCoarseLocation;
if (((int)Build.VERSION.SdkInt < 23) || (CheckSelfPermission(permission) == Permission.Granted))
{
}
else
RequestPermissions(PermissionsLocation, RequestLocationId);
}
catch (Exception ex)
{
Debug.WriteLine("Error while getting Location service");
Debug.WriteLine(ex.Message);
Messaging.AlertUser("There was an error with determining your location");
}
}
However, I get two errors on CheckSelfPermission and RequestPermissions. These two methods are only available to activities. The code works fine in MainActivity; however, I want to ask for permissions when the user hits a button, not in OnCreate or OnResume, etc.
Thanks for any help.
In your Android project, You can use this and use the Dependency Service to call it in Xamarin.Forms PCL project later:
var thisActivity = Forms.Context as Activity;
ActivityCompat.RequestPermissions(thisActivity, new string[] {
Manifest.Permission.AccessFineLocation }, 1);
ActivityCompat.RequestPermissions(thisActivity,
new String[] { Manifest.Permission.AccessFineLocation },
1);
You can try with ContextCompat.CheckSelfPermission, passing the application context, like this:
ContextCompat.CheckSelfPermission(Android.App.Application.Context, permission)
Update
In case of ActivityCompat.RequestPermissions, which requires an activity reference, you can keep track of the current activity. There is a very handy lib for that, called "CurrentActivityPlugin". You can find at https://github.com/jamesmontemagno/CurrentActivityPlugin
Rafael came up with a solution but I found another option that is a lot less effort just using MessagingCenter. In the MainActivity's OnCreate add a receiver that runs all the location code, that way you have access to all of the activities methods (and there are a bunch of tutorials on doing location services in MainActivity). Then add the Send inside of your service (the class).
To expound Rafael Steil's answer, I tried the suggested CurrentActivityPlugin and it worked on me. In my case I am trying to execute a voice call which needs CALL_PHONE permission. Here is the code snippet in your case: I used the ContextCompat & ActivityCompat so that I don't need to check the VERSION.SdkInt
using Plugin.CurrentActivity;
public void GetLocation(SearchViewModel viewModel){
var context = CrossCurrentActivity.Current.AppContext;
var activity = CrossCurrentActivity.Current.Activity;
int YOUR_ASSIGNED_REQUEST_CODE = 9;
if (ContextCompat.CheckSelfPermission(context, Manifest.Permission.AccessCoarseLocation) == (int)Android.Content.PM.Permission.Granted)
{
//Permission is granted, execute stuff
}
else
{
ActivityCompat.RequestPermissions(activity, new string[] { Manifest.Permission.AccessCoarseLocation }, YOUR_ASSIGNED_REQUEST_CODE);
}
}
It's dead simple
public bool CheckPermission()
{
const string permission = Manifest.Permission.ReceiveSms;
return ContextCompat.CheckSelfPermission(Forms.Context, permission) == (int) Permission.Granted;
}

how to wait for the android activity to fully resume after calling ZXing qr scanner

I have an application which when starting requests a qr code from the user, and according to the qr scanned, a different fragment is loaded in the activity
I am using ZXing mobile scanner to do this
unfortunatelly the scanner returns a reply way before it shuts down and returns to the calling activity
this means that when I call the transaction code to replace the current fragment with the new one, the activity is not yet in the foreground so nothing happens
To solve this I created a ManualResetEvent (I'm using Xamarin, but I will use a Semaphore when I have to convert this to Android Studio) that I set before starting the scan , and then reset in the OnResume part of the activity
this seems to solve the problem, but it feels like there is a much better solution
Is there something I am missing?
thanks in advance for any help you can provide
Edit: the code I am currently using
public class MyActivity : Activity {
ManualResetEvent _has_resumed = new ManualResetEvent(false);
.....
protected override void OnResume() {
base.OnResume();
_has_resumed.Set();
}
......
void scan_qr(Action<string> finished_callback) {
#region initialize the scanner
MobileBarcodeScanner scanner = new MobileBarcodeScanner();
MobileBarcodeScanningOptions options = new MobileBarcodeScanningOptions();
options.UseNativeScanning = true; //use native scan
options.AutoRotate = false;//do not rotate the screen
options.PossibleFormats = new List<BarcodeFormat> { BarcodeFormat.QR_CODE }; // only allow qr_codes;
#endregion
#region perform the actual scan, when it finishes return to the main thread and execute the callback
_has_resumed.Reset();
scanner.Scan(this, options) //do scan
.ContinueWith(result => {
_has_resumed.WaitOne();
return result.Result;
})//wait until the activity has resumed
.ContinueWith((task_result) => { //then return the result
Result result = task_result.Result;
if (result == null) {
show_toast( Resource.String.questions_select_error_no_qr_scanned );
} else {
finished_callback(result.Text);
}
}, System.Threading.Tasks.TaskScheduler.FromCurrentSynchronizationContext());
#endregion
}
}

Xamarin: Does the carousel page load all the pages at one

I new in Xamarin form. On Android, I used ViewPager to load images and the user swipe around the pages. Since Android has adapter, all the views are not initialized at once. Now I want to move to Xamarin form and seeing there is Carousel Page. Does it behave the same as ViewPager only load pages as needed?
Xamarin.Forms CarouselPage does not support UI virtualization (recycling).
Initialization performance and memory usage can be a problem depending upon the number of pages/children.
The new preferred VisualElement to use is the CarouselView that is basically superseding CarouselPage and it has been optimized for each platform.
Blog: Xamarin.Forms CarouselView
Nuget: Xamarin.Forms.CarouselView (Currently in pre-release)
FYI: I just looked the source for for the Android renderer (CarouselViewRenderer.cs) and it does indeed implement RecyclerView...
If you prevent the call to InitializeComponent in the constructor of the page you might have an effect on the load time.
public interface CarouselChildPage {
void childAppearing();
void childDissapearing();
}
public partial class MainPage : CarouselPage {
CarouselPageChild previousPage;
protected override void OnCurrentPageChanged() {
base.OnCurrentPageChanged();
if (previousPage != null)
previousPage.childDissapearing();
int index = Children.IndexOf(CurrentPage);
CarouselPageChild childPage = Children[index] as CarouselPageChild;
childPage.childAppearing();
previousPage = childPage;
}
}
public partial class FriendsListPage : ContentPage, CarouselPageChild {
bool isLoaded = false;
public FriendsListPage() {
// Remove Initialise Component Here
}
public void childAppearing() {
Logger.log("My Appearing");
if (!isLoaded){
InitializeComponent();
isLoaded = true;
}
}
public void childDissapearing() {
Logger.log("My Disappearing");
}
}

Start specific view of Gluon App from a notification

I set up an alarm to show a corresponding Notification. The PendingIntent of the Notification is used to start the Gluon App main class. To show a View other than the homeView, I call switchView(otherView) in the postInit method. OtherView is shown, but without AppBar. While it's possible to make the AppBar appear, I wonder if this is the right approach.
#Override
public void postInit(Scene scene) {
// additional setUp logic
boolean showReadingView = (boolean) PlatformProvider.getPlatform().getLaunchIntentExtra("showReadingView", false);
if (showReadingView) {
switchView(READING_VIEW);
}
}
When triggering anything related to the JavaFX thread from another thread, we have to use Platform.runLater().
Yours is a clear case of this situation: the Android thread is calling some pending intent, and as a result, the app is started again.
This should be done:
#Override
public void postInit(Scene scene) {
// additional setUp logic
boolean showReadingView = (boolean) PlatformProvider.getPlatform().getLaunchIntentExtra("showReadingView", false);
if (showReadingView) {
Platform.runLater(() -> switchView(READING_VIEW));
}
}

Resources