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)
{

NSString *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];

}

iOS使用UIWebView遇到Error Domain=WebKitErrorDomain Code=101 “The operation couldn’t be completed. (WebKitErrorDomain error 101

Filed under: iOS,Web — 标签:, , — lizongbo @ 16:28

现在在接触iOS开发,今天在调试一个界面加载web页面的问题,发现死活无法加载,浏览器里能正常打开,加上相应代码之后得到了错误信息为:

2013-04-18 15:05:06.446 Client_DEMO[22889:1a303] webview didFailLoadWithError <UIWebView: 0x18ff2160; frame = (18 70; 315 408); autoresize = W+H; layer = <CALayer: 0xc1140a0>> , and err is Error Domain=WebKitErrorDomain Code=101 “The operation couldn’t be completed. (WebKitErrorDomain error 101.)”

加日志的办法为:

WebviewDelegat.h 代码如下:

#import <UIKit/UIKit.h>

@interface WebviewDelegate : NSObject <UIWebViewDelegate>

@end

WebviewDelegat.mm 代码如下:

#import “WebviewDelegate.h”

@implementation WebviewDelegate
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
NSString * str = [[request URL] absoluteString];
NSString *urlStr = [str stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

NSArray * strArray = [urlStr componentsSeparatedByString:@”:”];
if([strArray count]>2)
{
NSLog(@”webview for %@”,urlStr);
if ([[strArray objectAtIndex:0] isEqualToString:@”jscall”] ) {
//这里拦截做些本地的事情
}
return NO;
}else{
return YES;
}

}
– (void)webViewDidStartLoad:(UIWebView *)webView{
NSLog(@”webview webViewDidStartLoad %@”,webView.debugDescription);
}
– (void)webViewDidFinishLoad:(UIWebView *)webView{
NSLog(@”webview webViewDidFinishLoad %@”,webView.debugDescription);
}
– (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{

//把出错信息打出来
NSLog(@”webview didFailLoadWithError %@ , and err is %@”,webView.debugDescription, error.debugDescription);
}

@end

通过UIWebView的setDelegate方法将其绑定。

 

网上搜索之后得知是urlencode有问题,仔细核实了一下url,发现url里有个参数值有竖线,于是知道了是竖线未转义导致。

比如: http://lizongbo.com/?chid=0|0|618119, 应该为:http://lizongbo.com/?chid=0%7C0%7C618119

解决办法很简单,对url的字符串做了个替换:

sUrl=replaceStr(sUrl,”|”,”%7C”);//不转义的话会报错,nd err is Error Domain=WebKitErrorDomain Code=101 “The operation couldn’t be completed. (WebKitErrorDomain error 101.)”

参考:http://stackoverflow.com/questions/2028379/uiwebkit-error-101

 

 

 

Powered by WordPress