lizongbo at 618119.com 工作,生活,Android,前端,Linode,Ubuntu,nginx,java,apache,tomcat,Resin,mina,Hessian,XMPP,RPC

2013年04月18日

iOS中NSString转成GBK的c++ string的方法

Filed under: iOS — 标签:, , , — lizongbo @ 16:53

客户端为了跨平台使用c++代码开发,但需要集成苹果的IAP支付。

因此需要将支付失败的出错信息在c++代码写的对话框中显示出来,但苹果的出错信息NSString里默认是UTF8的,与GBK字符串简单拼接字符串显示出来的是乱码,经过尝试解决办法是使用kCFStringEncodingGB_18030_2000。

代码如下:

case SKPaymentTransactionStateFailed:
{
NSLog(@”the error %@”, transaction.error);
NSLog(@”[error localizedDescription] %@”,[transaction.error localizedDescription]);
NSLog(@”transId %@”,transaction.transactionIdentifier);
if(_listener)
{

*domain = @”SKErrorDomain”;
std::string s = “购买失败”;
if([transaction.error.domain isEqualToString:domain])
{
switch(transaction.error.code)
{
case SKErrorUnknown:
{s = “购买失败,出错原因为:”;
NSStringEncoding encGBK = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
s.append([[transaction.error localizedDescription] cStringUsingEncoding:encGBK]);

//转成GBK,此时的出信息一般是:“无法连接iTunes Store”。
}
break;
case SKErrorClientInvalid:
s = “购买失败(客户端验证错误)”;
break;
case SKErrorPaymentInvalid:
s = “购买失败(支付验证错误)”;
break;
case SKErrorPaymentNotAllowed:
s = “购买失败(不允许支付)”;
break;
case SKErrorPaymentCancelled:
s = “取消购买”;
break;
}
}
else if([transaction.error.domain isEqualToString:@”NSURLErrorDomain”])
{
s = “网络连接超时”;
}

_proxy-> showErrMsg(transaction.error.code,  s);//弹本地界面显示出错信息
}

 

 

同样,如果是c++中的gbk字符串想要在弹出iOS的本地对话框,则需要从GBK到NSString .

 

示例代码如下:

 

void AlertViewBridge::showDialog(std::string title,
std::string msg,
std::string cancelBtnText)
{
NSStringEncoding encGBK = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
NSString* stitle  = [NSString stringWithCString:title.c_str() encoding:encGBK];

NSString* smsg= [NSString stringWithCString:msg.c_str() encoding:encGBK];

NSString* scancelBtnText= [NSString stringWithCString:cancelBtnText.c_str() encoding:encGBK];

 

UIAlertView *alertView= [[UIAlertView alloc] initWithTitle:stitle
message:smsg
delegate:nil
cancelButtonTitle:scancelBtnText
otherButtonTitles:nil];
[alertViewshow];
[alertView release];

}

没有评论 »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress