Crossplatform iPhone / Android code sharing

前端 未结 11 1524
北海茫月
北海茫月 2020-12-01 01:51

Simply put: What is the most effective way to share / reuse code between iPhone and Android builds?

The two most common scenarios I think would be:

  1. Bla
11条回答
  •  南方客
    南方客 (楼主)
    2020-12-01 02:33

    You can use Scapix Language Bridge to automatically bridge C++ to Java and ObjC/Swift (among other languages). Bridge code automatically generated on the fly directly from C++ header files. Here is an example:

    Define your class in C++:

    #include 
    
    class contact : public scapix::bridge::object
    {
    public:
        std::string name();
        void send_message(const std::string& msg, std::shared_ptr from);
        void add_tags(const std::vector& tags);
        void add_friends(std::vector> friends);
    };
    

    And call it from Swift:

    class ViewController: UIViewController {
        func send(friend: Contact) {
            let c = Contact()
    
            contact.sendMessage("Hello", friend)
            contact.addTags(["a","b","c"])
            contact.addFriends([friend])
        }
    }
    

    And from Java:

    class View {
        private contact = new Contact;
    
        public void send(Contact friend) {
            contact.sendMessage("Hello", friend);
            contact.addTags({"a","b","c"});
            contact.addFriends({friend});
        }
    }
    

提交回复
热议问题