Freezing on permission request Xamarin.Forms - xamarin

I want to get a user's position, and therefore I'm using James Montemagno's Geolocator Plugin for Xamarin. The author uses this code in the code-behind.cs file; I extracted it to a static class:
public static class GeoService
{
public static Position savedPosition;
public static async Task<Position> GetPosition()
{
var lastknown = await ExecuteLastKnownPosition();
if(lastknown == null)
{
var current = await ExecuteGPSPosition();
if(current != null)
{
return current;
}
else
{
return null;
}
}
return lastknown;
}
private static async Task<Position> ExecuteLastKnownPosition()
{
try
{
var hasPermission = await Utils.CheckPermissions(Permission.Location);
if (!hasPermission)
return null;
var locator = CrossGeolocator.Current;
locator.DesiredAccuracy = 50;
//Progress Ring einfügen
var position = await locator.GetLastKnownLocationAsync();
if (position == null)
{
//Benachrichtigung über null lastknownLocation
//Aufrufen der CurrentPosition Methode
return null;
}
savedPosition = position;
return position;
}
catch (Exception ex)
{
Debug.WriteLine(ex);
await Application.Current.MainPage.DisplayAlert("Uh oh", "Something went wrong, but don't worry we captured for analysis! Thanks.", "OK");
return null;
}
finally
{
//Freigabe der Buttons und Bools
}
}
private static async Task<Position> ExecuteGPSPosition()
{
try
{
var hasPermission = await Utils.CheckPermissions(Permission.Location);
if (!hasPermission)
return null;
var locator = CrossGeolocator.Current;
locator.DesiredAccuracy = 100;
//WarteText/Symbol
var position = await locator.GetPositionAsync(TimeSpan.FromSeconds(15));
if (position == null)
{
//Warnung, dass kein GPS vorhanden ist
return null;
}
savedPosition = position;
return position;
}
catch (Exception ex)
{
await Application.Current.MainPage.DisplayAlert("Uh oh", "Something went wrong, but don't worry we captured for analysis! Thanks.", "OK");
return null;
}
finally
{
//Zurücksetzen von Buttons und Lademodus beenden
}
}
public static async Task<Address> ExecuteTrackingAdress(Position currentPosition)
{
try
{
//if (savedPosition == null)
// return null;
var hasPermission = await Utils.CheckPermissions(Permission.Location);
if (!hasPermission)
return null;
string mapkey = "Ajbb9XXXXXXatUzUg1w9BSXXXXXVUAEuF4P-TSXJpnvl5OpXXXXXXXXX";
var locator = CrossGeolocator.Current;
var addresses = await locator.GetAddressesForPositionAsync(currentPosition, mapkey);
var address = addresses.FirstOrDefault();
if (address == null)
{
Debug.WriteLine("Keine Adresse vorhanden");
}
return address;
}
catch (Exception ex)
{
await Application.Current.MainPage.DisplayAlert("Uh oh", "Something went wrong, but don't worry we captured for analysis! Thanks.", "OK");
return null;
}
finally
{
}
}
}
Now I got a ContentPage, and when I go to that page (PushModalAsync) the constructor in the view model calls the GetPosition() method. There's a permission Task and whenever the Task is running, UWP offers me to prompt the location permission.
Unfortunately, from this point on the app is freezing. I can't choose Yes/No, and there's no way of interaction.
I tried to call the methods async, with Task.WhenAll() and so on, but it freezes every time.
This was the last code I wrote in my view model
private async void ExecuteGetPosition()
{
IsBusy = true;
await Task.Yield();
var positiontask = GeoService.GetPosition();
var addresstask = GeoService.ExecuteTrackingAdress(positiontask.Result);
await Task.WhenAll(positiontask, addresstask);
CurrentPosition = positiontask.Result;
CurrentAddress = addresstask.Result;
OnPropertyChanged("CurrentAddressView");
IsBusy = false;
}
I'm assuming that the XAML ContentPage isn't already loaded correctly, and so the prompting-window slips "behind" the MainWindow or something.
Please could you give me an idea for a workaround to handle this?
Edit:
Adding these lines to my App.xaml.cs OnStart()-Method brought the solution. Windows is now calling for permission OnStart, Android asks for permission on gps-request...crazy:
var permission = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Location);

Are you running on iOS? If so, look at the simulator's console log to see if there is a privacy error. I am guessing it has to do with the privacy declarations required to be in the info.plist when requesting location permissions.
Check out all of the required privacy declarations listed here.
They include (check out the link for info on when to add each one):
<key>NSLocationWhenInUseUsageDescription</key>
<string>This app needs access location when open.</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>This app needs access location when in the background.</string>
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>This app needs access location when open and in the background.</string>

