Skip to content
This repository was archived by the owner on Dec 14, 2023. It is now read-only.

Commit af1dcd9

Browse files
authored
Appdelegate (#14)
* better appdelegate
1 parent 0c8ed2b commit af1dcd9

File tree

7 files changed

+110
-76
lines changed

7 files changed

+110
-76
lines changed

README.md

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -89,44 +89,14 @@ If you want to use Paypal & Venmo then you will need to edit your app `Info.plis
8989
```
9090
This scheme must start with your app's Bundle ID and be dedicated to Braintree app switch returns. For example, if the app bundle ID is `com.yourcompany.yourapp`, then your URL scheme could be `com.yourcompany.yourapp.payments` or `com.yourcompany.yourapp.anything`. Above I used `org.nativescript.demo.payments` because app's bundle ID is `org.nativescript.demo` & we will need this value bellow.
9191

92-
Now open your `app.ts` or `main.ts` (for Angular) file under `app` directory. If you are using webpack for angular then it will be `main.aot.ts`. Add following lines before `application.start({ moduleName: "main-page" });` or `platformNativeScriptDynamic().bootstrapModule(AppModule);` or `platformNativeScript().bootstrapModuleFactory(AppModuleNgFactory);`
92+
Now open your `app.ts` or `main.ts` (for Angular) file. Add following lines before `application.start({ moduleName: "main-page" });` or `platformNativeScriptDynamic().bootstrapModule(AppModule);` (Angular).
9393

9494
```
9595
import * as app from "application";
96-
declare var UIResponder, UIApplicationDelegate, BTAppSwitch;
96+
import { setupBraintreeAppDeligate } from "nativescript-braintree"
9797
9898
if (app.ios) {
99-
100-
class MyDelegate extends UIResponder {
101-
102-
public static ObjCProtocols = [UIApplicationDelegate];
103-
104-
applicationDidFinishLaunchingWithOptions(application, launchOptions): boolean {
105-
106-
try {
107-
BTAppSwitch.setReturnURLScheme("org.nativescript.demo.payments"); // should be same as CFBundleURLSchemes value.
108-
return true;
109-
} catch (error) {
110-
console.log(error);
111-
}
112-
return false;
113-
}
114-
115-
applicationOpenURLSourceApplicationAnnotation(application, url, sourceApplication, annotation) {
116-
117-
try {
118-
if (url.scheme == "org.nativescript.demo.payments") {
119-
BTAppSwitch.handleOpenURLSourceApplication(url, sourceApplication);
120-
return true;
121-
}
122-
} catch (error) {
123-
console.log(error);
124-
}
125-
return false;
126-
}
127-
}
128-
129-
app.ios.delegate = MyDelegate;
99+
setupBraintreeAppDeligate("org.nativescript.demo.payments");
130100
}
131101
```
132102
Example:

demo/app/app.ts

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,8 @@
11
import * as application from 'tns-core-modules/application';
2-
3-
declare var UIResponder, UIApplicationDelegate, BTAppSwitch;
2+
import { setupBraintreeAppDeligate } from "nativescript-braintree";
43

54
if (application.ios) {
6-
7-
class MyDelegate extends UIResponder {
8-
9-
public static ObjCProtocols = [UIApplicationDelegate];
10-
11-
applicationDidFinishLaunchingWithOptions(application, launchOptions): boolean {
12-
13-
try {
14-
15-
BTAppSwitch.setReturnURLScheme("org.nativescript.demo.payments");
16-
return true;
17-
18-
} catch (error) {
19-
console.log(error);
20-
}
21-
22-
return false;
23-
}
24-
25-
applicationOpenURLSourceApplicationAnnotation(application, url, sourceApplication, annotation) {
26-
27-
try {
28-
if (url.scheme === "org.nativescript.demo.payments") {
29-
BTAppSwitch.handleOpenURLSourceApplication(url, sourceApplication);
30-
return true;
31-
}
32-
} catch (error) {
33-
console.log(error);
34-
}
35-
36-
return false;
37-
}
38-
}
39-
40-
application.ios.delegate = MyDelegate;
5+
setupBraintreeAppDeligate("org.nativescript.demo.payments");
416
}
427

438
application.start({ moduleName: "main-page" });

demo/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
{
22
"nativescript": {
33
"id": "org.nativescript.demo",
4-
"tns-ios": {
5-
"version": "4.2.0"
6-
},
74
"tns-android": {
85
"version": "5.0.0"
6+
},
7+
"tns-ios": {
8+
"version": "5.0.0"
99
}
1010
},
1111
"dependencies": {

src/braintree.ios.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import { Observable } from 'tns-core-modules/data/observable';
22
import * as utils from "tns-core-modules/utils/utils";
3+
const setupAppDeligate = require('./getappdelegate').setupAppDeligate;
34

4-
declare var BTDropInRequest, BTDropInController, UIApplication, PPDataCollector;
5+
declare const BTDropInRequest, BTDropInController, UIApplication, PPDataCollector;
6+
7+
export function setupBraintreeAppDeligate(urlScheme) {
8+
setupAppDeligate(urlScheme);
9+
}
510

611
export class Braintree extends Observable {
712

src/getappdelegate.ts

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import * as application from 'tns-core-modules/application';
2+
declare const BTAppSwitch;
3+
4+
/**
5+
* This file has been copied from nativescript-urlhandler
6+
* https://github.com/hypery2k/nativescript-urlhandler/blob/45fa7d83d59897db8f3b5077432a154ae542a69e/src/getappdelegate.ts
7+
*/
8+
9+
/**
10+
* The following function is part of the NativeScript plugin: nativescript-plugin-firebase
11+
* It is licensed under the MIT license. The license can be found at the bottom of this file.
12+
* Copyright (c) EddyVerbruggen
13+
*
14+
* This function is an unmodified copy of the original function that can be found at:
15+
* https://github.com/EddyVerbruggen/nativescript-plugin-firebase/blob/78f60f55be30b022690722006e1080b1685548fa/src/firebase.ios.ts#L469
16+
*/
17+
18+
export function getAppDelegate() {
19+
// Play nice with other plugins by not completely ignoring anything already added to the appdelegate
20+
if (application.ios.delegate === undefined) {
21+
class UIApplicationDelegateImpl extends UIResponder implements UIApplicationDelegate {
22+
public static ObjCProtocols = [UIApplicationDelegate];
23+
24+
static new(): UIApplicationDelegateImpl {
25+
return <UIApplicationDelegateImpl>super.new();
26+
}
27+
}
28+
29+
application.ios.delegate = UIApplicationDelegateImpl;
30+
}
31+
32+
return application.ios.delegate;
33+
}
34+
35+
// copied from https://github.com/hypery2k/nativescript-urlhandler/blob/45fa7d83d59897db8f3b5077432a154ae542a69e/src/urlhandler.ios.ts#L7
36+
37+
export function enableMultipleOverridesFor(classRef, methodName, nextImplementation) {
38+
const currentImplementation = classRef.prototype[methodName];
39+
classRef.prototype[methodName] = function () {
40+
const result = currentImplementation && currentImplementation.apply(currentImplementation, Array.from(arguments));
41+
return nextImplementation.apply(nextImplementation, Array.from(arguments).concat([result]));
42+
};
43+
}
44+
45+
export function setupAppDeligate(urlScheme) {
46+
47+
let appDelegate = getAppDelegate();
48+
49+
enableMultipleOverridesFor(appDelegate, 'applicationDidFinishLaunchingWithOptions', function (application, launchOptions) {
50+
try {
51+
BTAppSwitch.setReturnURLScheme(urlScheme);
52+
return true;
53+
} catch (error) {
54+
console.log(error);
55+
}
56+
return false;
57+
});
58+
59+
enableMultipleOverridesFor(appDelegate, 'applicationOpenURLSourceApplicationAnnotation', function (application, url, sourceApplication, annotation) {
60+
try {
61+
if (url.scheme === urlScheme) {
62+
BTAppSwitch.handleOpenURLSourceApplication(url, sourceApplication);
63+
return true;
64+
}
65+
} catch (error) {
66+
console.log(error);
67+
}
68+
return false;
69+
});
70+
}
71+
72+
73+
/*
74+
The MIT License (MIT)
75+
Permission is hereby granted, free of charge, to any person obtaining a copy
76+
of this software and associated documentation files (the "Software"), to deal
77+
in the Software without restriction, including without limitation the rights
78+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
79+
copies of the Software, and to permit persons to whom the Software is
80+
furnished to do so, subject to the following conditions:
81+
The above copyright notice and this permission notice shall be included in all
82+
copies or substantial portions of the Software.
83+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
84+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
85+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
86+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
87+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
88+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
89+
SOFTWARE.
90+
*/

src/index.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import { Observable } from 'tns-core-modules/data/observable';
2+
3+
export declare function setupBraintreeAppDeligate(urlScheme: any): void;
4+
25
export declare class Braintree extends Observable {
36
constructor();
47
output: {
@@ -12,6 +15,7 @@ export declare class Braintree extends Observable {
1215
private callIntent(intent);
1316
private handleResults(requestCode, resultCode, data);
1417
}
18+
1519
export interface BrainTreeOptions {
1620
amount: string;
1721
collectDeviceData?: boolean;

src/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nativescript-braintree",
3-
"version": "2.0.2",
3+
"version": "2.0.3",
44
"description": "Braintree Payments (Drop-in) plugin for NativeScript.",
55
"main": "braintree",
66
"typings": "index.d.ts",
@@ -54,4 +54,4 @@
5454
},
5555
"dependencies": {},
5656
"bootstrapper": "nativescript-plugin-seed"
57-
}
57+
}

0 commit comments

Comments
 (0)