ios Today App Extension Widget 点击打开包含的应用程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24019820/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 00:19:12  来源:igfitidea点击:

Today App Extension Widget Tap To Open Containing App

iosios8ios-app-extensiontoday-extension

提问by Daniel Storm

I've implemented a Today widget for my application +Quotes which displays the day's quote within the notification center with the help of these Apple Docs. What I'd like to accomplish is opening the Containing App, in this case +Quotes, when the user taps the +Quotes widget within their Today notification view, not entirely sure what to call this, as Calendar would if you tapped it in the Today view. I've tried overlaying a button over the label which would call -(void)openURL:(NSURL *)URL completionHandler:(void (^)(BOOL success))completionHandlerupon it being tapped, then open the Custom URL Scheme I have declared to open the Containing App. The issue is it doesn't open the Containing App.

我已经为我的应用程序 +Quotes 实现了一个 Today 小部件,它在这些Apple Docs的帮助下在通知中心内显示当天的报价。我想要完成的是打开包含应用程序,在这种情况下是 +Quotes,当用户在他们的今天通知视图中点击 +Quotes 小部件时,不完全确定如何调用它,如果你在日历中点击它今天的观点。我尝试在标签上覆盖一个按钮,该按钮会调用 -(void)openURL:(NSURL *)URL completionHandler:(void (^)(BOOL success))completionHandler它被点击,然后打开我声明的自定义 URL 方案以打开包含应用程序。问题是它没有打开包含的应用程序。

+Quotes Today App Extension Widget

+Quotes Today App Extension Widget

-(IBAction)myButton:(id)sender {
    NSURL *customURL = [NSURL URLWithString:@"PositiveQuotes://"];
    [self openURL:customURL completionHandler:nil];
}

回答by sunseeker

EDIT: Ok, just a little correction here. I got it working with placing a button over the label just like suggested above and the following code:

编辑:好的,这里有一点更正。我按照上面的建议和以下代码在标签上放置了一个按钮:

- (IBAction) goToApp: (id)sender { 
    NSURL *url = [NSURL URLWithString:@"floblog://"];
    [self.extensionContext openURL:url completionHandler:nil]; 
  }

I linked it to a "Touch Up Inside" event. However, this also causes the app to launch when the user scrolls the Today view.

我将它与“Touch Up Inside”活动联系起来。但是,这也会导致应用在用户滚动“今天”视图时启动。

=======================================

========================================

I ran into the same issue. However, it seems that there is no solution for now since the release notesfor the first beta of iOS 8 mention:

我遇到了同样的问题。但是,由于iOS 8 第一个测试版的发行说明中提到了,目前似乎没有解决方案:

Known Issues: openURL does not work from an extension.

已知问题:openURL 不适用于扩展程序。

So I guess we will at least have to wait until beta 2.

所以我想我们至少要等到 beta 2。

回答by Quanlong

Swift 2 version, according to Apple Doc

Swift 2 版本,根据Apple Doc

extensionContext?.openURL(NSURL(string: "foo://")!, completionHandler: nil)

Swift 3 version

斯威夫特 3 版本

extensionContext?.open(URL(string: "foo://")! , completionHandler: nil)

And don't forget to add Custom URL Schemesinto Info.plist

并且不要忘记将自定义 URL Schemes添加到Info.plist

enter image description here

在此处输入图片说明

回答by teriiehina

The answer by @sunseeker is the good one but it is "hidden" in the comments. And as the accepted answer says that this isn't possible, it may mislead visitors.

@sunseeker 的答案很好,但它“隐藏”在评论中。正如公认的答案所说,这是不可能的,它可能会误导访问者。

this code works:

此代码有效:

- (IBAction)launchHostingApp:(id)sender
{
  NSURL *pjURL = [NSURL URLWithString:@"hostingapp://home"];
  [self.extensionContext openURL:pjURL completionHandler:nil];
}

I'm using Xcode 6.0 (6A215l) with Yosemite Beta 1.

我将 Xcode 6.0 (6A215l) 与 Yosemite Beta 1 一起使用。

And like Apple says in Handling Commons Scenarios:

就像苹果在处理公共场景中所说的那样:

An extension doesn't directly tell its containing app to open; instead, it uses the openURL:completionHandler: method of NSExtensionContext to tell the system to open its containing app. When an extension uses this method to open a URL, the system validates the request before fulfilling it.

扩展程序不会直接告诉其包含的应用程序打开;相反,它使用 NSExtensionContext 的 openURL:completionHandler: 方法告诉系统打开其包含的应用程序。当扩展程序使用此方法打开 URL 时,系统会在完成请求之前验证请求。

回答by EPage_Ed

Another way to do this without adding a hidden button is to add a UITapGestureRecognizer on the UILabel (make sure to set userInteractionEnabled to true on the label). Check the recognizer state in the handler to make sure you reached UIGestureReconizerStateEnded (and not Cancelled or Failed) and then run your openUrl code.

另一种不添加隐藏按钮的方法是在 UILabel 上添加一个 UITapGestureRecognizer(确保在标签上将 userInteractionEnabled 设置为 true)。检查处理程序中的识别器状态以确保您达到 UIGestureReconizerStateEnded(而不是 Canceled 或 Failed),然后运行您的 openUrl 代码。

回答by Mikita Manko

Just in case, here's the version for Swift 3 with the error handling version:

以防万一,这里是带有错误处理版本的 Swift 3 版本:

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

To make it work you need to open Application's info.plist (open as source code) and in the very top, after this

要使其工作,您需要打开应用程序的 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>

add the following, so the App will know which URLs it should handle Here's the complete exampleof how to open the containing app and share User Defaults between app and Extension.

添加以下内容,以便应用程序知道它应该处理哪些 URL 以下是如何打开包含应用程序并在应用程序和扩展程序之间共享用户默认值的完整示例