我正在尝试使用flutter在本地平台上复制文本。但是,每当我调用复制文本的方法时,我总是在Channel iOS上找不到方法copyText,但在android上运行良好。任何帮助,将不胜感激。下面是我的代码
我的Flutter代码:
const channel_iOS = MethodChannel('iOS');
const channel_android = MethodChannel('android');
const String method_copy_text = 'copyText';
Future copyToClipBoard(String text)
async {
if(Platform.isIOS)
return await channel_iOS.invokeMethod(method_copy_text,{'text': text});
else if(Platform.isAndroid)
return await channel_android.invokeMethod(method_copy_text,{'text': text});
}
用于处理方法调用的Xcode:
import UIKit
import Flutter
import Firebase
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
var flutterViewController: FlutterViewController?
var flutterResult: FlutterResult?
var methodCallArgs: Any?
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
self.flutterViewController = FlutterViewController()
let flutterChannel = FlutterMethodChannel.init(name: "iOS", binaryMessenger: flutterViewController as! FlutterBinaryMessenger);
flutterChannel.setMethodCallHandler{(flutterMethodCall, result) in
self.flutterResult = result
self.methodCallArgs = flutterMethodCall.arguments
switch flutterMethodCall.method {
case "openPlayer":
break;
case "copyText":
print("COPIED>>>>>")
self.copyText()
break
default:
break;
}
}
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
func copyText() {
if(self.methodCallArgs != nil)
{
if let copyData = self.methodCallArgs as? [String: String] {
let text = copyData["text"]
let pasteBoard = UIPasteboard.general
pasteBoard.string = text
self.flutterResult!(["isSuccess": true])
}
else{
self.flutterResult!(["isSuccess": false,"message":"text is empty"])
}
}
}
}
答案 0 :(得分:0)
import UIKit
import Flutter
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
var flutterResult: FlutterResult?
var methodCallArgs: Any?
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
GeneratedPluginRegistrant.register(with: self)
let flutterController : FlutterViewController = window?.rootViewController as! FlutterViewController
let flutterChannel = FlutterMethodChannel(name: "iOS",
binaryMessenger: flutterController.binaryMessenger)
flutterChannel.setMethodCallHandler{(flutterMethodCall, result) in
self.flutterResult = result
self.methodCallArgs = flutterMethodCall.arguments
switch flutterMethodCall.method {
case "openPlayer":
break;
case "copyText":
print("COPIED>>>>>")
self.copyText()
break
default:
break;
}
}
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
func copyText() {
if(self.methodCallArgs != nil)
{
if let copyData = self.methodCallArgs as? [String: String] {
let text = copyData["text"]
let pasteBoard = UIPasteboard.general
pasteBoard.string = text
self.flutterResult!(["isSuccess": true])
}
else{
self.flutterResult!(["isSuccess": false,"message":"text is empty"])
}
}
}
}