forked from atomicbird/iOS-Extension-Demo
/
ShareViewController.m
89 lines (69 loc) · 3.11 KB
/
ShareViewController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//
// ShareViewController.m
// Share to Note
//
// Created by Tom Harrington on 11/14/14.
// Copyright (c) 2014 Atomic Bird LLC. All rights reserved.
//
#import "ShareViewController.h"
#import <MobileCoreServices/MobileCoreServices.h>
#import <DemoSharedCode/DemoSharedCode.h>
#import "Amplitude.h"
NSString *const kDemoNoteFilename = @"notes.bin";
@interface ShareViewController ()
@property (weak, nonatomic) IBOutlet UITextView *textView;
@end
@implementation ShareViewController
- (BOOL)isContentValid {
// Do validation of contentText and/or NSExtensionContext attachments here
return YES;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[[Amplitude instance] initializeApiKey:@"7abab557a07d1fabe2be243d17eeca71"];
[[Amplitude instance] logEvent:@"widget view did load"];
for (NSExtensionItem *item in self.extensionContext.inputItems) {
for (NSItemProvider *itemProvider in item.attachments) {
if ([itemProvider hasItemConformingToTypeIdentifier:(NSString *)kUTTypePropertyList]) {
[itemProvider loadItemForTypeIdentifier:(NSString *)kUTTypePropertyList options:nil completionHandler:^(NSDictionary *jsDict, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
NSDictionary *jsPreprocessingResults = jsDict[NSExtensionJavaScriptPreprocessingResultsKey];
NSString *selectedText = jsPreprocessingResults[@"selection"];
NSString *pageTitle = jsPreprocessingResults[@"title"];
if ([selectedText length] > 0) {
self.textView.text = selectedText;
} else if ([pageTitle length] > 0) {
self.textView.text = pageTitle;
}
});
}];
break;
}
}
}
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[Amplitude instance] initializeApiKey:@"7abab557a07d1fabe2be243d17eeca71"];
[[Amplitude instance] logEvent:@"widget view will appear"];
[self.textView becomeFirstResponder];
}
- (IBAction)createNote:(id)sender {
NSManagedObjectContext *context = [[DemoNoteManager sharedManager] createManagedObjectContextWithConcurrencyType:NSMainQueueConcurrencyType];
DemoNote *newNote = [NSEntityDescription insertNewObjectForEntityForName:@"DemoNote" inManagedObjectContext:context];
[newNote setText:self.textView.text];
[[DemoNoteManager sharedManager] saveManagedObjectContextWithNotification:context error:nil];
[[Amplitude instance] initializeApiKey:@"7abab557a07d1fabe2be243d17eeca71"];
[[Amplitude instance] logEvent:@"widget create note"];
[self.extensionContext completeRequestReturningItems:nil completionHandler:nil];
}
- (IBAction)cancel:(id)sender {
[self.extensionContext cancelRequestWithError:nil];
}
- (NSArray *)configurationItems {
// To add configuration options via table cells at the bottom of the sheet, return an array of SLComposeSheetConfigurationItem here.
return @[];
}
@end