Compilation of lua extension library under xcode

Recently, I am researching the development of touchsprite scripts under ios jailbreak. The scripting language of touchsprite is lua, and there is a need to import contacts into ios in batches through touchsprite. I looked at the official documentation of touchsprite, but I did not find the contacts for the operation address book The method.
But seeing that the Lua code base can be extended, it is natural to want to write a Lua extension library by myself.

First of all, I learned how to operate the ios address book through Google, and I simply wrote an ios app called Import Master. The basic function is to write all the phone numbers that need to be imported into a file, with each phone number in one line, and the The file is imported into the mobile phone under the path /var/mobile/Media/touchsprite/lua/telphone.txt. In the app, you can set the number of imported numbers each time. You can also clear the address book.
Since you need to access the file system, you cannot simply use The ipa installation needs to be installed in the /Applications directory through cydia. I will publish it under my Weifeng source. If you are interested, you can add my Weifeng source to download in Cydia. The source code can be viewed on my github .

It can be seen that the address book can be imported and deleted normally on the app. Now it is necessary to expose the interface of importing and deleting the address book to lua.
Regarding writing lua extensions, I have never done it before, so I searched again on the Internet. Finally found out, the basic framework for writing a lua extension library.
Let me briefly explain through the code, the code is picked up from the development manual in the touch wizard.

#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;
}

The calling method in the lua layer is:

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

The official documentation only introduces the compilation of a simple single file.
Now I want to introduce how to compile through xcode.

By default, xcode cannot create a dynamic library project for ios,
please refer to http://blog.csdn.net/hursing/article/details/8951958 . Modify the xcode template.
After configuration, you can start working.

First, create a new xcode dynamic library project. Import the lua.h file. Implement the interface for the dynamic library.
The code is as follows:

//注册函数库
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;
}

After implementing each interface, the compilation will report a bunch of such errors.

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)

This is because he is looking for the corresponding implementation of the function, but we cannot link lua.dylib into it. What we do is to expand the library. There is no need to link these. The
solution is very simple, just build Settings – Linking – other in xcode Add -undefined dynamic_lookup to linker Falgs. Just recompile.
As shown in the figure:
Write picture description here

Copy luaPlugin.dylib to the plugin directory of touchsrpite.
Add the plugin directory to package.cpath in the script and call it directly

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

Successfully output hello world.

But there is a strange problem. The data in the address book cannot be obtained in the script, nor can it be added.
I think it may be limited by the touchsprite process. If anyone knows how to solve it, please let me know.

Guess you like

Origin blog.csdn.net/suzhijie325/article/details/50729485