美文网首页
OC集成百度翻译

OC集成百度翻译

作者: ytsq | 来源:发表于2017-08-04 11:20 被阅读0次

    APPID,APPKEY去百度翻译SDK网站申请
    需要使用AFN进行网络请求

    //百度翻译
    -(void)TransStr:(NSString *)str ToLanguage:(NSString *)language
    {
        NSString *q = str;//要翻译的内容
        NSString *from = @"auto";自动检测需要翻译的语言是哪国语言
        NSString *to = language;//要翻译成哪国语言
        int salt = 1435660288;//随便写
        
        NSString *sign = [NSString stringWithFormat:@"%ld%@%d%@",APPID,q,salt,APPKEY];
        
        NSString *md5Sign = [self md5:sign];//MD5加密
        
        NSString *URLQ = [q stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];//转化一下
        NSString *url = [NSString stringWithFormat:@"http://api.fanyi.baidu.com/api/trans/vip/translate?q=%@&from=%@&to=%@&appid=20170206000038634&salt=1435660288&sign=%@",URLQ,from,to,md5Sign];
        
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];//AFN网络请求
        
        [manager GET:url parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSDictionary *dict = (NSDictionary *)responseObject;
            NSArray *result = dict[@"trans_result"];
            NSDictionary *dd = [result firstObject];
            NSString *str = dd[@"dst"];//翻译结果
            # 做你想做的
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
        }];
    }
    //md5 加密
    - (NSString *) md5:(NSString *) input {
        const char *cStr = [input UTF8String];
        unsigned char digest[CC_MD5_DIGEST_LENGTH];
        CC_MD5( cStr, (CC_LONG)strlen(cStr), digest ); // This is the md5 call
        
        NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
        
        for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
            [output appendFormat:@"%02x", digest[i]];
        
        return  output;
    }
    

    demo

    相关文章

      网友评论

          本文标题:OC集成百度翻译

          本文链接:https://www.haomeiwen.com/subject/hqjvlxtx.html