在iOS iPhone设备上测试xamarin应用时出错

本文关键字:xamarin 应用 出错 测试 iOS iPhone | 更新日期: 2023-09-27 18:14:00

我已经得到了这段代码。

    SecRecord rec = new SecRecord (SecKind.GenericPassword);
    rec.Generic = NSData.FromString ("pass");
    SecStatusCode res;
    SecRecord match = SecKeyChain.QueryAsRecord (rec, out res);
    if (res == SecStatusCode.Success)
        txtLogin.Text = match.ValueData.ToString ();
    SecRecord login = new SecRecord (SecKind.GenericPassword) {
        Generic = NSData.FromString ("login") 
    };
    SecStatusCode reslogin;
    SecRecord matchlogin = SecKeyChain.QueryAsRecord (login, out reslogin);
    if (reslogin == SecStatusCode.Success)
        txtPassword.Text = matchlogin.ValueData.ToString ();

当我在iPhone模拟器上调试时,一切都很好,但是当我开始在iPhone上调试时,它给了我一个错误。

System.InvalidCastException: Cannot cast from source type to destination type.
at MonoTouch.Foundation.NSMutableDictionary.LowlevelFromObjectAndKey (IntPtr obj, IntPtr key) [0x00000] in /Developer/MonoTouch/Source/maccore/src/Foundation/.pmcs-compat.NSMutableDictionary.cs:408
at MonoTouch.Security.SecRecord..ctor (SecKind secKind) [0x0002d] in /Developer/MonoTouch/Source/maccore/src/Security/.pmcs-compat.Items.cs:619
at CompanyIOS.LoginController.ViewDidLoad () [0x00008] in /Users/nickolas/Projects/CompanyWorkSolution/CompanyIOS/LoginController.cs:38
at at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
at MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0004c] in /Developer/MonoTouch/Source/monotouch/src/UIKit/.pmcs-compat.UIApplication.cs:38
at CompanyIOS.Application.Main (System.String[] args) [0x00008] in /Users/nickolas/Projects/CompanyWorkSolution/CompanyIOS/Main.cs:17

我不明白我错过了什么。请帮我找出问题在哪里。

在iOS iPhone设备上测试xamarin应用时出错

问题可能与使用"Generic"属性有关。这是我之前对访问钥匙链的回答。下面是我的示例代码:

        SecRecord existingRec = new SecRecord (SecKind.GenericPassword) { 
            Service = Keychain.USER_SERVICE, 
            Label = Keychain.USER_LABEL 
        };
        var record = new SecRecord (SecKind.GenericPassword) {
            Service = Keychain.USER_SERVICE, 
            Label = Keychain.USER_LABEL, 
            Account = username,
            ValueData = NSData.FromString (password),
            Accessible = SecAccessible.Always
        };
        SecStatusCode code = SecKeyChain.Add (record);
        if (code == SecStatusCode.DuplicateItem) {
            code = SecKeyChain.Remove (existingRec);
            if (code == SecStatusCode.Success)
                code = SecKeyChain.Add (record);
        }

你还可以显示哪一行是第38行在LoginController,因为这是它抛出异常的地方?

我在MonoDevelop中也得到了这个错误,我所做的一切都无法解决它。我最终将Keychain.cs的源代码复制粘贴到我的解决方案中(您需要在构建设置中启用不安全代码),并且瞧,它起作用了:

var uri = new Uri("http://www.contoso.com");
MonoDevelop.MacInterop.Keychain.AddInternetPassword(uri, "ohad", "mypasswrd");
var pass = MonoDevelop.MacInterop.Keychain.FindInternetPassword(uri);
Console.WriteLine("Password: " + pass); //prints "mypasswrd"