Related

Xamarin IOS InAppBiling plugin how to get receipt-data

I use Plugin.InAppBiling for In-App Purchase ios. I want to know Purchase receipt data.
here is my code.
private async Task<bool> MakePurchase(string productId)
{
var billing = CrossInAppBilling.Current;
try
{
var connected = await billing.ConnectAsync();
if (!connected)
{
return false;
}
var verify = DependencyService.Get<IInAppBillingVerifyPurchase>();
var purchase = await CrossInAppBilling.Current.PurchaseAsync(productId, ItemType.InAppPurchase, verify);
if (purchase == null)
{
return false;
}
else if (purchase.State == PurchaseState.Purchased)
{
if (Device.RuntimePlatform == Device.iOS)
{
Console.WriteLine("CHECK");
if(verify == null)
{
Console.WriteLine("null");
}
else
{
Console.WriteLine($"{verify}");
}
}
return true;
}
return false;
}
finally
{
await billing.DisconnectAsync();
}
}
The payment process goes well. but verify is just return null.
I understood verify as receipt-data. It's right?
How do I get a base64 encoded string receipt-data?
The documentation you also link to clearly states that you need to implement IInAppBillingVerifyPurchase yourself. There is no implementation of this in the plugin.
So you need to create your own class and register it in the IoC container:
[assembly: Dependency(typeof(MyIAPVerification))]
public class MyIAPVerification : IInAppBillingVerifyPurchase
{
// implementation here
}
Only then will your verify instance not be null.

WebView File Chooser stops to respond after cancelled selection

we have implement file chooser for web view. it works successfully when attachment is selected, but fails when cancelled without file specification. The file chooser just stops to react on click
any help is appreciated. Thanks
we use chrome client. it works fine if in all cases, file selection is listed. but even from the first file selection is cancelled, no longer file chooser will work. It is Xamarin.Android app based fully on webview
Our code is:
protected override void OnActivityResult(int requestCode, Result resultCode, Intent intent)
{
if (requestCode == FILECHOOSER_RESULTCODE)
{
if (null == _mUploadMessage)
return;
// Check that the response is a good one
if (resultCode == Result.Ok)
{
Android.Net.Uri[] results = null;
if (intent == null)
{
// If there is not data, then we may have taken a photo
if (mCameraPhotoPath != null)
{
results = new Android.Net.Uri[] { Android.Net.Uri.Parse(mCameraPhotoPath) };
}
}
else
{
if (intent.DataString != null)
{
results = new Android.Net.Uri[] { Android.Net.Uri.Parse(intent.DataString) };
}
}
_mUploadMessage.OnReceiveValue(results);
_mUploadMessage = null;
}
}
}
Chrome client:
var chrome = new FileChooserWebChromeClient((uploadMsg) =>
{
_mUploadMessage = uploadMsg;
mCameraPhotoPath = null;
Intent takePictureIntent = new Intent(Android.Provider.MediaStore.ActionImageCapture);
//Create the File where the photo should go
File photoFile = null;
try
{
string folder = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;
photoFile = new File(folder, "image" + DateTime.Now.Millisecond + ".png");
takePictureIntent.PutExtra("PhotoPath", mCameraPhotoPath);
}
catch (IOException ex)
{
// Error occurred while creating the File
System.Console.WriteLine("" + ex.ToString());
}
// Continue only if the File was successfully created
if (photoFile != null)
{
mCameraPhotoPath = "file:" + photoFile.AbsolutePath;
takePictureIntent.PutExtra(Android.Provider.MediaStore.ExtraOutput,
Android.Net.Uri.FromFile(photoFile));
}
else
{
takePictureIntent = null;
}
Intent contentSelectionIntent = new Intent(Intent.ActionGetContent);
contentSelectionIntent.AddCategory(Intent.CategoryOpenable);
contentSelectionIntent.SetType("image/*");
Intent[] intentArray;
if (takePictureIntent != null)
{
intentArray = new Intent[] { takePictureIntent };
}
else
{
intentArray = new Intent[0];
}
Intent chooserIntent = new Intent(Intent.ActionChooser);
chooserIntent.PutExtra(Intent.ExtraIntent, contentSelectionIntent);
chooserIntent.PutExtra(Intent.ExtraTitle, this.GetStringFromResource(Resource.String.chose_photo));
chooserIntent.PutExtra(Intent.ExtraInitialIntents, intentArray);
base.StartActivityForResult(chooserIntent, HarmonyAndroid.AndroidMainActivity.FILECHOOSER_RESULTCODE);
});
return chrome;
Part 2
class FileChooserWebChromeClient : WebChromeClient
{
Action<IValueCallback> callback;
public FileChooserWebChromeClient(Action<IValueCallback> callback)
{
this.callback = callback;
}
public override bool OnShowFileChooser(WebView webView, IValueCallback filePathCallback, FileChooserParams fileChooserParams)
{
callback(filePathCallback);
return true;
}
public override void OnCloseWindow(WebView window)
{
base.OnCloseWindow(window);
}
}
Part 3
webView.ImprovePerformance();
webView.SetWebViewClient(new HomeWebViewClient(customWebViewClientListener, clientId));
webView.SetWebChromeClient(chrome);
webView.Settings.JavaScriptEnabled = true;
webView.Settings.DomStorageEnabled = true;
webView.SetDownloadListener(new CustomDownloadListener(activity, customDownloadListener));
webView.AddJavascriptInterface(new JavaScriptToCSharpCommunication(activity, javaScriptToCSharpCommunicationListener), Constants.JS_CSHARP_COMMUNICATOR_NAME);
Try to give a null object to the uri callback, when the resultCode is not RESULT_OK.
add in your OnActivityResult method:
if (resultCode != Result.Ok)
{
_mUploadMessage.OnReceiveValue(null);
_mUploadMessage = null;
return;
}

