游戏接入腾讯云iOS版

需求:项目需要把玩家截图上传到CDN,以后玩家能随时看见自己DIY的图。
安卓3天搞定代码,不懂OC语法,磕磕碰碰摸索了2周的时间才写完了iOS版本,拿去能直接用。
当然前提是需要按照腾讯云官网链接接入SDK文档:https://cloud.tencent.com/document/product/436/11280

我用的是手动集成。


image.png

自己弄了2个OC文件。

QCloudCOSXMLDemo.h

#import 

@interface QCloudCOSXMLDemo : NSObject 

- (void) registerCustomService;
- (void) putObject :(NSString*) urlStr cosStr:(NSString*) cosPath;
- (void) headerObject :(NSString*) cosPath;
@end

QCloudCOSXMLDemo.mm

#import "QCloudCOSXMLDemo.h"

#define UnityPlayerMessage "UnityPlayerMessage"

@implementation QCloudCOSXMLDemo
- (void)signatureWithFields:(QCloudSignatureFields *)fileds request:(QCloudBizHTTPRequest *)request urlRequest:(NSMutableURLRequest *)urlRequst compelete:(QCloudHTTPAuthentationContinueBlock)continueBlock {
    
    QCloudCredential* credential = [QCloudCredential new];
    credential.secretID = @"secretID";
    credential.secretKey = @"secretKey";
    QCloudAuthentationV5Creator* creator = [[QCloudAuthentationV5Creator alloc] initWithCredential:credential];
    QCloudSignature* signature =  [creator signatureForData:urlRequst];
    continueBlock(signature, nil);
    
}

- (void) registerCustomService
{
    QCloudServiceConfiguration* configuration = [QCloudServiceConfiguration new];
    configuration.appID = @"appID";
    configuration.signatureProvider = self;
    
    QCloudCOSXMLEndPoint* endpoint = [[QCloudCOSXMLEndPoint alloc] init];
    endpoint.regionName = @"ap-guangzhou";
    endpoint.useHTTPS = true;
    configuration.endpoint = endpoint;
    
    [QCloudCOSXMLService registerDefaultCOSXMLWithConfiguration:configuration];
    [QCloudCOSTransferMangerService registerDefaultCOSTransferMangerWithConfiguration:configuration];
    NSLog(@"注册QCloudCOSXMLService");
}

- (void) putObject :(NSString*) urlStr cosStr:(NSString*) cosPath
{
    QCloudPutObjectRequest* put = [QCloudPutObjectRequest new];
    put.object = cosPath;
    put.bucket = @"bucket";
    put.regionName = @"ap-guangzhou";
    NSURL *url = [NSURL fileURLWithPath:urlStr];
    put.body =  url;
    [put setFinishBlock:^(id outputObject, NSError *error) {
        NSMutableDictionary *data = [NSMutableDictionary dictionary];
           [data setValue:urlStr forKey:@"localPath" ];
           [data setValue:cosPath forKey:@"cosPath"];
           if(error.code == 0)
           {
              NSLog(@"成功上传图片");
               UnitySendMessage(UnityPlayerMessage, "Message_UpLoadPicToTXY", "");
           }else
           {
               [data setValue: error.domain forKey:@"message"];
               NSLog(@"失败上传图片");
           }
    }];
    [[QCloudCOSXMLService defaultCOSXML] PutObject:put];
}

- (void) headerObject :(NSString*) cosPath
{
    QCloudHeadObjectRequest* headerRequest = [QCloudHeadObjectRequest new];
    headerRequest.object = cosPath;
    headerRequest.bucket = @"bucket";
    headerRequest.regionName = @"ap-guangzhou";
    [headerRequest setFinishBlock:^(NSDictionary* result, NSError *error) {
        NSLog(@"查询数据,result %@ ",result);
        
        if(error.code == 0)
        {
            NSLog(@"查询数据成功");
            
            NSString *key = @"Etag";
            NSString *value = [result objectForKey: key];
            
            NSString *gameRet1 = [NSString stringWithFormat:@"1|%@|%@", cosPath,value];
            char * myCstring1 = (char*) [gameRet1 cStringUsingEncoding:NSUTF8StringEncoding];
            
            UnitySendMessage(UnityPlayerMessage, "Message_SearchPicTxYun", myCstring1);
        }else
        {
            NSString *gameRet2 = [NSString stringWithFormat:@"0|%@|0", cosPath];
            char * myCstring2 = (char*) [gameRet2 cStringUsingEncoding:NSUTF8StringEncoding];
            
            UnitySendMessage(UnityPlayerMessage, "Message_SearchPicTxYun", myCstring2);
            NSLog(@"查询数据失败");
        }
        
    }];
    
    [[QCloudCOSXMLService defaultCOSXML] HeadObject:headerRequest];
}


@end

调用比较简单的噢,需要注意的就是,在刚进游戏的时候需要调用一下注册函数txYunRegister。

QCloudCOSXMLDemo* demo;
void txYunRegister()
{
    demo = [QCloudCOSXMLDemo new];
    [demo registerCustomService];
}
void upLoadPicTxYun(char *resPath,char *cosPath)
{
    NSString *resPatht = [NSString stringWithUTF8String:resPath];
    NSString *cosPatht = [NSString stringWithUTF8String:cosPath];
    [demo putObject:resPatht cosStr:cosPatht];
}
void searchPicTxYun(char *cosPath)
{
    NSString *cosPatht = [NSString stringWithUTF8String:cosPath];
    [demo headerObject:cosPatht];
}

安卓如果遇到困难也可私信我。

你可能感兴趣的:(游戏接入腾讯云iOS版)