简体   繁体   English

今日应用扩展小部件点击以打开包含应用

[英]Today App Extension Widget Tap To Open Containing App

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 . 我为我的应用程序+ Quotes实现了Today小部件,借助这些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. 我要完成的工作是打开“包含”应用程序,在本例中为“ + Quotes”,当用户在其“今日”通知视图中点击“ + Quotes”小部件时,并不完全确定要调用的名称,就像“日历”会在您在今天查看。 I've tried overlaying a button over the label which would call -(void)openURL:(NSURL *)URL completionHandler:(void (^)(BOOL success))completionHandler upon it being tapped, then open the Custom URL Scheme I have declared to open the Containing App. 我试图在按钮上覆盖一个按钮,该按钮在被点击时会调用-(void)openURL:(NSURL *)URL completionHandler:(void (^)(BOOL success))completionHandler ,然后打开我拥有的自定义URL方案声明要打开“包含应用程序”。 The issue is it doesn't open the Containing App. 问题是它无法打开“包含应用程序”。

+今日行情应用扩展插件

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

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. 我将其链接到“ Touch Up Inside”事件。 However, this also causes the app to launch when the user scrolls the Today view. 但是,这也会导致应用程序在用户滚动“今日”视图时启动。

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

I ran into the same issue. 我遇到了同样的问题。 However, it seems that there is no solution for now since the release notes for the first beta of iOS 8 mention: 但是,由于iOS 8的第一个beta 版本的发行说明中提到了以下内容,因此似乎目前尚无解决方案:

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。

Swift 2 version, according to Apple Doc 根据Apple Doc提供的 Swift 2版本

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

Swift 3 version Swift 3版本

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

And don't forget to add Custom URL Schemes into Info.plist 并且不要忘记将自定义URL方案添加到Info.plist

在此处输入图片说明

The answer by @sunseeker is the good one but it is "hidden" in the comments. @sunseeker的答案是一个很好的答案,但在评论中被“隐藏”了。 And as the accepted answer says that this isn't possible, it may mislead visitors. 而且正如公认的答案所言,这是不可能的,它可能会误导访问者。

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. 我正在优胜美地Beta 1中使用Xcode 6.0(6A215l)。

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. 相反,它使用NSExtensionContext的openURL:completionHandler:方法来告诉系统打开其包含的应用程序。 When an extension uses this method to open a URL, the system validates the request before fulfilling it. 当扩展使用此方法打开URL时,系统会在完成请求之前先验证请求。

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). 另一种不添加隐藏按钮的方法是在UILabel上添加UITapGestureRecognizer(确保在标签上将userInteractionEnabled设置为true)。 Check the recognizer state in the handler to make sure you reached UIGestureReconizerStateEnded (and not Cancelled or Failed) and then run your openUrl code. 检查处理程序中的识别器状态,以确保您达到UIGestureReconizerStateEnded(而不是Canceled或Failed),然后运行openUrl代码。

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 example of how to open the containing app and share User Defaults between app and Extension. 添加以下内容,以便应用程序知道应处理的URL。这是如何打开包含的应用程序以及如何在应用程序和扩展程序之间共享用户默认值的完整示例

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

相关问题 点击扩展程序(今天的扩展程序)后是否可以打开特定的viewcontroller(包含应用程序)? - is it possible to open specific viewcontroller(containing app) after tap on extension(today extension)? 如何使用NSUserDefaults与包含应用程序共享数据? - 今日Extension Widget - How to share data with containing app using NSUserDefaults ? - Today Extension Widget 打开包含今日扩展的应用程序时崩溃 - Crash when open containing app from today extension 从我的应用程序中的“今日扩展”(窗口小部件)打开Safari - Open Safari from my Today Extension (widget) within my app iOS应用内购买+今日扩展小部件 - iOS in-app-purchase + Today Extension widget 为什么 SwiftUI 中的 Today App Extension Widget 是白色的? - Why Today App Extension Widget in SwiftUI is white? 共享扩展以打开包含应用程序 - Share Extension to open containing app iOS Today扩展NSUserDefaults使用回调与包含应用程序共享数据 - iOS Today Extension NSUserDefaults Sharing Data with Containing App using Callbacks 从Swift 4中的Today扩展打开时,包含应用程序崩溃 - Containing App crashes when opened from Today extension in swift 4 在Swift中的主应用程序和Today小部件/扩展之间共享UI元素 - Sharing UI elements between main App and Today widget/extension in Swift
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM