iOS SDK Config
This document will introduce how to configure the iOS SDK for QWeather API.
OS Requirement:
- iOS 9.0+
- macOS 10.10+
Hint: QWeather iOS SDK also includes macOS SDK.
Step 1: Create Project and Credential
Make sure you have created a Project and Credential, see Project and KEY and Authentication.
Step 2: Install SDK
iOS SDK supports the following installation methods:
CocoaPods
QWeather iOS SDK can be installed via CocoaPods. CocoaPods is an open source dependency manager, if you haven’t installed CocoaPods yet, please refer to CocoaPods Getting Started guide. The following steps assume that you have already completed the installation and setup of CocoaPods.
- Create a file named
Podfile
in your project directory - Add the following to the
Podfile
and save it- iOS
target 'YourTargetName-iOS' do pod 'QWeather-SDK' end
- macOS
target 'YourTargetName-macOS' do pod 'QWeather-SDK' end
- iOS
- Open a terminal and go to the directory containing the
Podfile
and run:pod install
Update SDK
Open a terminal and go to the directory containing the Podfile
and run:
pod update
Swift Package Manager
You can also use Swift Package Manager to integrate iOS SDK, please refer to Adding package dependencies to your app.
- SDK URL: https://github.com/qwd/iOS-SDK.git
- Dependency Rule: Up to Next Major Version 4.14.0 < 5.0.0
Manual install
- Download SDK: QWeather_iOS_SDK_Pub_V4.17
MD5: ddedb09235363920b408e1a6cc9434ad - Add
QWeather.xcframework
to iOS/macOS Target.
If the project is written by swift:
- Create a new OC class in the main directory of the Swift project. If the OC class is created for the first time in the project, a prompt window as shown in the figure will pop up. This prompt window is a pop-up prompt for whether to create a Swift-OC bridge file.
- Click Create Bridging Header, Xcode will automatically create a header file. This header file is the bridge file of Swift-OC
- Declare this framework class in the Swift-OC bridge file and it can be used.
The sdk needs to enable the location permission, which is configured in Info.plist:
NSLocationWhenInUseUsageDescription NSLocationAlwaysAndWhenInUseUsageDescription
Warning:
- If there is a crash say
unrecognized selector sent to ...
, please add-ObjC
to Other Linker Flags in Build SettingsThe framework does not provide log function, error information can be obtained from the following callback
getError
-(void)weatherWithInquireType:(INQUIRE_TYPE)inquireType WithSuccess:(void(^)(id responseObject))getSuccess faileureForError:(void(^)(NSError *error))getError;
Step 3: Setup KEY and Subscription
Setup the API KEY and Subscription in the AppDelegate
:
Setup Public ID and KEY
Replace Your_KEY
and Your_Public_ID
with your KEY and credential ID:
QWeatherConfigInstance.publicID = @"Your_Public_ID";
QWeatherConfigInstance.appKey = @"Your_KEY";
Setup Subscription
-
Standard subscription:
QWeatherConfigInstance.appType = APP_TYPE_BIZ;
-
Free subscription:
QWeatherConfigInstance.appType = APP_TYPE_DEV;
Sample Code
Add the following code where you need to use, enter the required parameters, and return to the data model of the corresponding type
/**
* Real-time weather
*
* @param userType: user type, divided into commercial version and free development version
* @param location: location to be queried
* @param lang: set multi-language, the default is simplified Chinese
* @param unit: set unit, metric (m) or imperial (i), the default is metric unit
*/
#import <QWeather/QWeather.h>
AllWeatherInquieirs *qWeather = [AllWeatherInquieirs sharedInstance];//or AllWeatherInquieirs *qWeather = [[AllWeatherInquieirs alloc] init];
qWeather.appType = APP_TYPE_BIZ;
qWeather.location = @"101010300";
qWeather.lang = @"";//or qWeather.languageType = @"";
qWeather.unit = @"";//or qWeather.unitType = @"";
[qWeather weatherWithInquireType: INQUIRE_TYPE_WEATHER_NOW WithSuccess:^(WeatherBaseClass * responseObject) {
} faileureForError:^(NSError *error) {
}];