We’re building a PDF share extension. The app is launched, but the SceneDelegate isn’t, so the scheme can’t be used further. I assume the controller works fine, but the SceneDelegate can’t print anything.
We’ve tried many approaches, such as uninstalling & reinstalling the app, restarting Xcode, and appDelegate, but none of them have worked. I think this version is the closest one.
Question: How can we get the SceneDelegate’s methods working?
It’s taking us so long. If more information is needed, I’ll provide it immediately. Thank you!
SceneDelegate.swift
import SwiftUI
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
print("hahahah")
guard let windowScene = scene as? UIWindowScene else { return }
let contentView = ContentView()
if let urlContext = connectionOptions.urlContexts.first {
handleIncomingURL(urlContext.url)
}
let window = UIWindow(windowScene: windowScene)
window.rootViewController = UIHostingController(rootView: contentView)
self.window = window
window.makeKeyAndVisible()
}
func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
print("asdfasdf")
if let urlContext = URLContexts.first {
handleIncomingURL(urlContext.url)
}
}
private func handleIncomingURL(_ url: URL) {
print(url)
guard url.scheme == "yanfudaoapp" else {
return
}
if url.host == "openUrl" || url.host == "openPDF" {
if let pdfURLString = url.queryParameters?["url"],
let pdfURL = URL(string: pdfURLString) {
if let hostingController = window?.rootViewController as? UIHostingController<ContentView> {
DispatchQueue.main.async {
print(pdfURL)
hostingController.rootView.handleIncomingURL(url: pdfURL)
}
}
}
}
}
}
info.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDisplayName</key>
<string>yanfd</string>
<key>CFBundleIdentifier</key>
<string>-1111.text</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>yanfd</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLName</key>
<string>yanfudao</string>
<key>CFBundleURLSchemes</key>
<array>
<string>yanfudaoapp</string>
</array>
</dict>
</array>
<key>LSApplicationQueriesSchemes</key>
<array>
<string>weixin</string>
<string>netdisk</string>
</array>
<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>CFBundleTypeName</key>
<string>PDF Document</string>
<key>LSHandlerRank</key>
<string>Owner</string>
<key>CFBundleTypeRole</key>
<string>Viewer</string>
<key>LSItemContentTypes</key>
<array>
<string>com.adobe.pdf</string>
</array>
</dict>
</array>
<key>LSSupportsOpeningDocumentsInPlace</key>
<false/>
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>
<key>UIApplicationSceneManifest</key>
<dict>
<key>UISceneConfigurations</key>
<dict>
<key>Default Configuration</key>
<dict>
<key>UISceneDelegateClassName</key>
<string>sceneDelegate.SceneDelegate</string>
</dict>
</dict>
</dict>
</dict>
</plist>
We expect the SceneDelegate’s method to be called for processing scheme commands.