[英]xamarin forms clear session ios xamarin forms
我試圖用此代碼清除cookie,我不明白為什么它不起作用。
我已經嘗試過了:
if (Device.RuntimePlatform == Device.iOS)
我認為我必須包括
using Foundation;
但如果我將其放在指令部分中,則會顯示錯誤。 我需要知道如何僅在ios上執行此代碼,現在我顯然可以正常運行它,但它不會清除cookie。 我不知道如何特別指向要執行的文件。
這是我的代碼:
using System;
using System.Collections.Generic;
using System.Net.Http;
using app_app.Helpers;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using static app_app.MainPage;
#if __IOS__
using Foundation;
using UIKit;
#endif
namespace app_app
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class ActualizarDatos : ContentPage
{
private MainPage.loginInfo userInfo;
public ActualizarDatos(loginInfo userInfo)
{
InitializeComponent();
this.userInfo = userInfo;
nombre.Text = userInfo.nombre;
apellido.Text = userInfo.apellido;
email.Text = userInfo.email;
cedula.Text = userInfo.cedula;
nombreHeader.Text = userInfo.nombre + " " + userInfo.apellido;
}
private async void btnCambiar_Clicked(object sender, EventArgs e)
{
await Navigation.PushAsync(new CambiarContrasena(userInfo) { Title = "app Cambiar Contraseña" });
}
public void DeleteAllCookiesForSite()
{
#if __IOS__
NSHttpCookieStorage storage = NSHttpCookieStorage.SharedStorage;
foreach (NSHttpCookie cookie in storage.Cookies)
{
if (cookie.Domain == ".facebook.com")
{
storage.DeleteCookie(cookie);
}
}
NSUserDefaults.StandardUserDefaults.Synchronize();
#endif
}
private async void btnCerrar_Clicked(object sender, EventArgs e)
{
userInfo = null;
DeleteAllCookiesForSite();
Settings.IsLoggedIn = false;
await Navigation.PushAsync(new MainPage());
}
private async void Actualizar_Clicked(object sender, EventArgs e) {
if (string.IsNullOrEmpty(nombre.Text))
{
await DisplayAlert("Error", "Debe ingresar un nombre válido", "Aceptar");
nombre.Focus();
return;
}
if (string.IsNullOrEmpty(apellido.Text))
{
await DisplayAlert("Error", "Debe ingresar un apellido válido", "Aceptar");
apellido.Focus();
return;
}
if (string.IsNullOrEmpty(cedula.Text))
{
await DisplayAlert("Error", "Debe ingresar una cédula válida", "Aceptar");
cedula.Focus();
return;
}
if (string.IsNullOrEmpty(email.Text))
{
await DisplayAlert("Error", "Debe ingresar un email", "Aceptar");
email.Focus();
return;
}
if (proyecto.SelectedIndex == -1) {
await DisplayAlert("Error", "Debe indicar la linea de negocios de su preferencia", "Aceptar");
return;
}
/*if (adquirio_proyecto.SelectedIndex == -1)
{
await DisplayAlert("Error", "Debe indicar si ha adquirido proyectos", "Aceptar");
return;
}*/
if (recibir_notificaciones.SelectedIndex == -1)
{
await DisplayAlert("Error", "Debe indicar si desea recibir notificaciones", "Aceptar");
return;
}
btnActualizaUsuario.IsEnabled = false;
try
{
var uri = new Uri("https://www.app.com.co/app/service.php");
//await DisplayAlert("debug", userInfo.id, "Aceptar");
var formContent = new FormUrlEncodedContent(new[]{
new KeyValuePair<string, string>("id", userInfo.id),
new KeyValuePair<string, string>("nombre", nombre.Text),
new KeyValuePair<string, string>("apellido", apellido.Text),
new KeyValuePair<string, string>("cedula", cedula.Text),
new KeyValuePair<string, string>("email", email.Text),
//new KeyValuePair<string, string>("adquirio_proyecto", adquirio_proyecto.Items[adquirio_proyecto.SelectedIndex]),
new KeyValuePair<string, string>("proyecto", proyecto.Items[proyecto.SelectedIndex]),
new KeyValuePair<string, string>("recibir_notificaciones", recibir_notificaciones.Items[recibir_notificaciones.SelectedIndex]),
new KeyValuePair<string, string>("task", "actualizaUsuario")
});
var myHttpClient = new HttpClient();
myHttpClient.DefaultRequestHeaders.TryAddWithoutValidation("Accept", "text/html,application/xhtml+xml,application/xml");
myHttpClient.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:19.0) Gecko/20100101 Firefox/19.0");
myHttpClient.DefaultRequestHeaders.TryAddWithoutValidation("Accept-Charset", "ISO-8859-1");
var response = await myHttpClient.PostAsync(uri.ToString(), formContent);
var stringContent = await response.Content.ReadAsStringAsync();
//await DisplayAlert("debug", stringContent, "Aceptar");
switch (stringContent)
{
case "success":
stringContent = "";
var BienV = "Sus datos han sido actualizados debes iniciar sesión para ver los cambios";
await DisplayAlert("Registro Éxitoso", BienV, "Aceptar");
userInfo = null;
await Navigation.PushAsync(new MainPage());
break;
case "failed":
var VuelveA = "Algo salio mal, intente mas tarde";
await DisplayAlert("Registro no exitoso Incorrecto", VuelveA, "Aceptar");
email.Text = string.Empty;
btnActualizaUsuario.IsEnabled = true;
stringContent = "";
break;
case "exist":
var Existe = "Su correo se encuentra registrado";
await DisplayAlert("Registro no exitoso Incorrecto", Existe, "Aceptar");
email.Text = string.Empty;
btnActualizaUsuario.IsEnabled = true;
stringContent = "";
break;
}
}
catch (Exception ex)
{
var Msj = ex.Message;
await DisplayAlert("Error", Msj, "Aceptar");
return;
}
}
}}
提前致謝
我使用了依賴注入,因此在我的可移植項目中,我稱之為
//required for iOS
DependencyService.Get<IPlatform>().DeleteAllCookies();
iPlatform在哪里
public interface IPlatform
{
void DeleteAllCookies();
}
然后在iOS項目的platform.cs中
internal class Platform : IPlatform
{
public void DeleteAllCookies()
{
foreach (var c in NSHttpCookieStorage.SharedStorage.Cookies)
{
NSHttpCookieStorage.SharedStorage.DeleteCookie(c);
}
}
}
在Android項目中,該函數為空:
public void DeleteAllCookies()
{
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.