NSWidgetExtensionContext openURL Swift

亡梦爱人 提交于 2019-12-07 02:47:10

问题


I have been attempting to implement a button to open my iOS app from its widget. I realize this issue has been beaten to death on the forums but I cannot find explanation with the specific error I am receiving. Perhaps some of you more experienced iOS developers can shed some light on this.

I am developing an update to one of my iOS apps for iOS 10 using XCode 8.1 and Swift 2.

Code for my widget's button:

URL scheme added to the widget's info.plist:

The runtime error I receive when pressing the OpenApp button:

AppWidget[11872:3577323] __55-[_NCWidgetExtensionContext openURL:completionHandler:]_block_invoke failed: Error Domain=NSOSStatusErrorDomain Code=-10814 "(null)"

// Note: app name has been substituted with appropriate generics.


回答1:


I often find the OS Status lookup site pretty useful to infer details from errors. An OS error with code -10814 is a kLSApplicationNotFoundErr, which describes the scenario when:

No application in the Launch Services database matches the input criteria.

It sounds like your application has not been properly registered with the system as a consumer of the URL scheme you are using. Have you double-double (double!) checked that the bundle identifier and URL scheme match? Have you verified that your app can be launched with the URL from Safari?




回答2:


URL scheme should added to the main app's info.plist, not the widget's.




回答3:


To open the Containing App from Todays Extension:

let myAppUrl = URL(string: "main-screen:")!
extensionContext?.open(myAppUrl, completionHandler: { (success) in
    if (!success) {
        print("error: failed to open app from Today Extension")
    }
})

You also need to add the following lines to the application's info.plist (open as a source code):

    <key>CFBundleURLTypes</key>
    <array>
        <dict>
            <key>CFBundleURLName</key>
            <string>com.mikitamanko.bubblewrap</string>
            <key>CFBundleURLSchemes</key>
            <array>
                <string>main-screen</string>
            </array>
        </dict>
    </array>

right after the

<?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>

Here's the complete guide how to open the app or share Users Defaults with Extension and the containing app.



来源:https://stackoverflow.com/questions/40440169/nswidgetextensioncontext-openurl-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!