69 lines
2.4 KiB
Swift
69 lines
2.4 KiB
Swift
import Flutter
|
||
import UIKit
|
||
import FBSDKCoreKit
|
||
import os.log
|
||
|
||
public class ClientProxyFrameworkPlugin: NSObject, FlutterPlugin {
|
||
private var channel: FlutterMethodChannel?
|
||
|
||
public static func register(with registrar: FlutterPluginRegistrar) {
|
||
let ch = FlutterMethodChannel(
|
||
name: "client_proxy_framework/facebook_sdk",
|
||
binaryMessenger: registrar.messenger())
|
||
let instance = ClientProxyFrameworkPlugin()
|
||
instance.channel = ch
|
||
registrar.addMethodCallDelegate(instance, channel: ch)
|
||
}
|
||
|
||
public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
|
||
switch call.method {
|
||
case "setFacebookSdkDebugLogging":
|
||
let args = call.arguments as? [String: Any]
|
||
let enabled = Self.coerceFacebookSdkDebugLogsArg(args?["enabled"])
|
||
Self.applyFacebookSdkDebugLogging(enabled: enabled)
|
||
result(nil)
|
||
case "waitForFacebookSdkInit":
|
||
let args = call.arguments as? [String: Any]
|
||
let debugLogs = Self.coerceFacebookSdkDebugLogsArg(args?["facebookSdkDebugLogs"])
|
||
Self.applyFacebookSdkDebugLogging(enabled: debugLogs)
|
||
result(true)
|
||
channel?.invokeMethod("onFacebookSdkInitialized", arguments: nil)
|
||
default:
|
||
result(FlutterMethodNotImplemented)
|
||
}
|
||
}
|
||
|
||
/// 与 Android 一致:由 `facebook.debugLogs` 或 Dart `setFacebookSdkDebugLogging` 控制。
|
||
private static func applyFacebookSdkDebugLogging(enabled: Bool) {
|
||
if enabled {
|
||
Settings.shared.loggingBehaviors = Set([
|
||
.appEvents,
|
||
.networkRequests,
|
||
.informational,
|
||
.developerErrors,
|
||
.graphAPIDebugInfo,
|
||
])
|
||
os_log("Facebook SDK debug logging ON (filter console for FBSDK / Facebook)", log: .default, type: .info)
|
||
print("ClientProxyFB: Facebook SDK debug logging ON — also search Xcode console for FBSDK / FacebookSDK")
|
||
} else {
|
||
Settings.shared.loggingBehaviors = []
|
||
os_log("Facebook SDK debug logging OFF", log: .default, type: .info)
|
||
print("ClientProxyFB: Facebook SDK debug logging OFF")
|
||
}
|
||
}
|
||
|
||
private static func coerceFacebookSdkDebugLogsArg(_ raw: Any?) -> Bool {
|
||
switch raw {
|
||
case let b as Bool:
|
||
return b
|
||
case let n as NSNumber:
|
||
return n.intValue != 0
|
||
case let s as String:
|
||
let v = s.trimmingCharacters(in: .whitespacesAndNewlines).lowercased()
|
||
return v == "true" || v == "1" || v == "yes"
|
||
default:
|
||
return false
|
||
}
|
||
}
|
||
}
|