Skip to content

Commit

Permalink
Merge pull request #20 from capacitor-community/appcenter-crashes
Browse files Browse the repository at this point in the history
appcenter-crashes add plugin functions
  • Loading branch information
johnborges authored Jun 12, 2021
2 parents 80a940b + 07b4f0e commit ba3ed8a
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 14 deletions.
34 changes: 33 additions & 1 deletion appcenter-crashes/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# @capacitor-community/appcenter-crashes

App Center Crashes will automatically generate a crash log every time your app crashes. The log is first written to the device's storage and when the user starts the app again, the crash report will be sent to App Center.
App Center Crashes will automatically generate a crash log every time your app crashes. The log is first written to the device's storage and when the user starts the app again, the crash report will be sent to App Center. Collecting crashes works for both beta and live apps, i.e. those submitted to the App Store. Crash logs contain valuable information for you to help fix the crash.

## Install

Expand All @@ -15,6 +15,8 @@ npx cap sync

* [`isEnabled()`](#isenabled)
* [`setEnabled(...)`](#setenabled)
* [`generateTestCrash()`](#generatetestcrash)
* [`hasReceivedMemoryWarningInLastSession()`](#hasreceivedmemorywarninginlastsession)

</docgen-index>

Expand Down Expand Up @@ -55,4 +57,34 @@ The state is persisted in the device's storage across application launches.

--------------------


### generateTestCrash()

```typescript
generateTestCrash() => any
```

Generate a test crash for easy testing of the SDK. This API can only be used in test/beta apps and won't do anything in production apps.

**Returns:** <code>any</code>

**Since:** 0.2.0

--------------------


### hasReceivedMemoryWarningInLastSession()

```typescript
hasReceivedMemoryWarningInLastSession() => any
```

Check if app recieved memory warning in the last session.

**Returns:** <code>any</code>

**Since:** 0.2.0

--------------------

</docgen-api>
8 changes: 8 additions & 0 deletions appcenter-crashes/ios/Plugin/AppCenterCrashesBase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,12 @@ import AppCenterCrashes
public func start() {
AppCenter.startService(Crashes.self)
}

public func generateTestCrash() {
Crashes.generateTestCrash()
}

public func hasReceivedMemoryWarningInLastSession() -> Bool {
return Crashes.hasReceivedMemoryWarningInLastSession
}
}
2 changes: 2 additions & 0 deletions appcenter-crashes/ios/Plugin/AppCenterCrashesPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@
CAP_PLUGIN(CrashesPlugin, "Crashes",
CAP_PLUGIN_METHOD(setEnabled, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(isEnabled, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(generateTestCrash, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(hasReceivedMemoryWarningInLastSession, CAPPluginReturnPromise);
)
15 changes: 8 additions & 7 deletions appcenter-crashes/ios/Plugin/AppCenterCrashesPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,10 @@ public class CrashesPlugin: CAPPlugin {
let config: NSDictionary = AppCenterCapacitorShared.getConfiguration()

// get Crashes config options
let enableInJs = config["CrashesEnableInJs"] as? Bool
let alwaysSendCrashes = config["CrashesAlwaysSend"] as? Bool

if AppCenterCapacitorShared.isSdkConfigured() {

implementation.start()

// disable auto start of Crashes
if enableInJs ?? false {
implementation.enable(false)
}
}
}

Expand All @@ -37,6 +30,14 @@ public class CrashesPlugin: CAPPlugin {

@objc func isEnabled(_ call: CAPPluginCall) {
call.resolve(["value": implementation.isEnabled()])
}

@objc func generateTestCrash(_ call: CAPPluginCall) {
implementation.generateTestCrash()
call.resolve()
}

@objc func hasReceivedMemoryWarningInLastSession(_ call: CAPPluginCall) {
call.resolve(["value": implementation.hasReceivedMemoryWarningInLastSession()])
}
}
4 changes: 2 additions & 2 deletions appcenter-crashes/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@capacitor-community/appcenter-crashes",
"version": "0.1.0",
"description": "App Center Crashes records the state of the app and device and automatically generates a crash log.",
"version": "0.2.0",
"description": "Capacitor plugin for Microsoft AppCenter Crashes.",
"main": "dist/plugin.cjs.js",
"module": "dist/esm/index.js",
"types": "dist/esm/index.d.ts",
Expand Down
26 changes: 23 additions & 3 deletions appcenter-crashes/src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,23 +70,43 @@ export interface CrashesPlugin {
* const { value: enabled } = await Crashes.isEnabled();
*/
isEnabled(): Promise<{value: boolean}>;

/**
* You can enable and disable App Center Crashes at runtime. If you disable it, the SDK won't do any crash reporting for the app.
* The state is persisted in the device's storage across application launches.
* @param {shouldEnable: boolean} options
* @since 0.1.0
* @example
* import Crashes from '@capacitor-community/appcenter-crashes';
*
* await Crashes.enable({shouldEnable: true});
*/
setEnabled(options: {shouldEnable: boolean}): Promise<void>;

/**
* Generate a test crash for easy testing of the SDK. This API can only be used in test/beta apps and won't do anything in production apps.
* @since 0.2.0
* @example
* import Crashes from '@capacitor-community/appcenter-crashes';
*
* await Crashes.generateTestCrash();
*/
generateTestCrash(): Promise<void>;

/**
* Check if app recieved memory warning in the last session.
* @returns {Promise<{value: boolean}>}
* @since 0.2.0
* @example
* import Crashes from '@capacitor-community/appcenter-crashes';
*
* const { value: gotMemWarning } = await Crashes.hasReceivedMemoryWarningInLastSession();
*/
hasReceivedMemoryWarningInLastSession(): Promise<{value: boolean}>;
}

// convert
// export function generateTestCrash(): Promise<void>;
// export function hasCrashedInLastSession(): Promise<boolean>;
// export function hasReceivedMemoryWarningInLastSession(): Promise<boolean>;
// export function lastSessionCrashReport(): Promise<ErrorReport>;
// export function notifyUserConfirmation(userConfirmation: UserConfirmation): void;
// export function setListener(crashesListener: CrashesListener): Promise<void>;
6 changes: 6 additions & 0 deletions appcenter-crashes/src/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ import type { CrashesPlugin } from './definitions';
export class CrashesWeb
extends WebPlugin
implements CrashesPlugin {
hasReceivedMemoryWarningInLastSession(): Promise<{ value: boolean; }> {
throw this.unimplemented('Not supported on web.');
}
generateTestCrash(): Promise<void> {
throw this.unimplemented('Not supported on web.');
}
isEnabled(): Promise<{ value: boolean; }> {
throw this.unimplemented('Not supported on web.');
}
Expand Down
2 changes: 1 addition & 1 deletion example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"dependencies": {
"@capacitor-community/appcenter": "0.3.4",
"@capacitor-community/appcenter-analytics": "0.2.0",
"@capacitor-community/appcenter-crashes": "^0.1.0",
"@capacitor-community/appcenter-crashes": "0.2.0",
"@capacitor/app": "^1.0.0",
"@capacitor/cli": "3.0.0",
"@capacitor/core": "3.0.0",
Expand Down
10 changes: 10 additions & 0 deletions example/src/components/app-crashes/app-crashes.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
:host {
display: block;
}

header {
margin-bottom: 1.25em;
}

section {
margin-top: 1em;
margin-bottom: 2.5em;
}

25 changes: 25 additions & 0 deletions example/src/components/app-crashes/app-crashes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,21 @@ import Crashes from '@capacitor-community/appcenter-crashes';
export class AppCrashes {
/* Flag to toggle entire Crashes service */
@State() enabled: boolean = false
@State() memoryWarning: boolean = false

constructor() {
this.toggleCrashes = this.toggleCrashes.bind(this);
this.crashApp = this.crashApp.bind(this);
}

async componentWillLoad() {
try {
const { value: crashesEnabled } = await Crashes.isEnabled();
const { value: memoryWarning } = await Crashes.hasReceivedMemoryWarningInLastSession();

this.enabled = crashesEnabled
this.memoryWarning = memoryWarning
console.debug(`got mem warning: ${this.memoryWarning}`)
} catch (error) {
console.error(error)
}
Expand All @@ -34,6 +39,14 @@ export class AppCrashes {
}
}

async crashApp() {
try {
await Crashes.generateTestCrash()
} catch (error) {

}
}

render() {
return [
<ion-header>
Expand All @@ -51,7 +64,19 @@ export class AppCrashes {
<ion-label>Enable Analytics</ion-label>
<ion-toggle checked={this.enabled} onIonChange={e => this.toggleCrashes(e)} />
</ion-item>
<ion-list-header lines="full">
<ion-label>Previous Crash Info</ion-label>
</ion-list-header>
<ion-item>
<ion-label>Memory Warning</ion-label>
<ion-note>{this.memoryWarning.toString()}</ion-note>
</ion-item>
</ion-list>
<br />
<section>
<header>Generate Test Crash</header>
<ion-button color="danger" expand="block" onClick={this.crashApp}>Let app crash</ion-button>
</section>
</ion-content>,
];
}
Expand Down

0 comments on commit ba3ed8a

Please sign in to comment.