Kumar Ravi
Kumar Ravi

Reputation: 429

In iOS, how can I get the contact shared from "Recent" calls list to my app in Flutter?

Please excuse me if I sound stupid, I'm new to flutter.

I have started learning flutter recently and wanted to create an app where anyone can share a contact from the "Recent" calls list to my app. I'm following this blog post which allows text share from any other app to my app.

What I have done so far:

  1. This is my plist file, added the public.vcard to allow my app to appear on the tap of "Share Contact". enter image description here enter image description here
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>NSExtension</key>
    <dict>
        <key>NSExtensionAttributes</key>
        <dict>
        <key>NSExtensionActivationRule</key>
        <string>
            SUBQUERY (
            extensionItems, $extensionItem,
            SUBQUERY (
            $extensionItem.attachments, $attachment,
            ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.vcard"
            ).@count >= 1
            ).@count > 0
        </string>
        </dict>
        <key>NSExtensionMainStoryboard</key>
        <string>MainInterface</string>
        <key>NSExtensionPointIdentifier</key>
        <string>com.apple.share-services</string>
    </dict>
</dict>
</plist>
  1. Here's my ShareViewController.swift
import Social
import MobileCoreServices

class ShareViewController: SLComposeServiceViewController {

    override func isContentValid() -> Bool {
        // Do validation of contentText and/or NSExtensionContext attachments here
        print("Something is not right")
        return true
    }

    override func didSelectPost() {
        // This is called after the user selects Post. Do the upload of contentText and/or NSExtensionContext attachments.
    
        // Inform the host that we're done, so it un-blocks its UI. Note: Alternatively you could call super's -didSelectPost, which will similarly complete the extension context.
        let sharedSuiteName: String = "group.com.thelogicalbeing.whatsappshare"
            let sharedDataKey: String = "SharedData"
            let extensionItem = extensionContext?.inputItems[0] as! NSExtensionItem
            let contentTypeText = kUTTypeText as String // Note, you need to import 'MobileCoreServices' for this
            for attachment in extensionItem.attachments! {
                print(attachment)
                if attachment.hasItemConformingToTypeIdentifier(contentTypeText) {
                    attachment.loadItem(forTypeIdentifier: contentTypeText, options: nil, completionHandler: {(results, error) in
                        if let sharedText = results as! String? {
                            if let userDefaults = UserDefaults(suiteName: sharedSuiteName) {
                                userDefaults.set(sharedText, forKey: sharedDataKey)
                            }
                        }
                    })
                }
            }

            self.extensionContext!.completeRequest(returningItems: [], completionHandler: nil)

    }

    override func configurationItems() -> [Any]! {
        // To add configuration options via table cells at the bottom of the sheet, return an array of SLComposeSheetConfigurationItem here.
        return []
    }

}
  1. Here's my AppDelegate.swift
import UIKit
import Flutter

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
    GeneratedPluginRegistrant.register(with: self)
    let sharedSuiteName: String = "group.com.thelogicalbeing.whatsappshare"
    let sharedDataKey: String = "SharedData"

    let controller: FlutterViewController = window?.rootViewController as! FlutterViewController
    let methodChannel = FlutterMethodChannel(name: "com.thelogicalbeing.whatsappshare", binaryMessenger: controller.binaryMessenger)

    methodChannel.setMethodCallHandler({
        (call: FlutterMethodCall, result: @escaping FlutterResult) -> Void in
        if call.method == "getSharedData" {
            if let prefs = UserDefaults(suiteName: sharedSuiteName) {
                if let sharedText = prefs.string(forKey: sharedDataKey) {
                    result(sharedText);
                }
                // clear out the cached data
                prefs.set("", forKey: sharedDataKey);
            }
        }
    })

    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }
}

What I'm trying to achieve is that I need to receive the phone number and display it in my app.

Don't know how to proceed. Any help will be appreciated.

Upvotes: 2

Views: 2014

Answers (2)

Moaz El-sawaf
Moaz El-sawaf

Reputation: 3039

Have a look at this package called receive_sharing_intent, it allows you to to receive sharing photos, videos, text, urls or any other file types from another app. And it also supports iOS Share extension and launching the host app automatically.

Upvotes: 0

Pierre Janineh
Pierre Janineh

Reputation: 702

1- Apple does not allow fetching call logs on iOS!

You can fetch all contacts with all their information. But not the calls log.

2- On Android you can use the pub.dev dependency plugin call_log to do that.

Upvotes: 1

Related Questions