Skip to content
This repository was archived by the owner on Mar 18, 2024. It is now read-only.

Commit bf8beee

Browse files
committed
add callback step
1 parent 141c35d commit bf8beee

File tree

1 file changed

+53
-1
lines changed

1 file changed

+53
-1
lines changed

README.md

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,56 @@ after the dll is compiled, you can use it from the C# project.
6767

6868
## Multiplatform Support
6969

70-
the SDK is only available for Windows, IOS and Android, for this example exists `csharp-cross-platform.csproj` and `source.cs`.
70+
the SDK (C#) is only available for Windows, IOS and Android, for this example exists `csharp-cross-platform.csproj` and `source.cs`.
71+
72+
## AppCenter Callbacks
73+
74+
you may need to call the SDK from the C++ code, for example when you want to send a crash report or log an event.
75+
76+
- in the `C++ project` setup a function that saves the C# callback function pointer into c++ so it can be called from the c++ code , you can check it on `AppCenter.cpp`.
77+
```c++
78+
// the C function pointers
79+
void (*trackEventCallback)(const char *str) = nullptr;
80+
void (*trackEventExtraCallback)(const char *str, const char *data) = nullptr;
81+
// this functions will be called from another method in the c++ code
82+
void setTrackEventCallback(void (*callback)(const char *str)) {
83+
trackEventCallback = callback;
84+
}
85+
void setTrackEventCallback(void (*callback)(const char *str,
86+
const char *data)) {
87+
trackEventExtraCallback = callback;
88+
}
89+
```
90+
- once you have this functions you can call them directly or wrap them like this:
91+
```c++
92+
void trackEvent(std::string event) {
93+
if (trackEventCallback) {
94+
trackEventCallback(event.c_str());
95+
} else {
96+
std::cerr << "Interop::AppCenter::trackEventCallback is nullptr\n";
97+
}
98+
}
99+
100+
void trackEvent(std::string event, Properties properties) {
101+
if (trackEventExtraCallback) {
102+
trackEventExtraCallback(event.c_str(), toJson(properties).c_str());
103+
} else {
104+
std::cerr << "Interop::AppCenter::trackEventExtraCallback is nullptr\n";
105+
}
106+
}
107+
```
108+
- on `AppCenter.hpp` you can define them.
109+
- finally define `setupAppCenterCallbacks` in the `C++ project` like this:
110+
```c++
111+
extern "C"{
112+
// this will setup the callbacks needed by the C++ code
113+
// in order to call the AppCenter.trackEvent() function
114+
myAppAPI void setupAppCenterCallbacks(
115+
void (*trackEventCallback)(const char *str),
116+
void (*trackEventExtraCallback)(const char *str, const char *data)) {
117+
Interop::AppCenter::setTrackEventCallback(trackEventCallback);
118+
Interop::AppCenter::setTrackEventCallback(trackEventExtraCallback);
119+
}
120+
}
121+
```
122+
- on the `C# project` setup a class like the one on `AppCenter.cs` of this project.

0 commit comments

Comments
 (0)