### Example API Host Source: https://dev.qweather.com/docs/configuration/api-host This is an example of what your unique API Host will look like. Replace this with your actual API Host when making requests. ```text abc1234xyz.def.qweatherapi.com ``` -------------------------------- ### Install Dependencies with CocoaPods Source: https://dev.qweather.com/docs/configuration/ios-sdk-config Run this command in your terminal within the directory containing your Podfile to install the SDK and its dependencies. ```bash pod install ``` -------------------------------- ### Example API Request URL Source: https://dev.qweather.com/docs/best-practices/optimize-requests A basic example of a QWeather API request URL. Ensure all parameters and values are correctly formatted according to RFC 3986. ```text https://abcxyz.qweatherapi.com/v7/weather/3d?parameters ``` -------------------------------- ### Install QWeather SDK via CocoaPods (macOS) Source: https://dev.qweather.com/docs/configuration/ios-sdk-config Add the QWeather-SDK to your macOS project's Podfile for installation using CocoaPods. Ensure you have CocoaPods installed and configured. ```ruby target '{YOUR_macOS_TARGET}' do pod 'QWeather-SDK','~> 5.2.1' end ``` -------------------------------- ### Install QWeather SDK via CocoaPods (iOS) Source: https://dev.qweather.com/docs/configuration/ios-sdk-config Add the QWeather-SDK to your iOS project's Podfile for installation using CocoaPods. Ensure you have CocoaPods installed and configured. ```ruby target '{YOUR_iOS_TARGET}' do pod 'QWeather-SDK','~> 5.2.1' end ``` -------------------------------- ### Solar Radiation Forecast Request Example Source: https://dev.qweather.com/docs/api/solar-radiation/solar-radiation-forecast Use this cURL command to request solar radiation forecast data. Replace 'your_token' with your JWT authentication and 'your_api_host' with your actual API host. This example fetches data for a specific latitude and longitude. ```bash curl -X GET --compressed \ -H "Authorization: Bearer your_token" \ 'https://your_api_host/solarradiation/v1/forecast/50.11/8.68' ``` -------------------------------- ### General Attribution Example Source: https://dev.qweather.com/docs/terms/attribution This is the recommended way to attribute QWeather services in your product, such as an app or website. Ensure the text is clearly visible and not easily confused with other content. ```text 天气服务由和风天气驱动 ``` -------------------------------- ### Request Real-time Weather Data Source: https://dev.qweather.com/docs/configuration/android-sdk-config Example of requesting real-time weather data for Beijing using WeatherParameter and a Callback to handle the response. ```java WeatherParameter parameter = new WeatherParameter("101010100") .lang(Lang.ZH_HANS) .unit(Unit.METRIC); QWeather.instance.weatherNow(parameter, new Callback() { @Override public void onSuccess(WeatherNowResponse response) { Log.i(TAG, response.toString()); } @Override public void onFailure(ErrorResponse errorResponse) { Log.i(TAG,errorResponse.toString()); } @Override public void onException(Throwable e) { e.printStackTrace(); } }); ``` -------------------------------- ### Air Quality Daily Forecast cURL Example Source: https://dev.qweather.com/docs/api/air-quality/air-daily-forecast Example of how to make a GET request to the Air Quality Daily Forecast API using cURL. Replace 'your_token' with your JWT authentication and 'your_api_host' with your actual API host. ```curl curl -X GET --compressed \ -H 'Authorization: Bearer your_token' \ 'https://your_api_host/airquality/v1/daily/39.90/116.40' ``` -------------------------------- ### Initialize QWeather Instance Source: https://dev.qweather.com/docs/configuration/android-sdk-config Initialize the QWeather SDK instance with your API Host. Debug logs can be enabled for development. ```java QWeather.getInstance(MainActivity.this, "{YOUR_HOST}") // Initialize service address .setLogEnable(true); // Enable debug logs (recommended to set to false in production environment) ``` -------------------------------- ### Get 1-Day Weather Indices Forecast Source: https://dev.qweather.com/docs/api/indices/indices-forecast Example of how to request a 1-day forecast for specific lifestyle indices (e.g., sports and car washing) for a given location ID. Replace 'your_token' with your JWT and 'your_api_host' with your actual API host. ```curl curl -X GET --compressed \ -H 'Authorization: Bearer your_token' \ 'https://your_api_host/v7/indices/1d?type=1,2&location=101010100' ``` -------------------------------- ### Initialize QWeather Instance (Swift) Source: https://dev.qweather.com/docs/configuration/ios-sdk-config Initialize the QWeather SDK instance with your API Host. Debug logging can be enabled for development purposes. ```swift import QWeatherSDK ... try await QWeather .getInstance("{YOUR_HOST}") // Initialize service address .setupLogEnable(true) // Enable debug logging (recommended to set to false in production) ``` -------------------------------- ### Initialize QWeather Instance (Objective-C) Source: https://dev.qweather.com/docs/configuration/ios-sdk-config Initialize the QWeather SDK instance with your API Host. Debug logging can be enabled for development purposes. ```objectivec #import ... // Initialize service address [QWeatherObjc initConfigWithHost:@"{YOUR_HOST}"]; // Enable debug logging (recommended to set to false in production) [QWeatherObjc setupLogEnable:YES]; ``` -------------------------------- ### v1 Error Response Example Source: https://dev.qweather.com/docs/resource/status-code This example shows a v1 error response where the 'code' field indicates an authentication failure. ```json HTTP/2 200 content-type: application/json { "code": "401" } ``` -------------------------------- ### Request Real-time Weather Data (Objective-C) Source: https://dev.qweather.com/docs/configuration/ios-sdk-config Example of requesting real-time weather data for Beijing using the QWeather SDK. Handles responses and errors via a completion handler. ```objectivec #import ... WeatherParameter *parameter = [WeatherParameter instanceWithLocation:@"101010100" lang:@(LangZH_HANS) unit:@(UnitMETRIC)]; [QWeatherObjc weatherNow:parameter completionHandler:^(WeatherNowResponse * _Nullable response, NSError * _Nullable error) { if (response) { NSLog(@"%@", response.description); } if (error) { NSLog(@"%@", error.localizedDescription); } }]; ``` -------------------------------- ### 获取实时空气质量数据 (Objective-C) Source: https://dev.qweather.com/docs/ios-sdk/air-quality/ios-air-current 使用Objective-C调用实时空气质量API。需要导入QWeather SDK并提供经纬度参数。通过completion handler处理响应或错误。 ```objc AirV1Parameter *parameter = [AirV1Parameter instanceWithLongitude:116.41 latitude:39.92 lang:@(LangZH_HANS)]; [QWeatherObjc airCurrent:parameter completionHandler:^(AirV1CurrentResponse * _Nullable response, NSError * _Nullable error) { if (response) { NSLog(@"%@", response.description); } if (error) { NSLog(@"%@", error.localizedDescription); } }]; ``` -------------------------------- ### 获取实时空气质量数据 (Swift) Source: https://dev.qweather.com/docs/ios-sdk/air-quality/ios-air-current 使用Swift调用实时空气质量API。需要导入QWeather SDK并提供经纬度参数。捕获可能发生的网络或API错误。 ```swift Task { do { let parameter = AirV1Parameter(longitude: 116.41, latitude: 39.92) let response = try await QWeather.instance .airCurrent(parameter) print(response) } catch QWeatherError.errorResponse(let error) { print(error) } catch { print(error) } } ``` -------------------------------- ### Tropical Cyclone List Response Example Source: https://dev.qweather.com/docs/api/tropical-cyclone/storm-list This is an example of the JSON response structure for the tropical cyclone list API. It includes details about the storm, update time, and data sources. ```json { "code": "200", "updateTime": "2020-12-31T16:00+00:00", "fxLink": "https://www.qweather.com", "storm": [ { "id": "NP_2022", "name": "环高", "basin": "NP", "year": "2020", "isActive": "0" }, { "id": "NP_2021", "name": "艾涛", "basin": "NP", "year": "2020", "isActive": "0" }, { "id": "NP_2020", "name": "艾莎尼", "basin": "NP", "year": "2020", "isActive": "0" }, { "id": "NP_2019", "name": "天鹅", "basin": "NP", "year": "2020", "isActive": "0" }, { "id": "NP_2018", "name": "莫拉菲", "basin": "NP", "year": "2020", "isActive": "0" }, { "id": "NP_2017", "name": "沙德尔", "basin": "NP", "year": "2020", "isActive": "0" }, { "id": "NP_200012", "name": "无命名", "basin": "NP", "year": "2020", "isActive": "0" }, { "id": "NP_2016", "name": "浪卡", "basin": "NP", "year": "2020", "isActive": "0" }, { "id": "NP_2015", "name": "莲花", "basin": "NP", "year": "2020", "isActive": "0" }, { "id": "NP_2014", "name": "灿鸿", "basin": "NP", "year": "2020", "isActive": "0" }, { "id": "NP_2013", "name": "鲸鱼", "basin": "NP", "year": "2020", "isActive": "0" }, { "id": "NP_2012", "name": "白海豚", "basin": "NP", "year": "2020", "isActive": "0" }, { "id": "NP_2011", "name": "红霞", "basin": "NP", "year": "2020", "isActive": "0" }, { "id": "NP_2010", "name": "海神", "basin": "NP", "year": "2020", "isActive": "0" }, { "id": "NP_2009", "name": "美莎克", "basin": "NP", "year": "2020", "isActive": "0" }, { "id": "NP_2008", "name": "巴威", "basin": "NP", "year": "2020", "isActive": "0" }, { "id": "NP_2007", "name": "海高斯", "basin": "NP", "year": "2020", "isActive": "0" }, { "id": "NP_2006", "name": "米克拉", "basin": "NP", "year": "2020", "isActive": "0" }, { "id": "NP_2005", "name": "蔷薇", "basin": "NP", "year": "2020", "isActive": "0" }, { "id": "NP_2004", "name": "黑格比", "basin": "NP", "year": "2020", "isActive": "0" }, { "id": "NP_2003", "name": "森拉克", "basin": "NP", "year": "2020", "isActive": "0" }, { "id": "NP_2002", "name": "鹦鹉", "basin": "NP", "year": "2020", "isActive": "0" }, { "id": "NP_2001", "name": "黄蜂", "basin": "NP", "year": "2020", "isActive": "0" } ], "refer": { "sources": [ "NMC" ], "license": [ "QWeather Developers License" ] } } ``` -------------------------------- ### v2 Error Response Example Source: https://dev.qweather.com/docs/resource/error-code This is an example of an error response from the v2 API, indicating invalid parameters. It includes the HTTP status code, error type, title, detail, and specific invalid parameters. ```json HTTP/2 400 Content-Type: application/problem+json { "error": { "status": 400, "type": "https://dev.qweather.com/docs/resource/error-code/#invalid-parameters", "title": "Invalid Parameters", "detail": "Invalid parameters, please check your request.", "invalidParams": [ "lang" ] } } ``` -------------------------------- ### Add QWeather SDK JAR to Gradle Source: https://dev.qweather.com/docs/configuration/android-sdk-config Copy the downloaded JAR file to the app/libs/ directory and update your app/build.gradle file to include the SDK. ```gradle dependencies { // Add the following configuration implementation files('libs/QWeather_Public_Android_V5.2.2.jar') // Or add all JARs in bulk implementation fileTree(dir: 'libs', include: ['*.jar']) } ``` -------------------------------- ### Fetch Daily Weather Forecasts (Objective-C) Source: https://dev.qweather.com/docs/ios-sdk/weather/ios-weather-daily-forecast This Objective-C code demonstrates how to fetch daily weather forecast data for 3, 7, 10, 15, and 30 days. It includes setting up parameters and a completion handler for processing the response or errors. ```Objective-C WeatherParameter * parameter = [WeatherParameter instanceWithLocation:@"101010100" lang:@(LangZH_HANS) unit:@(UnitMETRIC)]; void (^handler)(WeatherDailyResponse *, NSError *) = ^(WeatherDailyResponse *response, NSError *error) { if (response) { NSLog(@"%@", response.description); } if (error) { NSLog(@"%@", error.localizedDescription); } }; /* * 获取3天预报数据 */ [QWeatherObjc weather3d:parameter completionHandler:handler]; /* * 获取7天预报数据 */ [QWeatherObjc weather7d:parameter completionHandler:handler]; /* * 获取10天预报数据 */ [QWeatherObjc weather10d:parameter completionHandler:handler]; /* * 获取15天预报数据 */ [QWeatherObjc weather15d:parameter completionHandler:handler]; /* * 获取30天预报数据 */ [QWeatherObjc weather30d:parameter completionHandler:handler]; ``` -------------------------------- ### Get 168-Hour Forecast Source: https://dev.qweather.com/docs/android-sdk/weather/android-weather-hourly-forecast Retrieve the 168-hour weather forecast data using the provided WeatherParameter and a callback. ```Java /** * 获取168小时预报数据 */ public void weather168h(WeatherParameter parameter, Callback callback); ``` -------------------------------- ### Get 72-Hour Forecast Source: https://dev.qweather.com/docs/android-sdk/weather/android-weather-hourly-forecast Retrieve the 72-hour weather forecast data using the provided WeatherParameter and a callback. ```Java /** * 获取72小时预报数据 */ public void weather72h(WeatherParameter parameter, Callback callback); ``` -------------------------------- ### Fetch Real-time Weather Alerts in Objective-C Source: https://dev.qweather.com/docs/ios-sdk/warning/ios-weather-alert This Objective-C code snippet demonstrates how to fetch real-time weather alerts using the QWeather SDK. It requires initializing the parameter object and handling the response or error in the completion handler. ```objc WeatherAlertCurrentParameter *parameter = [WeatherAlertCurrentParameter instanceWithLongitude: 113.26 latitude: 38.73 localTime: NO lang: @(LangZH_HANS)]; [QWeatherObjc weatherAlertCurrent:parameter completionHandler:^(WeatherAlertCurrentResponse * _Nullable response, NSError * _Nullable error) { if (response) { NSLog(@"%@", response.description); } if (error) { NSLog(@"%@", error.localizedDescription); } }]; ``` -------------------------------- ### Get 24-Hour Forecast Source: https://dev.qweather.com/docs/android-sdk/weather/android-weather-hourly-forecast Retrieve the 24-hour weather forecast data using the provided WeatherParameter and a callback. ```Java /** * 获取24小时预报数据 */ public void weather24h(WeatherParameter parameter, Callback callback); ``` -------------------------------- ### Objective-C 日出日落查询 Source: https://dev.qweather.com/docs/ios-sdk/astronomy/ios-sunrise-sunset 使用Objective-C查询指定地点的日出日落时间。需要导入QWeather SDK,并提供LocationID和日期作为参数。支持回调函数处理响应和错误。 ```objective-c NSDateFormatter * formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"yyyyMMdd"]; NSString * date = [formatter stringFromDate:[NSDate date]]; AstronomySunParameter *parameter = [AstronomySunParameter instanceWithLocation:@"101010100" date:date]; [QWeatherObjc astronomySun:parameter completionHandler:^(AstronomySunResponse * _Nullable response, NSError * _Nullable error) { if (response) { NSLog(@"%@", response.description); } if (error) { NSLog(@"%@", error.localizedDescription); } }]; ``` -------------------------------- ### v1 Success Response Example Source: https://dev.qweather.com/docs/resource/error-code A successful response from the v1 API, indicated by a 'code' field with a value of '200'. ```json HTTP/2 200 content-type: application/json { "code": "401" } ``` -------------------------------- ### Add SDK Dependencies Source: https://dev.qweather.com/docs/configuration/android-sdk-config Include necessary third-party libraries like OkHttp, Gson, and EdDSA for SDK functionality. ```gradle implementation 'com.squareup.okhttp3:okhttp:4.12.0' implementation 'com.google.code.gson:gson:2.10.1' implementation 'net.i2p.crypto:eddsa:0.3.0' ``` -------------------------------- ### Hourly Forecast JSON Response Source: https://dev.qweather.com/docs/api/weather/grid-weather-hourly-forecast This is an example of the JSON response received from the hourly forecast API. The data is Gzip compressed. ```json { "code": "200", "updateTime": "2021-12-16T19:27+08:00", "fxLink": "https://www.qweather.com", "hourly": [ { "fxTime": "2021-12-16T12:00+00:00", "temp": "-2", "icon": "150", "text": "晴", "wind360": "285", "windDir": "西北风", "windScale": "2", "windSpeed": "8", "humidity": "30", "precip": "0.0", "pressure": "1022", "cloud": "0", "dew": "-17" }, { "fxTime": "2021-12-16T13:00+00:00", "temp": "-3", "icon": "150", "text": "晴", "wind360": "289", "windDir": "西北风", "windScale": "2", "windSpeed": "8", "humidity": "32", "precip": "0.0", "pressure": "1023", "cloud": "0", "dew": "-17" }, { "fxTime": "2021-12-16T14:00+00:00", "temp": "-3", "icon": "150", "text": "晴", "wind360": "293", "windDir": "西北风", "windScale": "2", "windSpeed": "7", "humidity": "34", "precip": "0.0", "pressure": "1024", "cloud": "0", "dew": "-17" }, { "fxTime": "2021-12-16T15:00+00:00", "temp": "-4", "icon": "150", "text": "晴", "wind360": "296", "windDir": "西北风", "windScale": "2", "windSpeed": "6", "humidity": "35", "precip": "0.0", "pressure": "1024", "cloud": "0", "dew": "-17" }, { "fxTime": "2021-12-16T16:00+00:00", "temp": "-4", "icon": "150", "text": "晴", "wind360": "294", "windDir": "西北风", "windScale": "2", "windSpeed": "6", "humidity": "35", "precip": "0.0", "pressure": "1025", "cloud": "0", "dew": "-17" }, { "fxTime": "2021-12-16T17:00+00:00", "temp": "-5", "icon": "150", "text": "晴", "wind360": "290", "windDir": "西北风", "windScale": "2", "windSpeed": "7", "humidity": "35", "precip": "0.0", "pressure": "1025", "cloud": "0", "dew": "-18" }, { "fxTime": "2021-12-16T18:00+00:00", "temp": "-5", "icon": "150", "text": "晴", "wind360": "280", "windDir": "西风", "windScale": "2", "windSpeed": "8", "humidity": "33", "precip": "0.0", "pressure": "1025", "cloud": "0", "dew": "-19" }, { "fxTime": "2021-12-16T19:00+00:00", "temp": "-6", "icon": "154", "text": "阴", "wind360": "277", "windDir": "西风", "windScale": "2", "windSpeed": "9", "humidity": "30", "precip": "0.0", "pressure": "1026", "cloud": "6", "dew": "-20" }, { "fxTime": "2021-12-16T20:00+00:00", "temp": "-6", "icon": "150", "text": "晴", "wind360": "283", "windDir": "西北风", "windScale": "2", "windSpeed": "9", "humidity": "29", "precip": "0.0", "pressure": "1026", "cloud": "0", "dew": "-21" }, { "fxTime": "2021-12-16T21:00+00:00", "temp": "-7", "icon": "150", "text": "晴", "wind360": "286", "windDir": "西北风", "windScale": "2", "windSpeed": "9", "humidity": "27", "precip": "0.0", "pressure": "1026", "cloud": "0", "dew": "-22" }, { "fxTime": "2021-12-16T22:00+00:00", "temp": "-7", "icon": "150", "text": "晴", "wind360": "287", "windDir": "西北风", "windScale": "2", "windSpeed": "8", "humidity": "27", "precip": "0.0", "pressure": "1026", "cloud": "0", "dew": "-23" }, { "fxTime": "2021-12-16T23:00+00:00", "temp": "-7", "icon": "150", "text": "晴", "wind360": "293", "windDir": "西北风", "windScale": "1", "windSpeed": "5", "humidity": "28", "precip": "0.0", "pressure": "1027", "cloud": "0", "dew": "-23" }, { "fxTime": "2021-12-17T00:00+00:00", "temp": "-7", "icon": "150", "text": "晴", "wind360": "304", "windDir": "西北风", "windScale": "1", "windSpeed": "3", "humidity": "27", "precip": "0.0", "pressure": "1027", "cloud": "0", "dew": "-23" }, { "fxTime": "2021-12-17T01:00+00:00", "temp": "-7", "icon": "150", "text": "晴", "wind360": "335", "windDir": "西北风", "windScale": "1", "windSpeed": "4", "humidity": "24", "precip": "0.0", "pressure": "1028", "cloud": "0", "dew": "-24" }, { "fxTime": "2021-12-17T02:00+00:00", "temp": "-6", "icon": "150", "text": "晴", "wind360": "329", "windDir": "西北风", "windScale": "2", "windSpeed": "8", "humidity": "20", "precip": "0.0", "pressure": "1028", "cloud": "0", "dew": "-25" } ] } ```