iOS 中试用 Python3

使用版本 python3.5.1

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

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

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

4.工程中,添加库:

  • liblzma.tbd
  • libbz2.tbd
  • libz.tbd
  • libsqlite3.0.tbd
  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

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 在此特此声明:一下所有链接均来自互联网,在此记录下我的查阅学习历程,感谢各位原创作者的无私奉献 ! 技术一点一点积...
    远航的移动开发历程阅读 13,867评论 12 197
  • 获取相册数据 http://kayosite.com/ios-development-and-detail-of-...
    0271fb6f797c阅读 5,497评论 0 1
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,193评论 4 61
  • 日目标:1800 当月目标:54000 当日销售:4500 现在销售:64000 当日差比:2700 当月差比:1...
    c6397a55fadf阅读 994评论 0 0
  • 生活中那些被我们忽略的平凡在这里被重新装点。 可爱呆萌的画风加上轻快的文字, 它们也许不能帮你解决什么难题, 却能...
    花名佛手阅读 4,421评论 0 0