美文网首页
iOS 中试用 Python3

iOS 中试用 Python3

作者: 苹果农 | 来源:发表于2019-02-27 15:46 被阅读0次

    使用版本 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

    相关文章

      网友评论

          本文标题:iOS 中试用 Python3

          本文链接:https://www.haomeiwen.com/subject/bswayqtx.html