Use of iOS Mknetworkkit

Source: Internet
Author: User
<span id="Label3"></p><p><p>Commonly used frameworks such as: afnetworking,asihttprequest,sdwebimage,mknetworkit and so On.</p></p><p><p>IOS5 has been out so long, and asihttprequest the author has affirmed that not updated, in the iOS5 environment, in fact, there are some problems.</p></p><p><p>Now Mknetworkki absorbs the advantages of asihttprequest and afnetworking, and joins its own unique functions.</p></p>Download:<p><p>GitHub Address: Https://github.com/MugunthKumar/MKNetworkKit.git</p></p><p><p></p></p><p><p>Official instructions for Use: http://blog.mugunthkumar.com/products/ios-framework-introducing-mknetworkkit/</p></p><p><p></p></p><p><p></p></p><p><p>GitHub downloaded the project, if you want to run its demo, be sure to open mknetworkkit.xcworkspace the file, if opened separately, the compile will prompt the missing LIBMKNETWORKKIT-IOS.A file!!!</p></p><p><p></p></p>Installation:<p><p>After cloning, drag the <em>mknetworkkit</em> folder into the project and introduce 3 frameworks:</p></p><p><p><em>cfnetwork.framework, <strong>systemconfiguration.framework, security.framework</strong> <strong> </strong></em></p></p><p><p></p></p><p><p></p></p><p><p>Note: since Mknetworkkit supports arc, we have to open arc in the project, or we will get an Error.</p></p><p><p></p></p><p><p></p></p><p><p>Turn on the ARC Automatic Memory control mechanism: (all <strong><em>dealloc, release,</em></strong><strong><em>autorelease</em></strong> are commented out in the project after opening Arc)</p></p><p><p></p></p><p><p></p></p>How to Use:<p><p>Import where you need to use:<em>#import "MKNetworkKit.h"</em></p></p>HTTP mode:<p><p></p></p>Get Request:<p><p></p></p><p><p><strong>[cpp]</strong> View plaincopy</p></p> <ol> <ol> <li><p>Mknetworkengine *engine = [[mknetworkengine alloc] initwithhostname:@ "192.168.2.176:3000" customheaderfields:nil];</p></li> <li><p>Mknetworkoperation *op = [engine operationwithpath:@ "/index" params:nil httpmethod:@ "GET" ssl:no];</p></li> <li><p>[op addcompletionhandler:^ (mknetworkoperation *operation) {</p></li> <li><p>NSLog (@ "[operation responsedata]-->>%@", [operation responsestring]);</p></li> <li><p>}errorhandler:^ (mknetworkoperation *errorop, nserror* err) {</p></li> <li><p>NSLog (@ "mknetwork request error:%@", [err localizeddescription]);</p></li> <li><p>}];</p></li> <li><p>[engine enqueueoperation:op];</p></li> </ol> </ol>Post Request:<p><p></p></p><p><p><strong>[cpp]</strong> View plaincopy</p></p> <ol> <li><li><p>Mknetworkengine *engine = [[mknetworkengine alloc] initwithhostname:@ "192.168.2.176:3000" customheaderfields:nil];</p></li></li> <li><li><p>Nsmutabledictionary *dic = [[nsmutabledictionary alloc] init];</p></li></li> <li><li><p>[dic setvalue:@ "admin" forkey:@ "username"];</p></li></li> <li><li><p>[dic setvalue:@ "123" forkey:@ "password"];</p></li></li> <li><li><p></p></li></li> <li><li><p>Mknetworkoperation *op = [engine operationwithpath:@ "/login" params:dic httpmethod:@ "POST"];</p></li></li> <li><li><p>[op addcompletionhandler:^ (mknetworkoperation *operation) {</p></li></li> <li><li><p>NSLog (@ "[operation responsedata]-->>%@", [operation responsestring]);</p></li></li> <li><li><p>}errorhandler:^ (mknetworkoperation *errorop, nserror* err) {</p></li></li> <li><li><p>NSLog (@ "mknetwork request error:%@", [err localizeddescription]);</p></li></li> <li><li><p>}];</p></li></li> <li><li><p>[engine enqueueoperation:op];</p></li></li> </ol><p><p></p></p>HTTPS mode:<p><p></p></p><p><p><strong>[cpp]</strong> View plaincopy</p></p> <ol> <li><li><p>#define ServerHost @ "192.168.1.84:5558"</p></li></li> <li><li><p></p></li></li> <li><li><p>-(nsdictionary *) getdatafromurl: (nsstring *) Path params: (nsdictionary *) data</p></li></li> <li><li><p>{</p></li></li> <li><li><p>NSLog (@ "mknetwork request URL:%@%@ \ n Data:%@", serverhost,path,data);</p></li></li> <li><li><p></p></li></li> <li><li><p>__block nsdictionary *responsejson;</p></li></li> <li><li><p>__block Nserror *error = nil;</p></li></li> <li><li><p>Mknetworkengine *engine = [[mknetworkengine alloc] initwithhostname:serverhost customheaderfields:nil];</p></li></li> <li><li><p>Mknetworkoperation *op = [engine operationwithpath:path params:data httpmethod:@ "POST" ssl:yes];</p></li></li> <li><li><p>Add a certificate in the request</p></li></li> <li><li><p>Op.clientcertificate = [[[nsbundle mainbundle] resourcepath] stringbyappendingpathcomponent:@ "client.p12"];</p></li></li> <li><li><p>Op.clientcertificatepassword = @ "test";</p></li></li> <li><li><p>Whether to continue access when the Server-side certificate is not valid</p></li></li> <li><li><p>op.shouldcontinuewithinvalidcertificate=yes;</p></li></li> <li><li><p>[op addcompletionhandler:^ (mknetworkoperation *operation) {</p></li></li> <li><li><p>NSLog (@ "[operation responsedata]-->>%@", [operation responsestring]);</p></li></li> <li><li><p>Responsejson=[nsjsonserialization jsonobjectwithdata:[operation responsedata] options:kniloptions error:&error ];</p></li></li> <li><li><p>If (error) {</p></li></li> <li><li><p>NSLog (@ "jsonserialization failed!-error:%@", error);</p></li></li> <li><li><p>};</p></li></li> <li><li><p>error=nil;</p></li></li> <li><li><p>} errorhandler:^ (mknetworkoperation *errorop, nserror* err) {</p></li></li> <li><li><p>error=err;</p></li></li> <li><li><p>}];</p></li></li> <li><li><p>[engine enqueueoperation:op];</p></li></li> <li><li><p>While (!error&&!responsejson) {}</p></li></li> <li><li><p>If (error) {</p></li></li> <li><li><p>NSLog (@ "mknetwork Request error:%@", error);</p></li></li> <li><li><p>Return nil;</p></li></li> <li><li><p>}</p></li></li> <li><li><p>If (responsejson) {</p></li></li> <li><li><p>NSLog (@ "jsonserialization successed!-responsejson:%@", responsejson);</p></li></li> <li><li><p>}</p></li></li> <li><li><p>Return responsejson;</p></li></li> <li><li><p>}</p></li></li> <li><li><p></p></li></li> <li><li><p></p></li></li> <li><li><p>Nsdictionary *params=[nsdictionary dictionarywithobjectsandkeys:@ "admin", @ "userName", @ "123", @ "password", nil];</p></li></li> <li><li><p>Nsdictionary *responsedict = [self getdatafromurl:@ "/login" params:params];</p></li></li> </ol>Download file:<p><p></p></p><p><p><strong>[cpp]</strong> View plaincopy</p></p> <ol> <li><li><p>+ (mknetworkoperation*) downloadfatassfilefrom: (nsstring*) remoteurl tofile: (nsstring*) FilePath {</p></li></li> <li><li><p>Mknetworkengine *engine = [[mknetworkengine alloc] initwithhostname:@ "127.0.0.1:5558" customheaderfields:nil];</p></li></li> <li><li><p>Mknetworkoperation *op = [engine Operationwithurlstring:remoteurl</p></li></li> <li><li><p>Params:nil</p></li></li> <li><li><p>httpmethod:@ "GET"];</p></li></li> <li><li><p></p></li></li> <li><li><p>[op Adddownloadstream:[nsoutputstream Outputstreamtofileatpath:filepath</p></li></li> <li><li><p>append:yes]];</p></li></li> <li><li><p>[engine enqueueoperation:op];</p></li></li> <li><li><p>Return op;</p></li></li> <li><li><p>}</p></li></li> <li><li><p>+ (void) testdownload{</p></li></li> <li><li><p>Nsarray *paths = nssearchpathfordirectoriesindomains (nscachesdirectory, nsuserdomainmask, YES);</p></li></li> <li><li><p>NSString *cachesdirectory = [paths objectatindex:0];</p></li></li> <li><li><p>NSString *downloadpath = [cachesdirectory stringbyappendingpathcomponent:@ "downloadedfile.pdf"];</p></li></li> <li><li><p></p></li></li> <li><li><p>Mknetworkoperation *downloadoperation=[httpmanager downloadfatassfilefrom:@ "http://127.0.0.1:5558/QQ"</p></li></li> <li><li><p>tofile:downloadpath];</p></li></li> <li><li><p></p></li></li> <li><li><p>[downloadoperation ondownloadprogresschanged:^ (double Progress) {</p></li></li> <li><li><p>Download progress</p></li></li> <li><li><p>NSLog (@ "download progress:%.2f", progress*100.0);</p></li></li> <li><li><p>}];</p></li></li> <li><li><p>Event handling</p></li></li> <li><li><p>[downloadoperation addcompletionhandler:^ (mknetworkoperation* Completedrequest) {</p></li></li> <li><li><p>NSLog (@ "download File finished!");</p></li></li> <li><li><p>} errorhandler:^ (mknetworkoperation *errorop, nserror* err) {</p></li></li> <li><li><p>NSLog (@ "download file error:%@", err);</p></li></li> <li><li><p>}];</p></li></li> <li><li><p>}</p></li></li> </ol>Upload File:<p><p></p></p><p><p><strong>[cpp]</strong> View plaincopy</p></p> <ol> <li><li><p>+ (mknetworkoperation*) uploadimagefromfile: (nsstring*) filePath mimeType: (nsstring *) filetype{</p></li></li> <li><li><p>Mknetworkengine *engine = [[mknetworkengine alloc] initwithhostname:@ "127.0.0.1:5558" customheaderfields:nil];</p></li></li> <li><li><p>Mknetworkoperation *op = [engine operationwithpath:@ "upload"</p></li></li> <li><li><p>Params:[nsdictionary dictionarywithobjectsandkeys:</p></li></li> <li><li><p>@ "admin", @ "username",</p></li></li> <li><li><p>@ "123", @ "password", nil]</p></li></li> <li><li><p>httpmethod:@ "POST"];</p></li></li> <li><li><p></p></li></li> <li><li><p>[op Addfile:filepath forkey:@ "media" mimetype:filetype];</p></li></li> <li><li><p></p></li></li> <li><li><p>Setfreezable uploads your images after connection is restored!</p></li></li> <li><li><p>[op setfreezable:yes];</p></li></li> <li><li><p></p></li></li> <li><li><p>[op addcompletionhandler:^ (mknetworkoperation* Completedoperation) {</p></li></li> <li><li><p></p></li></li> <li><li><p>NSString *responsestring = [completedoperation responsestring];</p></li></li> <li><li><p>NSLog (@ "server response:%@", responsestring);</p></li></li> <li><li><p>} errorhandler:^ (mknetworkoperation *errorop, nserror* err) {</p></li></li> <li><li><p></p></li></li> <li><li><p>NSLog (@ "Upload file error:%@", err);</p></li></li> <li><li><p>}];</p></li></li> <li><li><p></p></li></li> <li><li><p>[engine enqueueoperation:op];</p></li></li> <li><li><p></p></li></li> <li><li><p>Return op;</p></li></li> <li><li><p>}</p></li></li> <li><li><p></p></li></li> <li><li><p>+ (void) testupload{</p></li></li> <li><li><p>NSString *uploadpath = [[[nsbundle mainbundle] resourcepath] stringbyappendingpathcomponent:@ "SampleImage.jpg"];</p></li></li> <li><li><p>NSData *mydata = [nsdata datawithcontentsoffile:uploadpath];</p></li></li> <li><li><p>NSLog (@ ">>>>>>>>%@", myData);</p></li></li> <li><li><p>Mknetworkoperation *uploadoperation = [httpmanager Uploadimagefromfile:uploadpath mimeType:@ "jpg"];</p></li></li> <li><li><p>[uploadoperation onuploadprogresschanged:^ (double Progress) {</p></li></li> <li><li><p>Upload Progress</p></li></li> <li><li><p>DLog (@ "Upload file progress:%.2f", progress*100.0);</p></li></li> <li><li><p>}];</p></li></li> <li><li><p>}</p></li></li> </ol><p><p></p></p>Server-side program (node. Js) when uploading files:<p><p><strong>[javascript]</strong> View plaincopy</p></p> <ol> <li><li><p>var express = require (' Express ')</p></li></li> <li><li><p>, Fs=require (' FS ');</p></li></li> <li><li><p></p></li></li> <li><li><p>var app = Module.exports = Express.createserver ();</p></li></li> <li><li><p>Configuration</p></li></li> <li><li><p>App.configure (function () {</p></li></li> <li><li><p>App.use (express.bodyparser ());</p></li></li> <li><li><p>App.use (express.methodoverride ());</p></li></li> <li><li><p>App.use (app.router);</p></li></li> <li><li><p>});</p></li></li> <li><li><p>Routes</p></li></li> <li><li><p>App.post ('/upload ', function (req, res) {</p></li></li> <li><li><p>Console.log (req);</p></li></li> <li><li><p>var Tmp_path = req.files.media.path; Get a temporary path to a file</p></li></li> <li><li><p>var Target_path = './' +req.files.media.name;//specifies The directory after the file is uploaded</p></li></li> <li><li><p>Fs.rename (tmp_path, target_path, function (err) {//move file</p></li></li> <li><li><p>If (err) throw err;</p></li></li> <li><li><p>Fs.unlink (tmp_path, function () {//delete Temp folder file,</p></li></li> <li><li><p>If (err) throw err;</p></li></li> <li><li><p>Res.send ({server: ' Success '});</p></li></li> <li><li><p>Res.end ();</p></li></li> <li><li><p>});</p></li></li> <li><li><p>});</p></li></li> <li><li><p>});</p></li></li> <li><li><p>App.listen (5558);</p></li></li> <li><li><p>Console.log ("Express Server listening on port%d in%s mode", app.address (). port, app.settings.env);</p><p></p></li></li> </ol><p><p>Use of iOS Mknetworkkit</p></p></span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.