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

 

 

 

2013年03月19日

cygwin上编译protobuf遇到No suitable threading library available的解决办法

Filed under: Android — 标签:, , , , — lizongbo @ 09:46

打算在Android的ndk中使用protobuf,参考了网上的说明,结果在cygwin下编译protobuf遇到”No suitable threading library available.”

以下是运行make时的错误信息:

/bin/sh ../libtool –tag=CXX –mode=compile g++ -DHAVE_CONFIG_H -I. -I.. -Wall -Wwrite-strings -Woverloaded-virtual -Wno-sign-compare -O2 -g -DNDEBUG -MT common.lo -MD -MP -MF .deps/common.Tpo -c -o common.lo `test -f ‘google/protobuf/stubs/common.cc’ || echo ‘./’`google/protobuf/stubs/common.cc
libtool: compile: g++ -DHAVE_CONFIG_H -I. -I.. -Wall -Wwrite-strings -Woverloaded-virtual -Wno-sign-compare -O2 -g -DNDEBUG -MT common.lo -MD -MP -MF .deps/common.Tpo -c google/protobuf/stubs/common.cc -DDLL_EXPORT -DPIC -o .libs/common.o
google/protobuf/stubs/common.cc:48:2: error: #error “No suitable threading library available.”
Makefile:1184: recipe for target `common.lo’ failed
make[2]: *** [common.lo] Error 1
make[2]: 离开目录“/cygdrive/d/cpplibs/protobuf-2.5.0/src”
Makefile:568: recipe for target `all-recursive’ failed
make[1]: *** [all-recursive] Error 1
make[1]: 离开目录“/cygdrive/d/cpplibs/protobuf-2.5.0”
Makefile:477: recipe for target `all’ failed
make: *** [all] Error 2

编译的步骤其实非常简单:

网上下载最新的protobuf文件:

http://protobuf.googlecode.com/files/protobuf-2.5.0.zip
来源:http://code.google.com/p/protobuf/

解压到:D:\cpplibs得到:D:\cpplibs\protobuf-2.5.0

然后cygwin下依次运行以下命令:

 

lizongbo@PC_LI ~
$ cd /cygdrive/d/cpplibs/protobuf-2.5.0/
lizongbo@PC_LI /cygdrive/d/cpplibs/protobuf-2.5.0
$ ./configure

lizongbo@PC_LI /cygdrive/d/cpplibs/protobuf-2.5.0
$ make

lizongbo@PC_LI /cygdrive/d/cpplibs/protobuf-2.5.0
$ make check

lizongbo@PC_LI /cygdrive/d/cpplibs/protobuf-2.5.0
$ make install

正常情况下就编译ok了,结果我运行make的是提示如下出错信息:

/bin/sh ../libtool –tag=CXX –mode=compile g++ -DHAVE_CONFIG_H -I. -I.. -Wall -Wwrite-strings -Woverloaded-virtual -Wno-sign-compare -O2 -g -DNDEBUG -MT common.lo -MD -MP -MF .deps/common.Tpo -c -o common.lo `test -f ‘google/protobuf/stubs/common.cc’ || echo ‘./’`google/protobuf/stubs/common.cc
libtool: compile: g++ -DHAVE_CONFIG_H -I. -I.. -Wall -Wwrite-strings -Woverloaded-virtual -Wno-sign-compare -O2 -g -DNDEBUG -MT common.lo -MD -MP -MF .deps/common.Tpo -c google/protobuf/stubs/common.cc -DDLL_EXPORT -DPIC -o .libs/common.o
google/protobuf/stubs/common.cc:48:2: error: #error “No suitable threading library available.”
Makefile:1184: recipe for target `common.lo’ failed
make[2]: *** [common.lo] Error 1
make[2]: 离开目录“/cygdrive/d/cpplibs/protobuf-2.5.0/src”
Makefile:568: recipe for target `all-recursive’ failed
make[1]: *** [all-recursive] Error 1
make[1]: 离开目录“/cygdrive/d/cpplibs/protobuf-2.5.0”
Makefile:477: recipe for target `all’ failed
make: *** [all] Error 2

 

在网上找遍了资料也没找到解决办法,然后查看common.cc对应代码为:

#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN  // We only need minimal includes
#include <windows.h>
#define snprintf _snprintf    // see comment in strutil.cc
#elif defined(HAVE_PTHREAD)
#include <pthread.h>
#else
#error “No suitable threading library available.”
#endif

也是是非win32平台且找不到pthread的情况下才出这个问题。

查看运行configure的记录则是:

checking for library containing zlibVersion… -lz
checking for the pthreads library -lpthreads… no
checking whether pthreads work without any flags… no
checking whether pthreads work with -Kthread… no
checking whether pthreads work with -kthread… no
checking for the pthreads library -llthread… no
checking whether pthreads work with -pthread… no
checking whether pthreads work with -pthreads… no
checking whether pthreads work with -mthreads… no
checking for the pthreads library -lpthread… no
checking whether pthreads work with –thread-safe… no
checking whether pthreads work with -mt… no
checking for pthread-config… no

但网上有人说了cygwin是自带pthread库的(

Pthread is included in Cygwin.  参考:http://stackoverflow.com/questions/2087435/some-issues-about-cygwinlinux-in-windows-socket-thread-other-programming-and  )

尝试各种方法之后仍然失败。

晚上在家里用电脑再按上述步骤操作,发现编译ok了。

再次尝试后总结出了原因,是因为我在环境变量里Path中增加了MinGW的bin目录,

在安装cygwin之前刚好先安装了minGW,且配置了以下环境变量:
MINGW_HOME=C:\MinGW
LIBRARY_PATH = %MINGW_HOME%\lib
C_INCLUDE_PATH = %MINGW_HOME%\include
在PATH变量的最后增加 ;%MINGW_HOME%\bin

于是把上面几个环境变量去掉,重新进入cygwin,终于编译ok了。

正常编译情况下运行configure的记录则是:

checking for the pthreads library -lpthreads… no
checking whether pthreads work without any flags… yes
checking for joinable pthread attribute… PTHREAD_CREATE_JOINABLE
checking if more special flags are required for pthreads… no
checking whether to check for GCC pthread/shared inconsistencies… no
checking whether what we have so far is sufficient with -nostdlib… no
checking whether -lpthread saves the day… no
configure: WARNING: Impossible to determine how to use pthreads with shared libr
aries and -nostdlib

 

 

 

D:\cpplibs\protobuf-2.5.0\src\.libs下顺利得到了libprotobuf.a,libprotobuf-lite.a,libprotoc.a等文件。

D:\cygwin\usr\local\include则是protobuf的相应头文件。

 

 

Older Posts »

Powered by WordPress