使用 Scriptable 实现 IOS 自定义小组件

Scriptable 简介

  • Plain JavaScript: Supports JavaScript ES6. Scripts are stored as plain JS files on disk.
  • Native APIs: Integrate with the native APIs of iOS directly from JavaScript.
  • Siri Shortcuts: Run scripts from Siri Shortcuts. Present tables, websites, HTML and more in Siri.
  • Documentation: All native APIs that are bridged to JavaScript have documentation which is available offline.
  • Share Sheet Extension: Run a script from a share sheet and process the inputs.
  • Files Integration: Integrated with the file system and Files.app enabling you to perform operations on files.
  • Customizable: The editor can be customized to match your preferences.
  • Example scripts: Comes with several example scripts to get you started.
  • x-callback-url: Communicate with other apps using x-callback-url.
  • Made by Simon Støvring in Copenhagen; Scriptable uses Apples JavaScriptCore

  • 官方文档

  • ios JavaScript Core
  • 一篇简介文章

使用天气小组件

效果图:

scriptable-weather

附:open weather 摘要说明

调用 Api:

let wetherurl = "http://api.openweathermap.org/data/2.5/weather?id=" + CITY_WEATHER + "&APPID=" + APP_ID + "&units=metric"

华氏度设置为英制 imperial,摄氏度设置为公制 metric。其中部分 CITY_WEATHER 代码如下:

1
2
3
4
5
6
7
8
9
// 北京 1816670
// 上海 1796236
// 广州 1809858
// 深圳 1795565
// 南京 1799962
// 苏州 1886760
// 厦门 1790645
// 济南 1805753
let CITY_WEATHER = "1805753";

在此处获取免费的API密钥:https://openweathermap.org/appid

示例:

http://api.openweathermap.org/data/2.5/weather?id=1805753&APPID=********&units=metric

返回值:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
{
"coord": {
"lon": 117,
"lat": 36.67
},
"weather": [
{
"id": 500,
"main": "Rain",
"description": "light rain",
"icon": "10d"
}
],
"base": "stations",
"main": {
"temp": 21.67,
"feels_like": 19.85,
"temp_min": 21.67,
"temp_max": 21.67,
"pressure": 1013,
"humidity": 50
},
"visibility": 10000,
"wind": {
"speed": 2.97,
"deg": 230
},
"rain": {
"1h": 0.25
},
"clouds": {
"all": 76
},
"dt": 1602314886,
"sys": {
"type": 3,
"id": 2020829,
"country": "CN",
"sunrise": 1602281682,
"sunset": 1602322978
},
"timezone": 28800,
"id": 1805753,
"name": "Jinan",
"cod": 200
}