简体   繁体   English

NSWidgetExtensionContext openURL Swift

[英]NSWidgetExtensionContext openURL Swift

I have been attempting to implement a button to open my iOS app from its widget.我一直在尝试实现一个按钮来从它的小部件打开我的 iOS 应用程序。 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.也许你们中一些更有经验的 iOS 开发人员可以对此有所了解。

I am developing an update to one of my iOS apps for iOS 10 using XCode 8.1 and Swift 2.我正在使用 XCode 8.1 和 Swift 2 为我的 iOS 10 应用程序之一开发更新。

Code for my widget's button:我的小部件按钮的代码: 在此处输入图片说明

URL scheme added to the widget's info.plist:添加到小部件的 info.plist 的 URL 方案: 在此处输入图片说明

The runtime error I receive when pressing the OpenApp button:按下 OpenApp 按钮时收到的运行时错误:

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. // 注意:应用名称已替换为适当的泛型。

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:代码为-10814的操作系统错误是kLSApplicationNotFoundErr ,它描述了以下情况:

No application in the Launch Services database matches the input criteria. Launch Services 数据库中没有与输入条件匹配的应用程序。

It sounds like your application has not been properly registered with the system as a consumer of the URL scheme you are using.听起来您的应用程序尚未作为您所使用的 URL 方案的使用者在系统中正确注册。 Have you double-double (double!) checked that the bundle identifier and URL scheme match?您是否仔细检查了包标识符和 URL 方案是否匹配? Have you verified that your app can be launched with the URL from Safari?您是否确认可以使用 Safari 中的 URL 启动您的应用程序?

URL 方案应该添加到主应用程序的 info.plist,而不是小部件的。

To open the Containing App from Todays Extension:要从 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):您还需要将以下行添加到应用程序的 info.plist(作为源代码打开):

    <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.这是如何打开应用程序或使用扩展程序和包含应用程序共享用户默认值的完整指南

Also you should check if you are using any not allowed character for your url scheme.此外,您应该检查您的 url 方案是否使用了任何不允许的字符。 Maybe it is not your case but I was using this and it was wrong:也许这不是你的情况,但我正在使用它,这是错误的:

my_AppName

instead this finally worked :)相反,这终于奏效了:)

myAppName

as said here , the scheme must begin with alphanumeric character and it can contain only alphanumeric characters , + , - and .正如此处所说,方案必须以字母数字字符开头,并且只能包含alphanumeric characters +-.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM