xcode下lua扩展库的编译

最近在研究ios越狱下的touchsprite脚本的开发. touchsprite的脚本语言是lua, 有个需求是需要通过touchsprite批量导入联系人到ios里. 看了一下touchsprite的官方文档, 并没有找到操作通信录联系人的方法.
但是看到了可以扩展lua代码库.于是很自然的就想自己写一个lua扩展库.

首先,通过google了解了如何操作ios的通信录, 自己简单写了个ios app取名叫导入大师, 基本功能是把需要导入的电话号码都写到一个文件里,每个电话号码一行,并将文件导入手机路径/var/mobile/Media/touchsprite/lua/telphone.txt下,在app里可以设置每次,导入的号码个数.也可以清空通信录.
由于需要访问文件系统,不能简单的以ipa方式安装,需要通过cydia安装到/Applications目录下.我把他发布到我的威锋源下,有兴趣的可以在cydia里添加我的威锋源下载. 源码可以在我github上查看.

可以看到在app上是可以正常导入删除通信录的.现在就需要把导入和删除通信录的接口暴露给lua.
关于写lua扩展,之前也没有弄过, 于是又在网上一顿海搜, 终于找出了,写一个lua扩展库的基本框架.
我通过代码简要说明一下,代码是从触动精灵里开发手册扒过来的.

#include <stdio.h>
#include <stdlib.h>
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
#import <objc/runtime.h>
#import <Foundation/Foundation.h>
#import <UIKit/UIDevice.h>
/*  库 open 函数的前置声明   */
int luaopen_mt(lua_State *L);
/* Function mt_get_device_name
 * @return string device name
 */
static int mt_get_device_name(lua_State *L)
{
    NSString *name = [[UIDevice currentDevice] name];
    const char * name_str = [name UTF8String];
    lua_pushstring(L, name_str);
    return 1;
}
//注册函数库
static const luaL_Reg mt_lib[] = {
    {"device_name", mt_get_device_name},    //获取设备名称
    {NULL, NULL}
};
int luaopen_mt(lua_State *L)//注意, mt为扩展库的文件名
{
    luaL_newlib(L, mt_lib);//暴露给lua脚本的接口
    return 1;
}

lua层里的调用方法为:

local mt = require "mt"
print(mt.device_name())

官方文档只介绍了简单的单个文件的编译.
现在我要介绍的是如果通过xcode来编译.

默认xcode是无法为ios创建动态库工程的,
请参考http://blog.csdn.net/hursing/article/details/8951958.修改xcode模版.
配置好了之后就可以开工了,

首先,新建一个xcode动态库工程. 导入lua.h文件. 为动态库实现接口.
代码如下:

//注册函数库
static const luaL_Reg mt_lib[] = {
    {"device_name", mt_get_device_name},    //获取设备名称
    {"test", luaPlugin_test},
    {"requestAddressBook", luaPlugin_requestAddressBook},
    {"importConactFromFiles", luaPlugin_importConactFromFiles},
    {"removeAllConacts", luaPlugin_removeAllConacts},
    {"saveHistory", luaPlugin_saveHistory},
    {"readHistory", luaPlugin_readHistory},
    {NULL, NULL}
};

int luaopen_luaPlugin(lua_State *L)
{
    luaL_newlib(L, mt_lib);
    return 1;
}

实现各个接口之后, 编译会报错一堆这样的错误.

Undefined symbols for architecture armv7:
  "_lua_isnumber", referenced from:
      _luaPlugin_importConactFromFiles in luaPlugin.o
      _luaPlugin_saveHistory in luaPlugin.o
  "_lua_tonumberx", referenced from:
      _luaPlugin_importConactFromFiles in luaPlugin.o
      _luaPlugin_saveHistory in luaPlugin.o
  "_lua_pushboolean", referenced from:
      _luaPlugin_importConactFromFiles in luaPlugin.o
  "_lua_gettop", referenced from:
      _luaPlugin_test in luaPlugin.o
      _luaPlugin_importConactFromFile
      s in luaPlugin.o
      _luaPlugin_saveHistory in luaPlugin.o
  "_lua_error", referenced from:
      _luaPlugin_test in luaPlugin.o
      _luaPlugin_importConactFromFiles in luaPlugin.o
      _luaPlugin_saveHistory in luaPlugin.o
  "_lua_tolstring", referenced from:
      _luaPlugin_test in luaPlugin.o
  "_lua_isstring", referenced from:
      _luaPlugin_test in luaPlugin.o
  "_lua_pushstring", referenced from:
      _mt_get_device_name in luaPlugin.o
      _luaPlugin_test in luaPlugin.o
      _luaPlugin_imp
ortConactFromFiles in luaPlugin.o
      _luaPlugin_saveHistory in luaPlugin.o
  "_lua_createtable", referenced from:
      _luaopen_luaPlugin in luaPlugin.o
  "_lua_pushinteger", referenced from:
      _luaPlugin_readHistory in luaPlugin.o
  "_luaL_setfuncs", referenced from:
      _luaopen_luaPlugin in luaPlugin.o
ld: symbol(s) not found for architecture armv7
clang: error: linker command failed with exit code 1 (use -v to see invocation)

这是因为他要找函数对应的实现, 但是我们不能把lua.dylib给链接进去.我们做的是扩展库.不需要链接这些.
解决方式很简单, 只需要在xcode中 Build Setttings – Linking – other linker Falgs 中添加-undefined dynamic_lookup.重新编译即可.
如图所示:
这里写图片描述

在把luaPlugin.dylib 复制到touchsrpite的plugin目录下.
脚本里把plugin目录添加到 package.cpath里就可以直接调用

local lp = require('luaPlugin')
local msg = lp.test("hello wrold");
print(msg)

成功输出hello wrold.

但是奇葩的问题出现. 脚本里始终无法获得通信录里的数据,也无法添加.
我想可能是touchsprite进程所限制了.如果哪位知道怎么解决的.请告知一下.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值