IOS Seven cow cloud upload and get pictures----"Client"

Source: Internet
Author: User
Tags cdata hmac sha1 sha1 encryption

Recently made seven cow cloud storage of the relevant content, involving with the background to obtain authentication token, helpless, backstage Zimingqinggao, not with the attention, no way ah, so I make Bai. First of all, register an account with seven cows, and then add a storage space this time the space name is used to identify the location of storage space, and the choice of the region is to set storage seven cattle server location, then choose a public space free trial can be ~ ~

Create a good account is to see the official document writing demo process, incidentally, with a: http://developer.qiniu.com/code/v7/sdk/objc.html download to see the next seven cows about iOS development demo use Cocopod Managed by. My goodness, the recent project has been useless, worrying that the problem of updating will not be able to use ... Indeed, because the Ruby version of the software is too low, can not update the third-party library, had to upgrade a third-party library, the specific upgrade method please refer to http://www.cnblogs.com/gfxxbk/p/5536397.html ... This will soon be updated, but there are also some of the problems involved in the upgrade failure, generally there will be error prompts, the corresponding search can be resolved. I upgrade when the version number is too high, resulting in the corresponding class library has not been found, so the upgrade should not write too high, 2.2.2 version is good by the way with some workarounds http://blog.csdn.net/springjustin/article/details/51836968

Finally, all the environmental needs are ready, then directly into the chant, when the pod after the import, because the previous upgrade, so generally do not appear in the document mentioned in the error, very smooth introduction of this class library. The key is to get verified tokens in the official documents this token is to be obtained from their own background server, no way, the background does not cooperate can only self-clothed.
First, this token is used for verification, and the official recommendation is to use the client after using the background build. But the client can also generate a specific build method as follows:

-(void) Createtoken {if(!self.scope.length | |!self.accesskey.length | |!self.secretKey.length) {return; }        //serializes Scrop and deadline in the upload policy into JSON formatNsmutabledictionary *authinfo =[Nsmutabledictionary dictionary]; [AuthInfo setObject:self.scope Forkey:@"Scope"]; [AuthInfo setobject:[nsnumber numberwithlong:[[nsdate Date] timeIntervalSince1970]+ Self.livetime * -*3600] Forkey:@"deadline"]; NSData*jsondata =[nsjsonserialization datawithjsonobject:authinfo options:nsjsonwritingprettyprinted Error:nil]; //URL-Safe base64 encoding of JSON serialized post-upload policyNSString *encodedstring =[self urlsafebase64encode:jsondata]; //using Secretkey to HMAC-SHA1 encryption of the encoded upload strategy, and make the security Base64 code, get encoded_signedNSString *encodedsignedstring =[self HMACSHA1:self.secretKey text:encodedstring]; //accesskey, encodedsignedstring, and encodedstring are stitched together in the middle with: separate, the token that is uploadedNSString *token =[NSString stringWithFormat:@"%@:%@:%@", Self.accesskey, encodedsignedstring, encodedstring]; Self.uploadtoken=token; }

The call to the function is: URL-safe base64 encoding:

-(NSString *) Urlsafebase64encode: (NSData *) Text {nsstring*base64 =[[NSString alloc] initwithdata:[qn_gtm_base64 Encodedata:text] encoding:nsutf8stringencoding]; Base64= [Base64 stringbyreplacingoccurrencesofstring:@"+"Withstring:@"-"]; Base64= [Base64 stringbyreplacingoccurrencesofstring:@"/"Withstring:@"_"]; returnbase64;}

To HMAC-SHA1 encryption in the upload policy:

-(NSString *) HMACSHA1: (NSString *) key text: (NSString *) text {    constchar *ckey = [ Key cstringusingencoding:nsutf8stringencoding];     Const char *cdata = [text cstringusingencoding:nsutf8stringencoding];         Char Chmac[cc_sha1_digest_length];        Cchmac (kCCHmacAlgSHA1, Ckey, strlen (Ckey), CDATA, strlen (CDATA), Chmac);         *hmac = [[NSData alloc] Initwithbytes:chmac length:cc_sha1_digest_length];     *hash = [self urlsafebase64encode:hmac];     return Hash;}

Three parameters are mentioned above, namely: scope, AccessKey, Secretkey, these three parameters are obtained when we apply for account and create space, first argument: scope: Is the name of the space, the two key is in the personal panel-- Personal center-Key management inside can be seen as follows:

This gets the token to write the method is much simpler, the following is for uploading pictures to do a package method:

-(void) Uploadimagetoqnfilepath: (NSString *) FilePath {Qnuploadmanager*upmanager =[[Qnuploadmanager alloc] init]; Qnuploadoption*uploadoption = [[Qnuploadoption alloc] Initwithmime:nil progresshandler:^ (NSString *key,floatpercent) {NSLog (@"Upload Progress%.2f", percent); }                                                                 params: Nil Checkcrc:no    Cancellationsignal:nil]; [Upmanager putfile:filepath key:nil token:self.uploadToken Complete:^ (Qnresponseinfo *info, NSString *key, Nsdictionary *resp) {                //send a notification to users to get pictures usingNsnotification * Notice =[nsnotification notificationwithname:@"DownLoad" Object: nil Userinfo:resp];            [[Nsnotificationcenter Defaultcenter] postnotification:notice];        } Option:uploadoption]; }

so that we can upload the picture, and we'll have to hit two. The return value is in the form of the following:

This time we can take this return value, splicing us from the seven cow cloud background to get the file path for splicing use Sdwebimage can get our uploaded pictures

By the way, when the interpretation of the dictionary from the notification to pay attention to the correct way of parsing, or will error.

IOS Seven cow cloud upload and get pictures----"Client"

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.