Files: 2c1932822a8e06b5876eb875f19924a15ae91539 / localnative-ios / share-ext / ShareViewController.swift
5222 bytesRaw
1 | /* |
2 | Local Native |
3 | Copyright (C) 2018-2019 Yi Wang |
4 | |
5 | This program is free software: you can redistribute it and/or modify |
6 | it under the terms of the GNU Affero General Public License as published by |
7 | the Free Software Foundation, either version 3 of the License, or |
8 | (at your option) any later version. |
9 | |
10 | This program is distributed in the hope that it will be useful, |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | GNU Affero General Public License for more details. |
14 | |
15 | You should have received a copy of the GNU Affero General Public License |
16 | along with this program. If not, see <https://www.gnu.org/licenses/>. |
17 | */ |
18 | // |
19 | // ShareViewController.swift |
20 | // share-ext |
21 | // |
22 | // Created by Yi Wang on 9/16/18. |
23 | // |
24 | // |
25 | |
26 | import MobileCoreServices |
27 | import UIKit |
28 | import Social |
29 | import UITextView_Placeholder |
30 | import MMWormhole |
31 | let wormhole = MMWormhole(applicationGroupIdentifier: "group.app.localnative.ios", optionalDirectory: "wormhole") |
32 | |
33 | class ShareViewController: UIViewController { |
34 | weak var saveButton: UIButton! |
35 | weak var cancelButton: UIButton! |
36 | weak var titleText: UITextView! |
37 | weak var urlText: UITextView! |
38 | weak var tagsText: UITextView! |
39 | weak var descriptionText: UITextView! |
40 | func cancelButtonTouchDown(_ sender: Any) { |
41 | self.extensionContext!.completeRequest(returningItems: [], completionHandler: nil) |
42 | } |
43 | |
44 | func openURL(_ url: URL) -> Bool { |
45 | var responder: UIResponder? = self |
46 | while responder != nil { |
47 | if let application = responder as? UIApplication { |
48 | return application.perform(#selector(openURL(_:)), with: url) != nil |
49 | } |
50 | responder = responder?.next |
51 | } |
52 | return false |
53 | } |
54 | |
55 | func saveButtonTouchDown(_ sender: Any) { |
56 | let message : [String: Any] = [ |
57 | "action": "insert", |
58 | |
59 | "title": titleText.text, |
60 | "url": urlText.text, |
61 | "tags": tagsText.text, |
62 | "description": descriptionText.text, |
63 | "comments": "", |
64 | "annotations": "", |
65 | |
66 | "limit": 10, |
67 | "offset": 0, |
68 | "is_public": false |
69 | ] |
70 | |
71 | let valid = JSONSerialization.isValidJSONObject(message) |
72 | |
73 | let url = URL(string: "localnative://insert") as! URL |
74 | openURL(url) |
75 | // send json after container app open |
76 | DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { |
77 | if valid { |
78 | let jsonText = try? JSONSerialization.data(withJSONObject: message) |
79 | wormhole.passMessageObject( String(data: jsonText!, encoding: .utf8)! as NSCoding, identifier: "message") |
80 | } |
81 | self.extensionContext!.completeRequest(returningItems: [], completionHandler: nil) |
82 | } |
83 | |
84 | } |
85 | |
86 | override func viewDidLoad() { |
87 | // super.viewDidLoad() |
88 | titleText.placeholder = "title" |
89 | urlText.placeholder = "url" |
90 | tagsText.placeholder = "type to add tags, comma or space as tag seperator" |
91 | descriptionText.placeholder = "description" |
92 | // tagsText.becomeFirstResponder() |
93 | // https://hackernoon.com/how-to-build-an-ios-share-extension-in-swift-4a2019935b2e |
94 | let extensionItem = extensionContext?.inputItems.first as! NSExtensionItem |
95 | let itemProvider = extensionItem.attachments?.first as! NSItemProvider |
96 | let propertyList = String(kUTTypePropertyList) |
97 | if itemProvider.hasItemConformingToTypeIdentifier(propertyList) { |
98 | itemProvider.loadItem(forTypeIdentifier: propertyList, options: nil, completionHandler: { (item, error) -> Void in |
99 | guard let dictionary = item as? NSDictionary else { return } |
100 | OperationQueue.main.addOperation { |
101 | if let results = dictionary[NSExtensionJavaScriptPreprocessingResultsKey] as? NSDictionary |
102 | { |
103 | print("results") |
104 | print(results) |
105 | self.urlText.text = results["url"] as? String |
106 | self.titleText.text = results["title"] as? String |
107 | |
108 | } |
109 | } |
110 | }) |
111 | } else { |
112 | print("error") |
113 | } |
114 | } |
115 | func isContentValid() -> Bool { |
116 | // Do validation of contentText and/or NSExtensionContext attachments here |
117 | return true |
118 | } |
119 | |
120 | func didSelectPost() { |
121 | // This is called after the user selects Post. Do the upload of contentText and/or NSExtensionContext attachments. |
122 | |
123 | // Inform the host that we're done, so it un-blocks its UI. Note: Alternatively you could call super's -didSelectPost, which will similarly complete the extension context. |
124 | self.extensionContext!.completeRequest(returningItems: [], completionHandler: nil) |
125 | } |
126 | |
127 | func configurationItems() -> [Any]! { |
128 | // To add configuration options via table cells at the bottom of the sheet, return an array of SLComposeSheetConfigurationItem here. |
129 | return [] |
130 | } |
131 | |
132 | } |
133 |
Built with git-ssb-web