用Xamarin Binding启动Optimizely时出错。

3

我希望使用Optimizely来改进应用程序中的A/B测试。为此,我已经实现了一个类似于这个repo的Xamarin绑定:

https://github.com/JustGiving/XamarinBindings/tree/master/Optimizely.iOS

当我在FinishedLaunching中注册我的API密钥时,采用以下方式:
static readonly string OptimizelyAPIToken = "mykey~projectid";

public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
{
      // create a new window instance based on the screen size
      Window = new UIWindow(UIScreen.MainScreen.Bounds);
      root = new WelcomeController();
      nav = new UINavigationController(root);

      // If you have defined a root view controller, set it here:
      Window.RootViewController = nav;

      // make the window visible
      Window.MakeKeyAndVisible();

      OptimizelyiOS.Optimizely.SharedInstance().VerboseLogging = true;

      OptimizelyiOS.Optimizely.StartOptimizelyWithAPIToken(OptimizelyAPIToken, launchOptions ?? new NSDictionary());


      return true;
}

我有这个问题

2015-09-01 11:57:23.588 MyAppOptm[3540:750731] [Optimizely Logging]: (ERROR) NSInvalidArgumentException: Stack Trace:
(
0 CoreFoundation 0x22787fa7 + 150
1 libobjc.A.dylib 0x310fec8b objc_exception_throw + 38
2 CoreFoundation 0x2278d3a9 + 0
3 CoreFoundation 0x2278b15f + 354
4 CoreFoundation 0x226baba8 CF_forwarding_prep_0 + 24
5 MyAppOptm 0x011e32f5 -[Optimizely registerGestureRecognizer] + 176
6 Foundation 0x234c136d __NSFireDelayedPerform + 468
7 CoreFoundation 0x2274dcbf + 14
8 CoreFoundation 0x2274d83b + 650
9 CoreFoundation 0x2274ba8b + 1418
10 CoreFoundation 0x22697f31 CFRunLoopRunSpecific + 476
11 CoreFoundation 0x22697d43 CFRunLoopRunInMode + 106
12 Foundation 0x2340713d + 264
13 MyAppOptm 0x011e24f9 -[Optimizely waitForNetworkWithTimeout:condition:] + 432
14 MyAppOptm 0x011e22af -[Optimizely waitForNetworkWithTimeout] + 274
15 MyAppOptm 0x011e181f -[Optimizely startOptimizelyWithToken:launchOptions:experimentsLoadedCallback:] + 1578
16 MyAppOptm 0x011dd8c1 +[Optimizely startOptimizelyWithAPIToken:launchOptions:experimentsLoadedCallback:] + 164
17 MyAppOptm 0x011dd811 +[Optimizely startOptimizelyWithAPIToken:launchOptions:] + 52
18 MyAppOptm 0x00e8006c wrapper_managed_to_native_ApiDefinition_Messaging_void_objc_msgSend_IntPtr_IntPtr_intptr_intptr_intptr_intptr + 236
19 MyAppOptm 0x00e76b44 OptimizelyiOS_Optimizely_StartOptimizelyWithAPIToken_string_Foundation_NSDictionary + 480
20 MyAppOptm 0x00091ad4 JR_UI_Touch_AppDelegate_FinishedLaunching_UIKit_UIApplication_Foundation_NSDictionary + 2448
21 MyAppOptm 0x00433fdc wrapper_runtime_invoke_object_runtime_invoke_dynamic_intptr_intptr_intptr_intptr + 256
22 MyAppOptm 0x012533f7 mono_jit_runtime_invoke + 1190
23 MyAppOptm 0x0129fd89 mono_runtime_invoke + 88
24 MyAppOptm 0x00ff556d native_to_managed_trampoline_4 + 420
25 MyAppOptm 0x00ffbac5 -[AppDelegate application:didFinishLaunchingWithOptions:] + 100
26 UIKit 0x25e750b3 + 374
27 UIKit 0x2606b929 + 2444
28 UIKit 0x2606dfe9 + 1412
29 UIKit 0x26078c69 + 36
30 UIKit 0x2606c78b + 130
31 FrontBoardServices 0x29328ec9 + 16
32 CoreFoundation 0x2274ddb5 + 12
33 CoreFoundation 0x2274d079 + 216
34 CoreFoundation 0x2274bbb3 + 1714
35 CoreFoundation 0x22697f31 CFRunLoopRunSpecific + 476
36 CoreFoundation 0x22697d43 CFRunLoopRunInMode + 106
37 UIKit 0x25e6ec87 + 558
38 UIKit 0x25e69879 UIApplicationMain + 1440
39 MyAppOptm 0x0022f1f4 wrapper_managed_to_native_UIKit_UIApplication_UIApplicationMain_int_string___intptr_intptr + 272
40 MyAppOptm 0x001aa9d4 UIKit_UIApplication_Main_string___intptr_intptr + 52
41 MyAppOptm 0x001aa994 UIKit_UIApplication_Main_string___string_string + 204
42 MyAppOptm 0x000909c0 JR_UI_Touch_Application_Main_string_ + 172
43 MyAppOptm 0x00433fdc wrapper_runtime_invoke_object_runtime_invoke_dynamic_intptr_intptr_intptr_intptr + 256
44 MyAppOptm 0x012533f7 mono_jit_runtime_invoke + 1190
45 MyAppOptm 0x0129fd89 mono_runtime_invoke + 88
46 MyAppOptm 0x012a34c3 mono_runtime_exec_main + 282
47 MyAppOptm 0x012a3305 mono_runtime_run_main + 476
48 MyAppOptm 0x0123d769 mono_jit_exec + 48
49 MyAppOptm 0x0130a0c8 xamarin_main + 2184
50 MyAppOptm 0x0100c269 main + 112
51 libdyld.dylib 0x316b0aaf + 2
)

