首先我们需要编译 lua 环境,得到 lua 工程的静态库
1. 到 http://www.lua.org/ftp/ 下载 lua 源码,版本任意
2. 解压后 的 res 文件夹便是我们的需要的 lua 源码
3. 新建一个静态库工程 ,打开 xcode —> File —>New —>Project –> 选择 Static Libary,并且工程名命名为 lua
4. 将 2 步骤中的 res 文件放到工程,Product->Build 便可以编出我们需要的 .a 包
5. 新建一个 iOS 工程
比如新建一个名字为 LuaOniOS 的工程。
6. 将 iOS 工程引用 lua 静态库工程。
因静态库工程是 iOS 工程的子工程,为管理方便,将其放到到 LuaOniOS 工程文件夹下
- 步骤 1 将 静态库工程的 lua.xcodeproj 文件拖拽到 iOS 工程中
- 步骤 2 设置 LuaOniOS 的 Build Settings –> Header Search Paths 添加一条 $(SRCROOT)/LuaOniOS/Lua/Lua 目的是搜索 lua 头文件时能够索引得到
- 步骤 2 设置 LuaOniOS 的 Build Phases –>Link Binary With Libraries 添加 libLua.a 目的是包含子工程编译出来的静态库
7. 在工程中新加一个 test.lua
- print("hello lua on ios")
然后在 ViewController.m 中编写
- #import "ViewController.h"#import "lua.h"#include "lauxlib.h"#include "lualib.h"@interface ViewController()@end@implementation ViewController - (void) viewDidLoad { [super viewDidLoad];
- lua_State * L = luaL_newstate();
- luaL_openlibs(L);
- NSString * scriptPath = [[NSBundle mainBundle] pathForResource: @"test"ofType: @"lua"];
- int error;
- error = luaL_dofile(L, [scriptPath cStringUsingEncoding: NSUTF8StringEncoding]);
- /* runs Lua script */
- if (error) {
- printf("%s", lua_tostring(L, -1));
- }
- lua_close(L);
- } - (void) didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end
8. 执行 控制台 可以看到
- hello lua on ios
就爱阅读 www.92to.com 网友整理上传, 为您提供最全的知识大全, 期待您的分享,转载请注明出处。
来源: http://www.92to.com/bangong/2017/02-16/17229601.html