Xamarin Forms picker SelectedIndexChange null exception

I have created an application that asks for a username and returns the stats for this user. When a counter is more than 1 it focuses on a picker and asks to choose one of the options. After, the SelectedIndexChanged (Platformselect_SelectedIndexChanged) method is being called and gets the stats. The problem is that if the user enters a username, the first time it works. When the user goes back and re-enters the username it doesn't focus (it doesn't show the picker) and automatically calls the SelectedIndexChanged method with platformselect.SelectedIndex being -1 causing my program to crash. Could you please help me?
I have tried to set focus on the main thread, I have tested it in both iOS and Android and the same problem appears.
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
platformselect.HeightRequest = 0;
this.search.Clicked += Button_Clicked;
}
public async void searchStats(string usernameText)
{
search.IsEnabled = false;
activityIndicator.IsRunning = true;
var id = await GetDataLogic.GetID(usernameText);
publicid = id;
activityIndicator.IsRunning = false;
if (id.uid == "error")
{
await DisplayAlert("Error", "Could not find username: " + usernameText, "OK");
search.IsEnabled = true;
return;
}
else if (id.uid == "errornointernet")
{
await DisplayAlert("Error", "Could not connect to the Internet. Please check your connection and try again.", "OK");
search.IsEnabled = true;
return;
}
else
{
if(id.platforms.Count > 1)
{
platformselect.ItemsSource = publicid.platforms.ToList();
//we have to show alert to choose platform and then call get user stats.
**Device.BeginInvokeOnMainThread(() =>
{
if (platformselect.IsFocused)
platformselect.Unfocus();
platformselect.Focus();
});**
}
else
{
//we call it without asking because there is only one platform.
var userstats = await GetDataLogic.GetStats(id, 0);
await Navigation.PushAsync(new Stats(userstats, favorites));
}
}
}
private async void Platformselect_SelectedIndexChanged(object sender, EventArgs e)
{
//the second time it doesnt show the picker and it automatically enters -1... it returns null exception. Has to be investigated.
var userstats = await GetDataLogic.GetStats(publicid, platformselect.SelectedIndex);
await Navigation.PushAsync(new Stats(userstats, favorites));
}
}
I would like it to always focus on the picker and wait for the response for the user, not automatically call SelectedIndexChanged with -1 as the selectedindex
I suggest change your event handler this way
private async void Platformselect_SelectedIndexChanged(object sender, EventArgs e)
{
//Check for unselection event
if(platformselect.SelectedIndex == -1) return;
var userstats = await GetDataLogic.GetStats(publicid, platformselect.SelectedIndex);
await Navigation.PushAsync(new Stats(userstats, favorites));
}

How to get current location coordinates in the Map feature in Xamarin Application?

