mirror of
https://github.com/wahyd4/libr-ios.git
synced 2026-08-23 03:56:03 +10:00
104 lines
4.3 KiB
Swift
104 lines
4.3 KiB
Swift
//
|
|
// ShareViewController.swift
|
|
// libxShareExt
|
|
//
|
|
// Created by Yunlong Zheng on 15/10/30.
|
|
// Copyright © 2015年 librx. All rights reserved.
|
|
//
|
|
|
|
import UIKit
|
|
import Social
|
|
import MobileCoreServices
|
|
|
|
class ShareViewController: SLComposeServiceViewController {
|
|
|
|
private var shareUrlString: String?
|
|
|
|
lazy var sharedDefaults: NSUserDefaults? = {
|
|
return NSUserDefaults(suiteName: "group.librme")
|
|
}()
|
|
|
|
override func viewDidLoad() {
|
|
super.viewDidLoad()
|
|
|
|
for item: AnyObject in self.extensionContext!.inputItems {
|
|
let inputItem = item as! NSExtensionItem
|
|
for provider: AnyObject in inputItem.attachments! {
|
|
|
|
let itemProvider = provider as! NSItemProvider
|
|
if itemProvider.hasItemConformingToTypeIdentifier(kUTTypePropertyList as String) {
|
|
// You _HAVE_ to call loadItemForTypeIdentifier in order to get the JS injected
|
|
itemProvider.loadItemForTypeIdentifier(kUTTypePropertyList as String, options: nil, completionHandler: {
|
|
(list, error) in
|
|
if let results = list as? NSDictionary {
|
|
NSOperationQueue.mainQueue().addOperationWithBlock {
|
|
// We don't actually care about this...
|
|
self.itemLoadCompletedWithPreprocessingResults(results["NSExtensionJavaScriptPreprocessingResultsKey"] as! NSDictionary)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
func itemLoadCompletedWithPreprocessingResults(javaScriptPreprocessingResults: NSDictionary) {
|
|
if let urlString = javaScriptPreprocessingResults["baseURI"] as? String {
|
|
|
|
self.shareUrlString = urlString
|
|
//print("share url:\(urlString)\n")
|
|
//self.doneWithResults(["message": "Successfully added to the note app"])
|
|
}
|
|
}
|
|
|
|
func doneWithResults(resultsForJavaScriptFinalizeArg: NSDictionary?) {
|
|
if let resultsForJavaScriptFinalize = resultsForJavaScriptFinalizeArg {
|
|
|
|
let identifierType = NSString(format: kUTTypePropertyList, NSUTF8StringEncoding)
|
|
let resultsDictionary = [NSExtensionJavaScriptFinalizeArgumentKey: resultsForJavaScriptFinalize]
|
|
let resultsProvider = NSItemProvider(item: resultsDictionary, typeIdentifier: identifierType as String)
|
|
let resultsItem = NSExtensionItem()
|
|
resultsItem.attachments = [resultsProvider]
|
|
self.extensionContext!.completeRequestReturningItems([resultsItem], completionHandler: nil)
|
|
|
|
} else {
|
|
// 就算我们没有任何要返回的信息,也要执行这段代码,用于告知我们的Action扩展已经完成了逻辑处理。
|
|
self.extensionContext!.completeRequestReturningItems(nil, completionHandler: nil)
|
|
}
|
|
}
|
|
|
|
|
|
override func isContentValid() -> Bool {
|
|
// Do validation of contentText and/or NSExtensionContext attachments here
|
|
return true
|
|
}
|
|
|
|
override func didSelectPost() {
|
|
// This is called after the user selects Post. Do the upload of contentText and/or NSExtensionContext attachments.
|
|
|
|
// 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.
|
|
print("didSelectPost: \(self.shareUrlString)")
|
|
|
|
var results = sharedDefaults?.objectForKey("ShareContents") as? [AnyObject]
|
|
if results != nil {
|
|
results!.append(self.shareUrlString!)
|
|
sharedDefaults?.setObject(results, forKey: "ShareContents")
|
|
}else{
|
|
let contents = [self.shareUrlString!]
|
|
sharedDefaults?.setObject(contents, forKey: "ShareContents")
|
|
}
|
|
|
|
sharedDefaults!.synchronize()
|
|
self.extensionContext!.completeRequestReturningItems([], completionHandler: nil)
|
|
}
|
|
|
|
|
|
|
|
override func configurationItems() -> [AnyObject]! {
|
|
// To add configuration options via table cells at the bottom of the sheet, return an array of SLComposeSheetConfigurationItem here.
|
|
return []
|
|
}
|
|
|
|
}
|