skip to Main Content

Can an xcode Swift app call functions in a 2nd xcode Swift app, or must 2nd be static lib only?

Why:
I need to provide functions to be called by a 3rd Swift app.
I have an existing swift app.
It will save development time to drop new functions into existing app, which 3rd app calls.

Otherwise must create and maintain static lib.

2

Answers


  1. As you probably already know, all the apps on the iPhone run in a sandbox in order to provide a high level of security. As far as I know, there are multiple ways to achieve this, but lots of them are too complicated to use for this.

    Here are two ways to manage the communication between apps that may help you:

    1. Manage communication between apps through registered URL handlers. Click here for how to accomplish this.
    2. Peer-to-peer networking over Wi-Fi and Bluetooth. Sample

    Later edit:

    Using the first method is really simple and effective, as long as you do not need to transfer data between apps.

    Here is an old project that demonstrates how this is working. (check the gif at the end of the readme)

    Do not forget, if you are using SceneDegate to implement in that file

      
        func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
            if let url = URLContexts.first?.url {
                print(url)
                if url.pathComponents.contains("method1") {
                    print("Method1 called")
                }
                if url.pathComponents.contains("method2") {
                    print("Method2 called")
                }
            }
        }
    
    Login or Signup to reply.
  2. Being in the same project is not relevant here. If they are separate apps, they are separate apps, and cannot interact directly (see C-Viorel’s answer for some ways they can indirectly communicate, but this isn’t related to calling methods).

    But you don’t need to do much management. Pull the files into a local Swift Package, which is extremely lightweight. If even that is too complex, just add the relevant files to both apps.

    Target Membership Pane

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search