Share number from contact/call logs

Hi,

I'm trying to implement a way to share a phone number from call logs with my app. I would select the call log entry, then "share contact" and share to my app. I implemented a Share Extension and my application appears in the list of application to share with.

How can I list my application in the list of actions below the app icons? When I share with my app, the Share Extension view shows up, but no action is possible: I implemented a button to be able to send the number to my application but the action never fires, is it the correct implementation? How would I implement the sharing of the number with my main app? I tried with a custom url binding but I'm not sure if it is the correct way. My ideal implementation would be that no view show up and my application is immediately launched and the number is pasted.

struct ShareView: View {
    var extensionContext: NSExtensionContext?
    
    @State private var contact: CNContact? = nil
    @State private var error: Error? = nil
    @State private var multiSelection = Set<String>()
    @State private var selectedPhoneNumber: String? = nil
    
    var body: some View {
        NavigationStack {
            VStack() {
                if let contact = contact {
                    Button(action: submitPhoneNumber) {
                        Text("Submit")
                            .padding()
                            .background(Color.green)
                            .cornerRadius(8)
                            .foregroundColor(.white)
                    }
                    List(contact.phoneNumbers, id: \.self) { phoneNumber in
                        Button(action: {
                            print("Phone number selected: \(phoneNumber.value.stringValue)")
                            self.selectedPhoneNumber = phoneNumber.value.stringValue
                        }) {
                            Text(phoneNumber.value.stringValue)
                            //.padding()
                                .background(self.selectedPhoneNumber == phoneNumber.value.stringValue ? Color.blue : Color.clear)
                            //.cornerRadius(8)
                                .foregroundColor(.black)
                        }
                    }
                    
                    
                } else if let error = error {
                    Text("Error: \(error.localizedDescription)")
                } else {
                    Text("Loading...")
                        .onAppear {
                            loadContact()
                        }
                }
            }
        }
        .padding()
        .navigationTitle("Number")
        .toolbar() {
            Button("Close") {
                close()
            }
        }
    }
    
    func close() {
            NotificationCenter.default.post(name: NSNotification.Name("close"), object: nil)
    }
    
    private func isButtonActive() -> Bool {
        return selectedPhoneNumber != nil
    }
    
    private func submitPhoneNumber() {
        print("Selected phone number: \(self.selectedPhoneNumber ?? "Nothing")")
        
        guard let phoneNumber = self.selectedPhoneNumber else { return }
        guard let extensionContext = self.extensionContext else { return }
        
        print("PhoneNumber: \(phoneNumber)")
        
        let urlScheme = "callclean://"
        if let url = URL(string: "\(urlScheme)?phone=\(phoneNumber)") {
            print(url.absoluteString)
            extensionContext.open(url, completionHandler: nil)
        }
        
        close()
    }
    
    private func loadContact() {
        guard let extensionContext = extensionContext else { return }
        
        for item in extensionContext.inputItems {
            if let inputItem = item as? NSExtensionItem {
                if let attachments = inputItem.attachments {
                    for provider in attachments {
                        if provider.hasItemConformingToTypeIdentifier("public.vcard") {
                            provider.loadItem(forTypeIdentifier: "public.vcard", options: nil) { (data, error) in
                                if let error = error {
                                    DispatchQueue.main.async {
                                        self.error = error
                                    }
                                    return
                                }
                                
                                if let data = data as? Data {
                                    do {
                                        //let contactStore = CNContactStore()
                                        let contacts = try CNContactVCardSerialization.contacts(with: data)
                                        DispatchQueue.main.async {
                                            self.contact = contacts.first
                                        }
                                    } catch {
                                        DispatchQueue.main.async {
                                            self.error = error
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

Thanks for your guidance.

Share number from contact/call logs
 
 
Q