diff --git a/DemoNotes/AppDelegate.h b/DemoNotes/AppDelegate.h index 4f30c42..4f4b2e3 100644 --- a/DemoNotes/AppDelegate.h +++ b/DemoNotes/AppDelegate.h @@ -8,6 +8,9 @@ #import +extern NSString *noteRequestedNotification; +extern NSString *noteRequestedIndex; + @interface AppDelegate : UIResponder @property (strong, nonatomic) UIWindow *window; diff --git a/DemoNotes/AppDelegate.m b/DemoNotes/AppDelegate.m index d1891e2..3696755 100644 --- a/DemoNotes/AppDelegate.m +++ b/DemoNotes/AppDelegate.m @@ -9,6 +9,9 @@ #import "AppDelegate.h" #import "DetailViewController.h" +NSString *noteRequestedNotification = @"noteRequestedNotification"; +NSString *noteRequestedIndex = @"noteRequestedIndex"; + @interface AppDelegate () @end @@ -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; } @@ -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 { diff --git a/DemoNotes/DetailViewController.m b/DemoNotes/DetailViewController.m index 2ba289e..178a391 100644 --- a/DemoNotes/DetailViewController.m +++ b/DemoNotes/DetailViewController.m @@ -19,6 +19,7 @@ @implementation DetailViewController - (void)setDetailItem:(id)newDetailItem { if (_detailItem != newDetailItem) { + _detailItem.text = self.textView.text; _detailItem = newDetailItem; // Update the view. diff --git a/DemoNotes/Info.plist b/DemoNotes/Info.plist index 1c74592..20af25c 100644 --- a/DemoNotes/Info.plist +++ b/DemoNotes/Info.plist @@ -18,6 +18,19 @@ 1.0 CFBundleSignature ???? + CFBundleURLTypes + + + CFBundleTypeRole + Editor + CFBundleURLName + com.atomicbird.demonote + CFBundleURLSchemes + + demonote + + + CFBundleVersion 1 LSRequiresIPhoneOS diff --git a/DemoNotes/MasterViewController.m b/DemoNotes/MasterViewController.m index 1c7bfba..3863017 100644 --- a/DemoNotes/MasterViewController.m +++ b/DemoNotes/MasterViewController.m @@ -9,6 +9,7 @@ #import "MasterViewController.h" #import "DetailViewController.h" #import +#import "AppDelegate.h" NSString *const kDemoNoteFilename = @"notes.bin"; @@ -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]; @@ -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 @@ -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"]; @@ -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; diff --git a/DemoToday/MainInterface.storyboard b/DemoToday/MainInterface.storyboard index 576d22e..e2a075b 100644 --- a/DemoToday/MainInterface.storyboard +++ b/DemoToday/MainInterface.storyboard @@ -13,7 +13,7 @@ - + diff --git a/DemoToday/TodayViewController.m b/DemoToday/TodayViewController.m index 1b72ae9..576ecca 100644 --- a/DemoToday/TodayViewController.m +++ b/DemoToday/TodayViewController.m @@ -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