我的 Optimizely 的 Info.plist 节点是:

        <dict>
            <key>CFBundleURLName</key>
            <string>com.optimizely</string>
            <key>CFBundleURLSchemes</key>
            <array>
                <string>optlyprojectid</string>
            </array>
            <key>CFBundleURLTypes</key>
            <string>Editor</string>
            <key>CFBundleURLIconFile</key>
            <string>Images.xcassets/AppIcons.appiconset/Icon-Small@2x</string>
        </dict>

在我的 API 密钥中,optlyprojectid = optly + projectid。

1个回答

4
这可能有所帮助,但Optimizely确实有一个基本的Xamarin绑定 - 我很想知道这是否有所帮助:
可以在此处找到绑定文件:这里 1. 将来自SDK的Optimizely库文件的副本放置在与Optimizely.linkwith.cs文件相同的目录中 2. 项目现在应在Xamarin中构建并生成.dll文件 3. 现在,在目标应用程序项目中,要么添加对上述.dll文件的引用,要么将绑定项目包含在解决方案中并向其添加项目引用。 4. 将com.optimizely URL方案添加到plist文件中(按照xcode项目) 5. 在AppDelegate.FinishedLaunching中添加类似于以下代码的代码:

 

公共覆盖布尔值FinishedLaunching(UIApplication app, NSDictionary options) { // 根据屏幕大小创建一个新的窗口实例 window = new UIWindow(UIScreen.MainScreen.Bounds);

ABTesting.Preregister();
Optimizely.Optimizely.SharedInstance.VerboseLogging = true;
Optimizely.Optimizely.StartWithAPIToken("***YOURTOKEN***", options);

}

  1. Add an exported property "Window": (not 100% sure if this is needed)

    
    

    [Export("window")] UIWindow window { get; set; }

  2. In AppDelegate.OpenUrl, this:

    
    public override bool OpenUrl(UIApplication application, NSUrl url, string sourceApplication, NSObject annotation)
    {
        if (Optimizely.Optimizely.HandleOpenUrl(url))
            return true;
    }
    

  3. For ABTesting, C# doesn't support C-style macros as used by the Objective-C Optimizely headers, so I've set up a static class to encapsulate all our A/B variables. Currently we have just one integer variable "onboardingExperience".


public static class ABTesting
    {
        public static void Preregister()
        {
            Optimizely.Optimizely.Preregister(_onboardingExperienceKey);
        }

    public static int OnboardingExperience
    {
        get
        {
            return Optimizely.Optimizely.NumberForKey(_onboardingExperienceKey).IntValue;
        }
    }

    static OptimizelyVariableKey _onboardingExperienceKey = 
        OptimizelyVariableKey.Create("onboardingExperience", NSNumber.FromInt32(0));

}
  1. 然后,在其他地方我们只需要引用ABTesting.OnboardingExperience

备注:

在 Debug 模式下,通过绘制 Optimizly 的应用手势可能会导致应用程序崩溃,原因是由 Optimizely SDK 违反了 Xamarin 库执行的线程检查。 (这可能仅针对子类化的 UIView 类 - 例如:对于我们来说,它会在 UIButton 的子类中崩溃)。

并非所有 Optimizely SDK 都被绑定。有些功能和 API 需要在 ApiDefinition.cs 中编写相应的签名才能支持这些功能。

希望可以简化变量实现方式,使其不那么冗长重复 - 或许可以使用 C# 反射和属性来驱动它。

祝好运!


1
嗨@ Joel Balmer,报告的绑定可能是指旧版本的Optimizely SDK(必须说界面也没有更改,这就是为什么我的和您的几乎完全相同)。 帮助我解决崩溃的是第6点! 添加它后,我的应用程序不会崩溃!现在我正在检查Optimizely SDK是否正常工作。 但这解决了发布的问题,现在我将验证绑定的好处。 真的很感谢!路易吉 - Luigi Saggese
嗨@LuigiSaggese - 我是来自JustGiving的Mark。感谢您帮助我们测试Optimizely Binding - 我们也正在努力将其纳入我们的应用程序中,目前正在解决一些代码块问题。如果我们发现API定义等方面有任何变化,我们将更新我们的绑定!如果您自己发现任何错误修复,请告诉我们! - Mark Gibaud

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接