最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【记录】试用Swift中的网络库Alamofire

iOS crifan 1792浏览 0评论

之前就知道Alamofire,但是以为只有OC,没有swift的

现在发现也有swift的了:

Alamofire/Alamofire: Elegant HTTP Networking in Swift

现在去安装到项目中:

<code>github "robb/Cartography"
github "danielgindi/Charts" ~&gt; 2.2.4
github "realm/realm-cocoa"
github "DaveWoodCom/XCGLogger" ~&gt; 3.3
github "Alamofire/Alamofire" ~&gt; 3.4
</code>

然后去安装:

<code>licrifandeMacBook-Pro:QorosSales crifan$ carthage update Alamofire
*** Cloning Alamofire
*** Fetching XCGLogger
*** Fetching realm-cocoa
*** Fetching Charts
*** Fetching Cartography
*** Checking out Alamofire at "3.4.0"
*** xcodebuild output can be found in /var/folders/46/2hjxz38n22n3ypp_5f6_p__00000gn/T/carthage-xcodebuild.QdeN2y.log
*** Building scheme "Alamofire iOS" in Alamofire.xcworkspace
2016-05-18 19:45:33.061 xcodebuild[2435:88642] [MT] PluginLoading: Required plug-in compatibility UUID ACA8656B-FEA8-4B6D-8E4A-93F4C95C362C for plug-in at path '~/Library/Application Support/Developer/Shared/Xcode/Plug-ins/WCGitTagsPlugin.xcplugin' not present in DVTPlugInCompatibilityUUIDs
2016-05-18 19:46:02.538 xcodebuild[2555:89822] [MT] PluginLoading: Required plug-in compatibility UUID ACA8656B-FEA8-4B6D-8E4A-93F4C95C362C for plug-in at path '~/Library/Application Support/Developer/Shared/Xcode/Plug-ins/WCGitTagsPlugin.xcplugin' not present in DVTPlugInCompatibilityUUIDs
*** Building scheme "Alamofire watchOS" in Alamofire.xcworkspace
2016-05-18 19:46:30.448 xcodebuild[2647:90845] [MT] PluginLoading: Required plug-in compatibility UUID ACA8656B-FEA8-4B6D-8E4A-93F4C95C362C for plug-in at path '~/Library/Application Support/Developer/Shared/Xcode/Plug-ins/WCGitTagsPlugin.xcplugin' not present in DVTPlugInCompatibilityUUIDs
2016-05-18 19:46:44.083 xcodebuild[2713:91191] [MT] PluginLoading: Required plug-in compatibility UUID ACA8656B-FEA8-4B6D-8E4A-93F4C95C362C for plug-in at path '~/Library/Application Support/Developer/Shared/Xcode/Plug-ins/WCGitTagsPlugin.xcplugin' not present in DVTPlugInCompatibilityUUIDs
*** Building scheme "Alamofire tvOS" in Alamofire.xcworkspace
2016-05-18 19:46:58.965 xcodebuild[2772:91580] [MT] PluginLoading: Required plug-in compatibility UUID ACA8656B-FEA8-4B6D-8E4A-93F4C95C362C for plug-in at path '~/Library/Application Support/Developer/Shared/Xcode/Plug-ins/WCGitTagsPlugin.xcplugin' not present in DVTPlugInCompatibilityUUIDs
2016-05-18 19:47:14.175 xcodebuild[2839:92008] [MT] PluginLoading: Required plug-in compatibility UUID ACA8656B-FEA8-4B6D-8E4A-93F4C95C362C for plug-in at path '~/Library/Application Support/Developer/Shared/Xcode/Plug-ins/WCGitTagsPlugin.xcplugin' not present in DVTPlugInCompatibilityUUIDs
*** Building scheme "Alamofire OSX" in Alamofire.xcworkspace
2016-05-18 19:47:38.550 xcodebuild[2898:93027] [MT] PluginLoading: Required plug-in compatibility UUID ACA8656B-FEA8-4B6D-8E4A-93F4C95C362C for plug-in at path '~/Library/Application Support/Developer/Shared/Xcode/Plug-ins/WCGitTagsPlugin.xcplugin' not present in DVTPlugInCompatibilityUUIDs
</code>

现在抽空去用试试

Alamofire/Alamofire: Elegant HTTP Networking in Swift

代码:

<code>        Alamofire.request(.POST, ServerApi.getSmsCodeUrl(), parameters: ["phone" : self.registerView.phoneTextField.text!, "codetype" : "register"], encoding: ParameterEncoding.JSON, headers: ["Accept" : "application/json"]).responseJSON(completionHandler: { response in
            gLog.verbose("request=\(response.request), response=\(response.response), statusCode=\(response.response?.statusCode), data=\(response.data), result=\(response.result)")
            /*
            request=Optional(&lt;NSMutableURLRequest: 0x7a6c4e20&gt; { URL: http://qorosmm.chinacloudapp.cn/code }), response=Optional(&lt;NSHTTPURLResponse: 0x7a6c0e90&gt; { URL: http://qorosmm.chinacloudapp.cn/code } { status code: 403, headers {
                Connection = "keep-alive";
                "Content-Length" = 56;
                "Content-Type" = "application/json;charset=utf-8";
                Date = "Tue, 31 May 2016 14:27:12 GMT";
                Server = "nginx/1.10.0";
            } }), statusCode=Optional(403), data=Optional(&lt;7b22636f 6465223a 3430332c 226d6573 73616765 223a2254 68652070 686f6e65 20697320 72656769 73746572 65642062 65666f72 6520227d&gt;), result=SUCCESS
             */

            switch response.result {
            case .Success(let value):
                gLog.verbose("value=\(value)")

                /*
                {
                    code = 403;
                    message = "The phone is not found ";
                }

                {
                    code = 403;
                    message = "The phone is registered before ";
                }
                 */
            case .Failure(let error):
                gLog.verbose("error=\(error)")
            }
        })
</code>

更详细和完整的代码请参考:

https://github.com/crifan/crifanLib/blob/master/swift/Http/CrifanLibHttp.swift

https://github.com/crifan/crifanLib/blob/master/swift/Http/CrifanLibHttpDemo.swift

转载请注明:在路上 » 【记录】试用Swift中的网络库Alamofire

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
83 queries in 0.176 seconds, using 22.05MB memory