-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
50 changed files
with
1,873 additions
and
399 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# RFAPI Cookbook | ||
|
||
## URL 拼接 | ||
|
||
RFAPIDefine.path 如果是带 scheme 的完整 URL,将直接用这个 URL;否则会先与 pathPrefix 直接进行字符串拼接,并相对 baseURL 生成最终 URL。 | ||
|
||
baseURL 会依次从当前规则、默认规则、RFAPI 属性中取。baseURL host 后面如果有其他 path,应以「/」结尾,如 `http://example.com/base` 实际相当于 `http://example.com`,正确的应是 `http://example.com/base/`。 | ||
|
||
RFAPIDefine.path 支持用花括号定义参数,如 `user/{user_id}/profile`,传参时带入 `user_id: 123`,则最终 URL 会变成 `user/123/profile`。 | ||
|
||
## 传参 | ||
|
||
请求的参数通过 context 的 `parameters` 属性传入: | ||
|
||
```swift | ||
api.request(name: "some api") { c in | ||
c.parameters = ["foo": "bar", "number": 456, "bool": true] | ||
} | ||
``` | ||
|
||
如果整个参数是数组,通过 `RFAPIRequestArrayParameterKey` 传入: | ||
|
||
```swift | ||
api.request(name: "some api") { c in | ||
c.parameters = [RFAPIRequestArrayParameterKey: [1, 2, 3]] | ||
} | ||
``` | ||
|
||
如果 HTTP body 是 `multipart/form-data` 格式的,可通过 context 的 `formData` 构建数据。 | ||
|
||
```swift | ||
api.request(name: "some api") { c in | ||
c.formData = { formData in | ||
try? formData.appendPart(withFileURL: fileUrl, name: "field1") | ||
formData.appendPart(withForm: someData, name: "field2") | ||
} | ||
} | ||
``` | ||
|
||
## Authentication | ||
|
||
接口定义(RFAPIDefine) `needsAuthorization` 为 `true` 的接口会自动附加 `RFAPIDefineManager` 定义的 HTTP 头(authorizationHeader)和参数(authorizationParameters)。 | ||
|
||
设置方法为: | ||
|
||
```swift | ||
let api = ... // RFAPI instance | ||
api.defineManager.authorizationHeader["Authorization"] = "Custom Credential" | ||
api.defineManager.authorizationParameters["Custom Parameter"] = "Custom Value" | ||
``` | ||
|
||
URLCredential 因接口未暴露暂不支持。 | ||
|
||
## 添加 HTTP header | ||
|
||
在请求中附加 HTTP 头有多种方式: | ||
|
||
* 如需集中式的处理,可通过重载 `RFAPI` 的 `preprocessingRequest(parametersRef:httpHeadersRef:parameters:define:context:)` 或 `finalizeSerializedRequest(_:define:context:)` 方法进行修改; | ||
* 通过接口定义(RFAPIDefine)的 `HTTPRequestHeaders` 属性,如果接口定义中设置了该属性,会使用自己的(不会与默认定义合并),否则会使用默认定义(RFAPIDefineManager.defaultDefine)的; | ||
* 通过 context 传入 `HTTPHeaders`,与其他方式设置的头进行合并,这种方式优先级最高,会覆盖接口定义和认证头。 | ||
|
||
## 错误处理 | ||
|
||
创建请求时可以通过 context 设置请求失败的回调(failure),除此之外,重载 `RFAPI` 的 `generalHandlerForError(_:define:task:failure:)` 方法可以集中处理错误。 | ||
|
||
[一个示例](https://github.com/BB9z/iOS-Project-Template/tree/4.1/App/Networking/API.swift#L63):对系统错误进行包装,token 失效登出,创建请求时不定义错误处理默认报错。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// | ||
// TestEntity.swift | ||
// Example-iOS | ||
// | ||
// Created by BB9z on 2020/3/29. | ||
// Copyright © 2020 RFUI. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
@objc(RFDTestEntity) | ||
class RFDTestEntity : JSONModel { | ||
@objc var uid: Int64 = -1 | ||
@objc var name: String? | ||
|
||
override class func keyMapper() -> JSONKeyMapper! { | ||
return JSONKeyMapper(modelToJSONDictionary: [#keyPath(RFDTestEntity.uid) : "id"]) | ||
} | ||
|
||
override class func propertyIsOptional(_ propertyName: String!) -> Bool { | ||
// All property is optional. | ||
return true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="16096" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="01J-lp-oVM"> | ||
<device id="retina4_7" orientation="portrait" appearance="light"/> | ||
<dependencies> | ||
<deployment identifier="iOS"/> | ||
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="16086"/> | ||
<capability name="Safe area layout guides" minToolsVersion="9.0"/> | ||
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/> | ||
</dependencies> | ||
<scenes> | ||
<!--View Controller--> | ||
<scene sceneID="EHf-IW-A2E"> | ||
<objects> | ||
<viewController id="01J-lp-oVM" sceneMemberID="viewController"> | ||
<view key="view" contentMode="scaleToFill" id="Ze5-6b-2t3"> | ||
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/> | ||
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/> | ||
<subviews> | ||
<stackView opaque="NO" contentMode="scaleToFill" axis="vertical" spacing="18" translatesAutoresizingMaskIntoConstraints="NO" id="RZF-Zt-LQk"> | ||
<rect key="frame" x="155" y="234.5" width="65" height="65"/> | ||
<subviews> | ||
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="RFAPI" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" minimumScaleFactor="0.5" adjustsLetterSpacingToFitWidth="YES" translatesAutoresizingMaskIntoConstraints="NO" id="0dB-em-eZT"> | ||
<rect key="frame" x="0.0" y="0.0" width="65" height="26.5"/> | ||
<fontDescription key="fontDescription" style="UICTFontTextStyleTitle2"/> | ||
<nil key="textColor"/> | ||
<nil key="highlightedColor"/> | ||
</label> | ||
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Example" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" minimumScaleFactor="0.5" adjustsLetterSpacingToFitWidth="YES" translatesAutoresizingMaskIntoConstraints="NO" id="vCa-yw-B28"> | ||
<rect key="frame" x="0.0" y="44.5" width="65" height="20.5"/> | ||
<fontDescription key="fontDescription" style="UICTFontTextStyleBody"/> | ||
<color key="textColor" systemColor="secondaryLabelColor" red="0.23529411759999999" green="0.23529411759999999" blue="0.26274509800000001" alpha="0.59999999999999998" colorSpace="custom" customColorSpace="sRGB"/> | ||
<nil key="highlightedColor"/> | ||
</label> | ||
</subviews> | ||
</stackView> | ||
</subviews> | ||
<color key="backgroundColor" systemColor="secondarySystemBackgroundColor" red="0.94901960780000005" green="0.94901960780000005" blue="0.96862745100000003" alpha="1" colorSpace="custom" customColorSpace="sRGB"/> | ||
<constraints> | ||
<constraint firstItem="RZF-Zt-LQk" firstAttribute="centerX" secondItem="fZj-Pm-80C" secondAttribute="centerX" id="V71-TF-DHL"/> | ||
<constraint firstItem="RZF-Zt-LQk" firstAttribute="centerY" secondItem="fZj-Pm-80C" secondAttribute="centerY" multiplier="0.8" id="eAB-Up-kcN"/> | ||
<constraint firstItem="RZF-Zt-LQk" firstAttribute="leading" relation="greaterThanOrEqual" secondItem="fZj-Pm-80C" secondAttribute="leading" constant="20" id="gsS-en-wrg"/> | ||
</constraints> | ||
<viewLayoutGuide key="safeArea" id="fZj-Pm-80C"/> | ||
</view> | ||
</viewController> | ||
<placeholder placeholderIdentifier="IBFirstResponder" id="iYj-Kq-Ea1" userLabel="First Responder" sceneMemberID="firstResponder"/> | ||
</objects> | ||
<point key="canvasLocation" x="47.200000000000003" y="278.41079460269867"/> | ||
</scene> | ||
</scenes> | ||
</document> |
51 changes: 51 additions & 0 deletions
51
Example/iOS-Swift/Launching/LaunchScreen_zh-Hans.storyboard
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="16096" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="01J-lp-oVM"> | ||
<device id="retina4_7" orientation="portrait" appearance="light"/> | ||
<dependencies> | ||
<deployment identifier="iOS"/> | ||
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="16086"/> | ||
<capability name="Safe area layout guides" minToolsVersion="9.0"/> | ||
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/> | ||
</dependencies> | ||
<scenes> | ||
<!--View Controller--> | ||
<scene sceneID="EHf-IW-A2E"> | ||
<objects> | ||
<viewController id="01J-lp-oVM" sceneMemberID="viewController"> | ||
<view key="view" contentMode="scaleToFill" id="Ze5-6b-2t3"> | ||
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/> | ||
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/> | ||
<subviews> | ||
<stackView opaque="NO" contentMode="scaleToFill" axis="vertical" spacing="18" translatesAutoresizingMaskIntoConstraints="NO" id="RZF-Zt-LQk"> | ||
<rect key="frame" x="153" y="234.5" width="69.5" height="65"/> | ||
<subviews> | ||
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="RFAPI" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" minimumScaleFactor="0.5" adjustsLetterSpacingToFitWidth="YES" translatesAutoresizingMaskIntoConstraints="NO" id="0dB-em-eZT"> | ||
<rect key="frame" x="0.0" y="0.0" width="69.5" height="26.5"/> | ||
<fontDescription key="fontDescription" style="UICTFontTextStyleTitle2"/> | ||
<nil key="textColor"/> | ||
<nil key="highlightedColor"/> | ||
</label> | ||
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="演示项目" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" minimumScaleFactor="0.5" adjustsLetterSpacingToFitWidth="YES" translatesAutoresizingMaskIntoConstraints="NO" id="vCa-yw-B28"> | ||
<rect key="frame" x="0.0" y="44.5" width="69.5" height="20.5"/> | ||
<fontDescription key="fontDescription" style="UICTFontTextStyleBody"/> | ||
<color key="textColor" systemColor="secondaryLabelColor" red="0.23529411759999999" green="0.23529411759999999" blue="0.26274509800000001" alpha="0.59999999999999998" colorSpace="custom" customColorSpace="sRGB"/> | ||
<nil key="highlightedColor"/> | ||
</label> | ||
</subviews> | ||
</stackView> | ||
</subviews> | ||
<color key="backgroundColor" systemColor="secondarySystemBackgroundColor" red="0.94901960780000005" green="0.94901960780000005" blue="0.96862745100000003" alpha="1" colorSpace="custom" customColorSpace="sRGB"/> | ||
<constraints> | ||
<constraint firstItem="RZF-Zt-LQk" firstAttribute="centerX" secondItem="fZj-Pm-80C" secondAttribute="centerX" id="V71-TF-DHL"/> | ||
<constraint firstItem="RZF-Zt-LQk" firstAttribute="centerY" secondItem="fZj-Pm-80C" secondAttribute="centerY" multiplier="0.8" id="eAB-Up-kcN"/> | ||
<constraint firstItem="RZF-Zt-LQk" firstAttribute="leading" relation="greaterThanOrEqual" secondItem="fZj-Pm-80C" secondAttribute="leading" constant="20" id="gsS-en-wrg"/> | ||
</constraints> | ||
<viewLayoutGuide key="safeArea" id="fZj-Pm-80C"/> | ||
</view> | ||
</viewController> | ||
<placeholder placeholderIdentifier="IBFirstResponder" id="iYj-Kq-Ea1" userLabel="First Responder" sceneMemberID="firstResponder"/> | ||
</objects> | ||
<point key="canvasLocation" x="47.200000000000003" y="278.41079460269867"/> | ||
</scene> | ||
</scenes> | ||
</document> |
Oops, something went wrong.