3

我想使用 Optimizely 来改进 App 中的 A/B 测试。为此,我实现了一个等于这个 repo 的 Xamarin Binding:

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>

其中 optlyprojectid = optly + projectid (在我的 api 密钥中)。

4

1 回答 1

4

这可能有帮助,也可能没有帮助,但 Optimizely 确实有一个基本的 Xamarin 绑定 - 我很想知道这是否有帮助:

绑定文件可以在这里找到

  1. 将您的 SDK 中的 Optimizely 库文件的副本放置到与文件 Optimizely.linkwith.cs 相同的目录中
  2. 该项目现在应该在 Xamarin 中构建并生成一个 .dll 文件
  3. 现在在目标应用程序项目中,或者添加对上述 .dll 文件的引用,或者在解决方案中包含绑定项目并添加对它的项目引用。
  4. 将 com.optimizely URL 方案添加到 plist 文件(根据 xcode 项目)
  5. 添加AppDelegate.FinishedLaunching类似这样的代码:

 

public override bool FinishedLaunching(UIApplication app, NSDictionary options) { // create a new window instance based on the screen size window = new UIWindow(UIScreen.MainScreen.Bounds);

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

}

  1. 添加导出的属性“Window”:(不是 100% 确定是否需要)

    
    

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

  2. AppDelegate.OpenUrl中,这:

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

  3. 对于 ABTesting,C# 不支持 Objective-C Optimizely 标头使用的 C 样式宏,因此我设置了一个静态类来封装我们所有的 A/B 变量。目前我们只有一个整数变量“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

笔记:

由于 Xamarin 库执行的线程检查违反了 Optimizely SDK,绘制 Optimizly 应用内手势可能会使应用程序在调试模式构建中崩溃。(这可能只发生在子类 UIView 类中 - 例如:对我们来说,它在 UIButton 的子类中崩溃)。

并非所有 Optimizely SDK 都是绑定的。有些功能和 API 只需要将适当的签名写入 ApiDefinition.cs 即可支持这些功能。

简化变量实现会很好,这样它就不会那么冗长/重复——也许可以用 C# 反射和属性来驱动它。

祝你好运!

于 2015-09-03T12:37:57.790 回答