aslan-flutter/ios/Runner/AppDelegate.swift
2026-07-01 18:25:58 +08:00

80 lines
3.1 KiB
Swift
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import Flutter
import UIKit
@main
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
// Flutter
let controller : FlutterViewController = window?.rootViewController as! FlutterViewController
// MethodChannel
let channel = FlutterMethodChannel(name: "com.chat.auu.snapchat_share/intent", binaryMessenger: controller.binaryMessenger)
//
channel.setMethodCallHandler { [weak self] (call: FlutterMethodCall, result: @escaping FlutterResult) -> Void in
// shareToSnapchat
if call.method == "shareToSnapchat" {
// Fluttertext
let text = call.arguments as? [String: Any]? ?? [:]
let shareText = text?["text"] as? String ?? ""
//
self?.shareToSnapchat(text: shareText, result: result)
} else {
//
result(FlutterMethodNotImplemented)
}
}
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
private func shareToSnapchat(text: String, result: @escaping FlutterResult) {
// 1. SnapchatURL
guard let snapchatUrl = URL(string: "snapchat://") else {
result(FlutterError(code: "INVALID_URL", message: "Snapchat URL无效", details: nil))
return
}
// 2. Snapchat
if UIApplication.shared.canOpenURL(snapchatUrl) {
// SnapchatURL Scheme
let shareParams = [
"text": text
]
// 3. URLSnapchat
var components = URLComponents(url: snapchatUrl, resolvingAgainstBaseURL: true)
components?.queryItems = shareParams.map { URLQueryItem(name: $0.key, value: $0.value) }
guard let finalUrl = components?.url else {
result(FlutterError(code: "URL_BUILD_FAILED", message: "Open Snapchat Fail", details: nil))
return
}
// 4. Snapchat
if #available(iOS 10.0, *) {
UIApplication.shared.open(finalUrl, options: [:]) { success in
if success {
result(nil) //
} else {
result(FlutterError(code: "SHARE_FAILED", message: "Open Snapchat Fail", details: nil))
}
}
} else {
// iOS 9
let success = UIApplication.shared.openURL(finalUrl)
if success {
result(nil)
} else {
result(FlutterError(code: "SHARE_FAILED", message: "Open Snapchat Fail", details: nil))
}
}
} else {
// Snapchat
result(FlutterError(code: "SNAPCHAT_NOT_INSTALLED", message: "Snapchat is not installed.", details: nil))
}
}
}