iOS 中试用 Python3

2019-02-27  本文已影响0人  苹果农

使用版本 python3.5.1

  1. 下载Python-iOS
    地址:
    https://github.com/pybee/Python-iOS-support/releases

  2. 将两个framework拖入工程中(python.framework , OpenSSL.framework)

  3. 编译。。。不通过,一堆错。

4.工程中,添加库:

  1. 再编译,通过。

  2. 撸码
    撸码前 把 python.framework 添加到 bundle 中

wchar_t *getWCharFromString(NSString *inStr)
{
  const char *cString = [inStr cStringUsingEncoding:NSUTF8StringEncoding];
  setlocale(LC_CTYPE, "UTF-8");
  wchar_t *outStr = NULL;
  size_t size = mbstowcs(NULL, cString, 0);
  outStr = new wchar_t[size + 1];
  if (outStr) {
    memset(outStr, 0, size * sizeof(wchar_t));
    size_t ret = mbstowcs(outStr, cString, size+1);
    if (ret == -1) {
      delete[] outStr;
      outStr = NULL;
    }
  }
  return outStr;
}

int initPython(void)
{
  NSBundle *bundle = [NSBundle mainBundle];
  NSString *frameworkPath = [bundle pathForResource:@"Python.framework" ofType:@""];
  NSString *home = [NSString stringWithFormat:@"%@/Resources",frameworkPath];
  wchar_t  *pythonHome = getWCharFromString(home);
  Py_SetPythonHome(pythonHome);
  Py_Initialize();
  if ( !Py_IsInitialized() ) {
    return -1;
  }
  PyRun_SimpleString("print('hello py')");
  return 0;
}

int finalizePython(void)
{
  Py_Finalize();
  return 0;
}

参考:
https://blog.csdn.net/u013158533/article/details/84001175
https://www.jianshu.com/p/80b5be51fb1d
https://blog.csdn.net/leonpengweicn/article/details/7433593

上一篇下一篇

猜你喜欢

热点阅读