最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

swift ExtensionFoundationExtensionKit in iOS - Stack Overflow

programmeradmin0浏览0评论

Is it possible to use ExtensionFoundation in iOS ?

Officially there is support but only a server side.

For example:

public protocol TextTransformExtension: AppExtension {
    
    /// Transform the input string according to your extension's behavior
    /// - Parameter input: The text entered by the user in TextTransformer.
    /// - Returns: The output that should be shown to the user, or `nil` if the transformation failed.
    func transform(_ input: String) async -> String?
}

/// Configuration for extensions that conform to the ``TextTransformExtension`` protocol.
public struct TextTransformExtensionConfiguration<E: TextTransformExtension>: AppExtensionConfiguration {
    let appExtension: E
    let server: TextTransformerExtensionXPCServer
    
    /// Creates a default configuration for the given extension.
    /// - Parameter appExtension: An instance of your custom extension that conforms to the ``TextTransformExtension`` protocol.
    public init(_ appExtension: E) {
        self.appExtension = appExtension
        self.server = TextTransformerExtensionXPCServer(with: appExtension)
    }
}

@_spi(TextTransformerSPI)
@objc public protocol TextTransformerXPCProtocol: NSObjectProtocol {
    func transform(input: String, reply: @escaping (String?) -> Void)
}

@objc final class TextTransformerExtensionXPCServer: NSObject, TextTransformerXPCProtocol {
    
    let implementation: any TextTransformExtension
    
    init(with implementation: some TextTransformExtension) {
        self.implementation = implementation
    }
    
    func transform(input: String, reply: @escaping (String?) -> Void) {
        Task {
            let result = await implementation.transform(input)
            await MainActor.run { reply(result) }
        }
    }
}

extension TextTransformExtensionConfiguration {
    public func accept(connection: NSXPCConnection) -> Bool {
        connection.exportedInterface = NSXPCInterface(with: TextTransformerXPCProtocol.self)
        connection.exportedObject = server
        
        connection.resume()
        
        return true
    }
}

but for connection we need to use something like let process: AppExtensionProcess // only available on macOS let connection = try process.makeXPCConnection()

or viewController.makeXPCConnection()

where viewController is EXHostViewController // only available on macOS

Is there any way to do it for iOS ? How to make a connection between iOS app and some app extension ?

发布评论

评论列表(0)

  1. 暂无评论