我创建了一个 Xamarin Forms 应用程序,该应用程序使用 Azure Active Directory 对 Azure 移动服务进行身份验证。这适用于我的 WinPhone 应用程序,但不适用于 iOS 设备。它适用于 iOS 模拟器,但不适用于实际设备。我已将其缩小到导致应用程序在设备上崩溃的确切行。它在 AppDelegate.cs 文件中,它是“App.Init(this)”行。它不会抛出任何错误(至少我看不到)。它在模拟器中运行良好,在设备上部署和安装良好。但是,当您单击设备上的应用程序运行它时,它会显示启动画面,然后只是简单地退出。
我遵循了https://docs.microsoft.com/en-us/azure/app-service-mobile/app-service-mobile-xamarin-forms-get-started-users上的文档,该文档非常投入,其他一切都有效.
谁能解释为什么“App.Init(this)”会在 iOS AppDelegate.cs 文件中失败?它正在实现 IAuthenticate(完整代码如下)
[Register("AppDelegate")]
public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate, IAuthenticate
{
//
// 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();
Microsoft.WindowsAzure.MobileServices.CurrentPlatform.Init();
//App.Init(this); //If I uncomment this line, the app crashes. Leaving this line commented, the app doesn't every do the login/authentication.
LoadApplication(new App());
return base.FinishedLaunching(app, options);
}
// Define a authenticated user.
private MobileServiceUser user;
public async Task<bool> Authenticate()
{
var success = false;
var message = string.Empty;
try
{
// Sign in with Facebook login using a server-managed flow.
if (user == null)
{
user = await FormDataManager.DefaultManager.CurrentClient
.LoginAsync(UIApplication.SharedApplication.KeyWindow.RootViewController,
MobileServiceAuthenticationProvider.WindowsAzureActiveDirectory);
if (user != null)
{
message = string.Format("You are now signed-in as {0}.", user.UserId);
success = true;
}
}
}
catch (Exception ex)
{
message = ex.Message;
}
// Display the success or failure message.
UIAlertView avAlert = new UIAlertView("Sign-in result", message, null, "OK", null);
avAlert.Show();
return success;
}
}