Today App Extension Widget Tap To Open Containing App

IosIos8Ios App-ExtensionToday Extension

Ios Problem Overview


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][1]. 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))completionHandler upon 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 App Extension Widget][2]

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

[1]: https://developer.apple.com/library/prerelease/ios/documentation/General/Conceptual/ExtensibilityPG/index.html#//apple_ref/doc/uid/TP40014214-CH20-SW1 "Apple's Docs" [2]: http://i.stack.imgur.com/NGmcr.png

Ios Solutions


Solution 1 - Ios

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.

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

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:

> Known Issues: openURL does not work from an extension.

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

Solution 2 - Ios

Swift 2 version, according to Apple Doc

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

Swift 3 version

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

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

enter image description here

Solution 3 - Ios

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.

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.

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.

Solution 4 - Ios

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.

Solution 5 - Ios

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

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

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDaniel StormView Question on Stackoverflow
Solution 1 - IossunseekerView Answer on Stackoverflow
Solution 2 - IosQuanlongView Answer on Stackoverflow
Solution 3 - IosteriiehinaView Answer on Stackoverflow
Solution 4 - IosEPage_EdView Answer on Stackoverflow
Solution 5 - IosMikita MankoView Answer on Stackoverflow