Skip to content

Commit

Permalink
Give the app a URL scheme, and have the today extension use it to lau…
Browse files Browse the repository at this point in the history
…nch the app when the user taps a note.
  • Loading branch information
atomicbird committed Nov 14, 2014
1 parent 2cbde1d commit dc5e4ea
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 3 deletions.
3 changes: 3 additions & 0 deletions DemoNotes/AppDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

#import <UIKit/UIKit.h>

extern NSString *noteRequestedNotification;
extern NSString *noteRequestedIndex;

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
Expand Down
15 changes: 15 additions & 0 deletions DemoNotes/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
#import "AppDelegate.h"
#import "DetailViewController.h"

NSString *noteRequestedNotification = @"noteRequestedNotification";
NSString *noteRequestedIndex = @"noteRequestedIndex";

@interface AppDelegate () <UISplitViewControllerDelegate>

@end
Expand All @@ -22,6 +25,7 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
UINavigationController *navigationController = [splitViewController.viewControllers lastObject];
navigationController.topViewController.navigationItem.leftBarButtonItem = splitViewController.displayModeButtonItem;
splitViewController.delegate = self;

return YES;
}

Expand All @@ -47,6 +51,17 @@ - (void)applicationWillTerminate:(UIApplication *)application {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
NSString *noteIndexString = [url resourceSpecifier];
NSInteger noteIndex = [noteIndexString integerValue];
[[NSNotificationCenter defaultCenter] postNotificationName:noteRequestedNotification
object:self
userInfo:@{noteRequestedIndex: @(noteIndex)}];

return YES;
}

#pragma mark - Split view

- (BOOL)splitViewController:(UISplitViewController *)splitViewController collapseSecondaryViewController:(UIViewController *)secondaryViewController ontoPrimaryViewController:(UIViewController *)primaryViewController {
Expand Down
1 change: 1 addition & 0 deletions DemoNotes/DetailViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ @implementation DetailViewController

- (void)setDetailItem:(id)newDetailItem {
if (_detailItem != newDetailItem) {
_detailItem.text = self.textView.text;
_detailItem = newDetailItem;

// Update the view.
Expand Down
13 changes: 13 additions & 0 deletions DemoNotes/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@
<string>1.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLName</key>
<string>com.atomicbird.demonote</string>
<key>CFBundleURLSchemes</key>
<array>
<string>demonote</string>
</array>
</dict>
</array>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSRequiresIPhoneOS</key>
Expand Down
26 changes: 24 additions & 2 deletions DemoNotes/MasterViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#import "MasterViewController.h"
#import "DetailViewController.h"
#import <DemoSharedCode/DemoSharedCode.h>
#import "AppDelegate.h"

NSString *const kDemoNoteFilename = @"notes.bin";

Expand All @@ -33,7 +34,7 @@ - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.navigationItem.leftBarButtonItem = self.editButtonItem;

UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject:)];
self.navigationItem.rightBarButtonItem = addButton;
self.detailViewController = (DetailViewController *)[[self.splitViewController.viewControllers lastObject] topViewController];
Expand All @@ -51,6 +52,11 @@ - (void)viewDidLoad {
}

_hasChangesPredicate = [NSPredicate predicateWithFormat:@"hasChanges = YES"];

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(openRequestedNote:)
name:noteRequestedNotification
object:[[UIApplication sharedApplication] delegate]];
}

- (void)viewWillAppear:(BOOL)animated
Expand All @@ -63,6 +69,22 @@ - (void)viewWillAppear:(BOOL)animated
}
}

- (void)openRequestedNote:(NSNotification *)notification
{
NSInteger requestedItemIndex = [[notification userInfo][noteRequestedIndex] integerValue];
if (requestedItemIndex < self.objects.count) {
[self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:requestedItemIndex inSection:0] animated:NO scrollPosition:UITableViewScrollPositionTop];

if ([[[self navigationController] visibleViewController] isKindOfClass:[DetailViewController class]]) {
DetailViewController *detailViewController = (DetailViewController *)[[self navigationController] visibleViewController];
DemoNote *requestedNote = self.objects[requestedItemIndex];
detailViewController.detailItem = requestedNote;
} else {
[self performSegueWithIdentifier:@"showDetail" sender:self];
}
}
}

- (NSURL *)demoNoteFileURL
{
NSURL *groupURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.com.atomicbird.demonotes"];
Expand Down Expand Up @@ -123,7 +145,7 @@ - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

DemoNote *object = self.objects[indexPath.row];
cell.textLabel.text = [object text];
return cell;
Expand Down
2 changes: 1 addition & 1 deletion DemoToday/MainInterface.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<prototypes>
<tableViewCell contentMode="scaleToFill" selectionStyle="default" indentationWidth="10" reuseIdentifier="demoTodayCell" id="nHe-K2-Hrx">
<tableViewCell contentMode="scaleToFill" selectionStyle="none" indentationWidth="10" reuseIdentifier="demoTodayCell" id="nHe-K2-Hrx">
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="nHe-K2-Hrx" id="tmd-Hm-t88">
<autoresizingMask key="autoresizingMask"/>
Expand Down
6 changes: 6 additions & 0 deletions DemoToday/TodayViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,10 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"demonote:%ld", (long)indexPath.row]];
[self.extensionContext openURL:url completionHandler:nil];
}

@end

0 comments on commit dc5e4ea

Please sign in to comment.