I am working on location coordinates marking feature in the App.
I am using following dll's and google map.
Xamarin.Forms.Maps
Xam.Plugin.Geolocator
Xam.Plugin.ExternalMaps
In my iphone simulator, If the Location is NONE.It pulls default location some where in Rome, Italy
Following is the code written to pull the Map ..
public async Task<Xamarin.Forms.Maps.Position> GetPosition()
{
IsBusy = true;
Xamarin.Forms.Maps.Position p;
try
{
if (!locator.IsGeolocationAvailable)
{
p = new Xamarin.Forms.Maps.Position();
}
if (!locator.IsGeolocationEnabled)
{
p = new Xamarin.Forms.Maps.Position();
}
var position = await locator.GetPositionAsync(timeoutMilliseconds: 10000);
p = new Xamarin.Forms.Maps.Position(position.Latitude,position.Longitude);
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
p = new Xamarin.Forms.Maps.Position();
}
IsBusy = false;
return p;
}
public async Task<object> GetCityName(double latitude, double longitude) {
HttpClient client;
client = new HttpClient();
client.MaxResponseContentBufferSize = 256000;
try
{
var response = await client.GetAsync("https://maps.googleapis.com/maps/api/geocode/json?latlng=" + latitude + "," + longitude);
if (response.IsSuccessStatusCode)
{
var result = await response.Content.ReadAsStringAsync();
var json = Newtonsoft.Json.Linq.JObject.Parse(result);
var reValue = "" + json["results"][0]["formatted_address"];
var strArr = reValue.Split(',');
if (strArr.Length > 2)
return strArr[strArr.Length - 2] + ", " +strArr[strArr.Length - 1];
else
return "";
}
else {
Debug.WriteLine(#"Failed.");
return "Failed";
}
}
catch (Exception ex)
{
Debug.WriteLine(#"ERROR {0}", ex.Message);
return ex.Message;
}
}
Image :
elaborate your problem please, and change your conditions as follows:
if (CrossGeolocator.Current.IsGeolocationAvailable)
{
if (CrossGeolocator.Current.IsGeolocationEnabled)
{
var position = await locator.GetPositionAsync(timeoutMilliseconds: 10000);
p = new Xamarin.Forms.Maps.Position(position.Latitude,position.Longitude);
}
else
{
throw new Exception("Geolocation is turned off");
// Geolocation is turned off for the device.
}
}
else
{
throw new Exception("Geolocation is turned off");
// Geolocation not available for device
}
Here now Handle the catches from GetPosition() Method to make sure you don't get the default location.

Azure Notification Hub and WP8 Intermitant notifications

This is a fairly long piece of code but I am getting nowhere with this and cannot see any issues, although I am new to using notification hubs. I am trying to register for targeted notifications (the logged on user) using the notification hub in Azure. After the registration, a test notification is sent.
The issue I am having is that sometimes the notification is sent to the device, and sometimes it is not. It mostly isn't but occasionally when I step through the code on the server, i will get the notification on the emulator come through. Once when I deployed the app to my phone the notification came though on the emulator! I cannot discover a pattern.
My Controller class looks like this;
private NotificationHelper hub;
public RegisterController()
{
hub = NotificationHelper.Instance;
}
public async Task<RegistrationDescription> Post([FromBody]JObject registrationCall)
{
var obj = await hub.Post(registrationCall);
return obj;
}
And the helper class (which is used elsewhere so is not directly in the controller) looks like this;
public static NotificationHelper Instance = new NotificationHelper();
public NotificationHubClient Hub { get; set; }
// Create the client in the constructor.
public NotificationHelper()
{
var cn = "<my-cn>";
Hub = NotificationHubClient.CreateClientFromConnectionString(cn, "<my-hub>");
}
public async Task<RegistrationDescription> Post([FromBody] JObject registrationCall)
{
// Get the registration info that we need from the request.
var platform = registrationCall["platform"].ToString();
var installationId = registrationCall["instId"].ToString();
var channelUri = registrationCall["channelUri"] != null
? registrationCall["channelUri"].ToString()
: null;
var deviceToken = registrationCall["deviceToken"] != null
? registrationCall["deviceToken"].ToString()
: null;
var userName = HttpContext.Current.User.Identity.Name;
// Get registrations for the current installation ID.
var regsForInstId = await Hub.GetRegistrationsByTagAsync(installationId, 100);
var updated = false;
var firstRegistration = true;
RegistrationDescription registration = null;
// Check for existing registrations.
foreach (var registrationDescription in regsForInstId)
{
if (firstRegistration)
{
// Update the tags.
registrationDescription.Tags = new HashSet<string>() {installationId, userName};
// We need to handle each platform separately.
switch (platform)
{
case "windows":
var winReg = registrationDescription as MpnsRegistrationDescription;
winReg.ChannelUri = new Uri(channelUri);
registration = await Hub.UpdateRegistrationAsync(winReg);
break;
case "ios":
var iosReg = registrationDescription as AppleRegistrationDescription;
iosReg.DeviceToken = deviceToken;
registration = await Hub.UpdateRegistrationAsync(iosReg);
break;
}
updated = true;
firstRegistration = false;
}
else
{
// We shouldn't have any extra registrations; delete if we do.
await Hub.DeleteRegistrationAsync(registrationDescription);
}
}
// Create a new registration.
if (!updated)
{
switch (platform)
{
case "windows":
registration = await Hub.CreateMpnsNativeRegistrationAsync(channelUri,
new string[] {installationId, userName});
break;
case "ios":
registration = await Hub.CreateAppleNativeRegistrationAsync(deviceToken,
new string[] {installationId, userName});
break;
}
}
// Send out a test notification.
await SendNotification(string.Format("Test notification for {0}", userName), userName);
return registration;
And finally, my SendNotification method is here;
internal async Task SendNotification(string notificationText, string tag)
{
try
{
var toast = PrepareToastPayload("<my-hub>", notificationText);
// Send a notification to the logged-in user on both platforms.
await NotificationHelper.Instance.Hub.SendMpnsNativeNotificationAsync(toast, tag);
//await hubClient.SendAppleNativeNotificationAsync(alert, tag);
}
catch (ArgumentException ex)
{
// This is expected when an APNS registration doesn't exist.
Console.WriteLine(ex.Message);
}
}
I suspect the issue is in my phone client code, which is here and SubscribeToService is called immediately after WebAPI login;
public void SubscribeToService()
{
_channel = HttpNotificationChannel.Find("mychannel");
if (_channel == null)
{
_channel = new HttpNotificationChannel("mychannel");
_channel.Open();
_channel.BindToShellToast();
}
_channel.ChannelUriUpdated += async (o, args) =>
{
var hub = new NotificationHub("<my-hub>", "<my-cn>");
await hub.RegisterNativeAsync(args.ChannelUri.ToString());
await RegisterForMessageNotificationsAsync();
};
}
public async Task RegisterForMessageNotificationsAsync()
{
using (var client = GetNewHttpClient(true))
{
// Get the info that we need to request registration.
var installationId = LocalStorageManager.GetInstallationId(); // a new Guid
var registration = new Dictionary<string, string>()
{
{"platform", "windows"},
{"instId", installationId},
{"channelUri", _channel.ChannelUri.ToString()}
};
var request = new HttpRequestMessage(HttpMethod.Post, new Uri(ApiUrl + "api/Register/RegisterForNotifications"));
request.Content = new StringContent(JsonConvert.SerializeObject(registration), Encoding.UTF8, "application/json");
string message;
try
{
HttpResponseMessage response = await client.SendAsync(request);
message = await response.Content.ReadAsStringAsync();
}
catch (Exception ex)
{
message = ex.Message;
}
_registrationId = message;
}
}
Any help would be greatly appriciated as I have been stuck on this now for days! I know this is a lot of code to paste up here but it is all relevant.
Thanks,
EDIT: The SubscribeToService() method is called when the user logs in and authenticates with the WebAPI. The method is here;
public async Task<User> SendSubmitLogonAsync(LogonObject lo)
{
_logonObject = lo;
using (var client = GetNewHttpClient(false))
{
var logonString = String.Format("grant_type=password&username={0}&password={1}", lo.username, lo.password);
var sc = new StringContent(logonString, Encoding.UTF8);
var response = await client.PostAsync("Token", sc);
if (response.IsSuccessStatusCode)
{
_logonResponse = await response.Content.ReadAsAsync<TokenResponseModel>();
var userInfo = await GetUserInfoAsync();
if (_channel == null)
SubscribeToService();
else
await RegisterForMessageNotificationsAsync();
return userInfo;
}
// ...
}
}
I have solved the issue. There are tons of fairly poorly organised howto's for azure notification hubs and only one of them has this note toward the bottom;
NOTE:
You will not receive the notification when you are still in the app.
To receive a toast notification while the app is active, you must
handle the ShellToastNotificationReceived event.
This is why I was experiencing intermittent results, as i assumed you would still get a notification if you were in the app. And this little note is pretty well hidden.
Have you used proper tag / tag expressions while register/send the message. Also, Where are you storing the id back from the notification hub. It should be used when you update the channel uri (it will expire).
I would suggest to start from scratch.
Ref: http://msdn.microsoft.com/en-us/library/dn530749.aspx

Resources