Hey all!
For our new project we need to work with some ad networks, Chartboost especially. While waiting for updates on foreign code from the Fuse team I decided to try integrating Chartboost SDK. It took almost an hour to get it to work including reading the complete documentation and skimming through the code samples. I am a complete noob to Fuse, so I’m sharing this with you so can let me know if I’m doing something really absurd
I started with a native Javascript module which makes foreign calls to Chartboost iOS SDK.
using Fuse;
using Fuse.Scripting;
using Fuse.Reactive;
using Uno.Compiler.ExportTargetInterop;
public class ChartboostModule : NativeModule
{
public ChartboostModule()
{
AddMember(new NativeFunction("Log", (NativeCallback)Log));
AddMember(new NativeFunction("Register", (NativeCallback)Register));
AddMember(new NativeFunction("ShowInterstitial", (NativeCallback)ShowInterstitial));
}
static object Log(Context c, object[] args)
{
foreach (var arg in args) {
string msg = (string)arg;
Log_iOS(msg);
}
return null;
}
static object Register(Context c, object[] args)
{
Log_iOS("Registering Chartboost...");
Register_iOS();
return null;
}
static object ShowInterstitial(Context c, object[] args)
{
Log_iOS("Showing interstitial");
ShowInterstitial_iOS();
return null;
}
[Foreign(Language.ObjC)]
public static extern(iOS) void Log_iOS(string message)
@{
NSLog(@"%@", message);
@}
[Foreign(Language.ObjC)]
public static extern(iOS) void Register_iOS()
@{
uAppDelegate delegate = (uAppDelegate )[[UIApplication sharedApplication] delegate]; [Chartboost startWithAppId:@"4f21c409cd1cb2fb7000001b" appSignature:@"92e2de2fd7070327bdeb54c15a5295309c6fcd2d" delegate:[[UIApplication sharedApplication] delegate]]; @}
[Foreign(Language.ObjC)]
public static extern(iOS) void ShowInterstitial_iOS()
@{
[Chartboost showInterstitial:CBLocationHomeScreen]; @}
}
Here’s the ux calling the module:
<App Theme="Basic" Background="#eeeeeeff">
<ChartboostModule ux:Global="ChartboostModule" />
<JavaScript>
var Log = require("ChartboostModule").Log;
var Register = require("ChartboostModule").Register;
var ShowInterstitial = require("ChartboostModule").ShowInterstitial;
Log("App started.");
Register();
module.exports = {
showAdClick: function(args) {
Log("Show ads!");
ShowInterstitial();
}
}
</JavaScript>
<Button Text="Show Ad" Height="45" Alignment="Bottom" Margin="20,80" Clicked="{showAdClick}" /></App>
Also you need to add Fuse.Scripting to .unoproj.
Build the project for iOS and open the project in XCode.
Add the StoreKit and Chartboost frameworks to the project. At this point if you compile the project you’ll get reference errors because of the missing imports in the auto-generated module file, _root.ChartboostModule.m, named after our native JS module.
I added the following imports:
#import <Chartboost/Chartboost.h>
#import <Chartboost/CBAnalytics.h>
#import <Chartboost/CBInPlay.h>
#include <Uno-iOS/AppDelegate.h>
#import <StoreKit/StoreKit.h>
Question: I couldn’t find a way to include these imports statements in Fuse documentation, maybe I missed it. If there’s a way, please let me know.
_
I imported Uno-iOS/AppDelegate.h because in the native JS module I’m accessing the uAppDelegate to pass it to Chartboost initializer as a delegate:
_
uAppDelegate
delegate = (uAppDelegate *)[[UIApplication sharedApplication] delegate]; [Chartboost startWithAppId:@"4f21c409cd1cb2fb7000001b" appSignature:@"92e2de2fd7070327bdeb54c15a5295309c6fcd2d" delegate:(id<ChartboostDelegate>)delegate];
Another question : I also couldn’t find a way to intercept application didFinishLaunchingWithOptions. If there’s any please let me know
Now if your run the app from XCode and click the Show Ad button, everything will (hopefully) work fine.
Fuse is great!