2

如果我听起来很愚蠢,请原谅我,我是新手。

我最近开始学习 Flutter,并想创建一个应用程序,任何人都可以将“最近”通话列表中的联系人共享到我的应用程序。我正在关注这篇博客文章,它允许从任何其他应用程序向我的应用程序共享文本。

到目前为止我做了什么:

  1. 这是我的 plist 文件,添加了 public.vcard 以允许我的应用程序出现在“共享联系人”的点击中。 在此处输入图像描述 在此处输入图像描述
<!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. 这是我的 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. 这是我的 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)
  }
}

我想要实现的是我需要接收电话号码并将其显示在我的应用程序中。

不知道如何进行。任何帮助将不胜感激。

4

2 回答 2

1

1- Apple 不允许在iOS上获取通话记录!

您可以获取所有联系人及其所有信息。但不是通话记录。

2- 在Android上,您可以使用pub.dev依赖插件call_log来做到这一点。

于 2022-01-10T08:56:12.633 回答
0

看看这个名为receive_sharing_intent的包,它允许您从另一个应用程序接收共享照片、视频、文本、url 或任何其他文件类型。它还支持 iOS Share 扩展和自动启动主机应用程序。

于 2022-01-14T18:47:16.470 回答