You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Nov 17, 2022. It is now read-only.
@@ -17,16 +17,24 @@ In this article, you will learn how to call native APIs from JavaScript with var
17
17
18
18
All native numeric types (e.g., char, short, int, double, float on iOS and byte, short, int, long, double, float on Android) are implicitly converted to JavaScript number and vice versa. For example, when you run the following code on iOS:
19
19
20
+
- iOS
21
+
20
22
```JavaScript
21
-
// iOS
22
-
console.log('pow(2.5, 3) = ', pow(2.5, 3));
23
+
console.log(`pow(2.5, 3) = ${pow(2.5, 3)}`);
24
+
```
25
+
```TypeScript
26
+
console.log(`pow(2.5, 3) = ${pow(2.5, 3)}`);
23
27
```
24
28
25
29
the iOS Runtime converts the JavaScript number literals to native doubles and passes them to the native `pow(double x, double y)` function. The returned native integer is automatically converted to a JavaScript number and passed to `console.log()`. The same is valid for Android:
The native `java.lang.Math.min()` method expects two integers. The Android Runtime knows the signature of the function `java.lang.Math.min()` and translates the literals `3` and `4` to their representation in a Java integer data type. The returned integer is also automatically translated to a JavaScript number and passed to `console.log()`.
@@ -35,16 +43,26 @@ The native `java.lang.Math.min()` method expects two integers. The Android Runti
35
43
36
44
JavaScript strings are implicitly marshalled to `java.lang.String` on Android and `NSString` on iOS and vice versa.
37
45
46
+
- iOS
47
+
38
48
```JavaScript
39
-
// iOS
40
-
var button =newUIButton();
49
+
let button =newUIButton();
50
+
button.setTitleForState('Button title', UIControlStateNormal); // 'Button title' is converted to NSString
51
+
console.log(button.titleLabel.text); // The returned NSString is converted to JavaScript string
52
+
```
53
+
```TypeScript
54
+
let button =newUIButton();
41
55
button.setTitleForState('Button title', UIControlStateNormal); // 'Button title' is converted to NSString
42
56
console.log(button.titleLabel.text); // The returned NSString is converted to JavaScript string
43
57
```
44
58
59
+
- Android
60
+
45
61
```JavaScript
46
-
// Android
47
-
var file =newjava.io.File('myfile.txt'); // 'myfile.txt' is converted to java.lang.String
62
+
constfile=newjava.io.File('myfile.txt'); // 'myfile.txt' is converted to java.lang.String
63
+
```
64
+
```TypeScript
65
+
const file =newjava.io.File('myfile.txt'); // 'myfile.txt' is converted to java.lang.String
48
66
```
49
67
50
68
The exception to this are the methods on `NSString` classes declared as returning `instancetype` - init methods and factory methods. This means that a call to `NSString.stringWithString` whose return type in Objective-C is `instancetype` will return a wrapper around a `NSString` instance rather than a JavaScript string.
@@ -55,38 +73,62 @@ The exception to this are the methods on `NSString` classes declared as returnin
55
73
56
74
JavaScript boolean values are implicitly marshalled to `boolean` on Android and `BOOL` on iOS and vice versa.
57
75
76
+
- iOS
77
+
58
78
```JavaScript
59
-
// iOS
60
-
var str =NSString.stringWithString('YES');
61
-
var isTrue =str.boolValue();
79
+
let str =NSString.stringWithString('YES');
80
+
let isTrue =str.boolValue();
81
+
```
82
+
```TypeScript
83
+
let str =NSString.stringWithString('YES');
84
+
let isTrue =str.boolValue();
62
85
```
63
86
87
+
- Android
88
+
64
89
```JavaScript
65
-
// Android
66
-
var str =newjava.lang.String('Hello world!');
67
-
var result =str.endsWith('world!');
90
+
let str =newjava.lang.String('Hello world!');
91
+
let result =str.endsWith('world!');
92
+
console.log(result); // true
93
+
```
94
+
```TypeScript
95
+
let str =newjava.lang.String('Hello world!');
96
+
let result =str.endsWith('world!');
68
97
console.log(result); // true
69
98
```
70
99
71
100
## Array
72
101
73
102
JavaScript arrays map to specialized Java arrays on Android and `NSArray` on iOS.
74
103
104
+
- iOS
105
+
75
106
```JavaScript
76
-
// iOS
77
107
// nsArray is not a JavaScript array but a JavaScript wrapper around a native NSArray
78
-
var nsArray =NSArray.arrayWithArray(['Four', 'Five', 'Two', 'Seven']);
79
-
var jsArray = ['One', 'Two', 'Three']; // pure JavaScript array
80
-
var firstCommon =nsArray.firstObjectCommonWithArray(jsArray);
108
+
let nsArray =NSArray.arrayWithArray(['Four', 'Five', 'Two', 'Seven']);
109
+
let jsArray = ['One', 'Two', 'Three']; // pure JavaScript array
110
+
let firstCommon =nsArray.firstObjectCommonWithArray(jsArray);
81
111
console.log(firstCommon); // Two
82
112
```
113
+
```TypeScript
114
+
// nsArray is not a JavaScript array but a JavaScript wrapper around a native NSArray
115
+
let nsArray =NSArray.arrayWithArray(['Four', 'Five', 'Two', 'Seven']);
116
+
let jsArray = ['One', 'Two', 'Three']; // pure JavaScript array
117
+
let firstCommon =nsArray.firstObjectCommonWithArray(jsArray);
118
+
console.log(firstCommon); // Two
119
+
```
120
+
121
+
- Android
83
122
84
123
The following code snippet shows how to call a `ns.example.Math.minElement(int[] array)` from JavaScript:
85
124
86
125
```JavaScript
87
-
// Android
88
-
var numbers = [3, 6, 19, -2, 7, 6];
89
-
var min =ns.example.Math.minElement(numbers); // -2
126
+
let numbers = [3, 6, 19, -2, 7, 6];
127
+
let min =ns.example.Math.minElement(numbers); // -2
128
+
```
129
+
```TypeScript
130
+
let numbers = [3, 6, 19, -2, 7, 6];
131
+
let min =ns.example.Math.minElement(numbers); // -2
90
132
```
91
133
92
134
## Classes and Objects
@@ -98,8 +140,11 @@ All native classes are represented in the JavaScript world by a constructor func
98
140
Here is an example of how an instance of the `NSMutableArray` class is made and consumed in JavaScript:
99
141
100
142
```JavaScript
101
-
// iOS
102
-
var array =newNSMutableArray();
143
+
let array =newNSMutableArray();
144
+
array.addObject(newNSObject());
145
+
```
146
+
```TypeScript
147
+
let array =newNSMutableArray();
103
148
array.addObject(newNSObject());
104
149
```
105
150
@@ -109,47 +154,75 @@ This snippet creates an instance of `NSMutableArray` and adds an object to it us
109
154
110
155
You will most probably encounter methods accepting NSDictionary instances as parameters. There are few ways of creating an NSDictionary instance:
111
156
112
-
```Javascript
113
-
var dict =newNSDictionary([".example.com", "cookieName", "/", "cookieValue"], [NSHTTPCookieDomain, NSHTTPCookieName, NSHTTPCookiePath,NSHTTPCookieValue]);
114
-
var cookie =NSHTTPCookie.cookieWithProperties(dict);
157
+
- Using `NSDictionary` and passing arrays for keys and values.
158
+
159
+
```JavaScript
160
+
let dict =newNSDictionary([".example.com", "cookieName", "/", "cookieValue"], [NSHTTPCookieDomain, NSHTTPCookieName, NSHTTPCookiePath,NSHTTPCookieValue]);
161
+
let cookie =NSHTTPCookie.cookieWithProperties(dict);
115
162
```
163
+
```TypeScript
164
+
let dict =newNSDictionary([".example.com", "cookieName", "/", "cookieValue"], [NSHTTPCookieDomain, NSHTTPCookieName, NSHTTPCookiePath,NSHTTPCookieValue]);
165
+
let cookie =NSHTTPCookie.cookieWithProperties(dict);
166
+
```
167
+
168
+
- Using JSON literals
116
169
117
-
```Javascript
118
-
var cookie =NSHTTPCookie.cookieWithProperties({[NSHTTPCookieDomain]:".example.com", [NSHTTPCookieName]:"cookieName", [NSHTTPCookiePath]:"/", [NSHTTPCookieValue]:"cookieValue"});
170
+
```JavaScript
171
+
let cookie =NSHTTPCookie.cookieWithProperties({[NSHTTPCookieDomain]:".example.com", [NSHTTPCookieName]:"cookieName", [NSHTTPCookiePath]:"/", [NSHTTPCookieValue]:"cookieValue"});
119
172
```
173
+
```TypeScript
174
+
let cookie =NSHTTPCookie.cookieWithProperties({[NSHTTPCookieDomain]:".example.com", [NSHTTPCookieName]:"cookieName", [NSHTTPCookiePath]:"/", [NSHTTPCookieValue]:"cookieValue"});
175
+
```
176
+
177
+
120
178
In the second example we are passing a JSON literal to the method.**NSHTTPCookieDomain** is a variable and we need to use a [computed property name](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer) in order to have its value (otherwise we are getting *"NSHTTPCookieDomain"* as key).
121
179
122
180
### Working With Classes And Objects on Android
123
181
124
182
The following code snippet demonstrates how an instance of the `android.widget.Button` is created in JavaScript:
125
183
126
184
```JavaScript
127
-
// Android
128
-
var context =...;
129
-
var button =newandroid.widget.Button(context);
185
+
let context =...;
186
+
let button =newandroid.widget.Button(context);
187
+
button.setText("My Button"); // "My Button" is converted to java.lang.String
188
+
```
189
+
```TypeScript
190
+
let context =...;
191
+
let button =newandroid.widget.Button(context);
130
192
button.setText("My Button"); // "My Button" is converted to java.lang.String
131
193
```
194
+
132
195
As you can see, the native Java types are exposed through their corresponding packages. In other words, to access a native Java type, you simply need to know the package it is contained in and explicitly state it. Native Java methods are accessed in the same way as regular JavaScript methods: by using the method identifier and supplying the required arguments. You can read more about Java packages on Android [here](https://docs.nativescript.org/runtimes/android/metadata/accessing-packages).
133
196
134
197
## Undefined and Null
135
198
136
199
JavaScript [Undefined](http://www.w3schools.com/jsref/jsref_undefined.asp) & [Null](http://www.w3schools.com/js/js_datatypes.asp) map to Java null pointer and Objective-C nil. Native null values map to JavaScript null.
137
200
201
+
- iOS
202
+
138
203
```JavaScript
139
-
// iOS
140
204
console.log(NSStringFromClass(null)); // null
141
205
```
206
+
```TypeScript
207
+
console.log(NSStringFromClass(null)); // null
208
+
```
209
+
210
+
- Android
142
211
143
212
```JavaScript
144
-
// Android
145
-
var context =...;
146
-
var button =newandroid.widget.Button(context);
213
+
let context =...;
214
+
constbutton=newandroid.widget.Button(context);
215
+
button.setOnClickListener(undefined); // the Java call will be made using the null keyword
216
+
```
217
+
```TypeScript
218
+
let context =...;
219
+
const button =newandroid.widget.Button(context);
147
220
button.setOnClickListener(undefined); // the Java call will be made using the null keyword
148
221
```
149
222
150
223
## IntelliSense and Access to the Native APIs via TypeScript
151
224
152
-
To have access and Intellisense for the native APIs, you have to add a dev dependency to `tns-platform-declarations`
225
+
To have access and Intellisense for the native APIs, you have to add a developer dependency to `tns-platform-declarations`.
0 commit comments