Files
libr-ios/libxShareExt/ShareViewController.swift
2015-11-30 23:18:07 +08:00

149 lines
5.8 KiB
Swift
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//
// ShareViewController.swift
// libxShareExt
//
// Created by Yunlong Zheng on 15/10/30.
// Copyright © 2015年 librx. All rights reserved.
//
import UIKit
import Social
import MobileCoreServices
import Toast
class ShareViewController: SLComposeServiceViewController, SizeDelegate {
private var shareUrlString: String?
var selectedText = "网页" {
didSet {
self.config?.value = self.selectedText
}
}
weak var config : SLComposeSheetConfigurationItem?
lazy var sharedDefaults: NSUserDefaults? = {
return NSUserDefaults(suiteName: "group.libr.app")
}()
override func viewDidLoad() {
super.viewDidLoad()
let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.Dark)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.frame = self.view.bounds
blurEffectView.alpha = 0.6
//self.view.addSubview(blurEffectView)
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
}
}
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.
if let _ = sharedDefaults?.stringForKey("accessTokenKey") {
let shareContent = "\(getShareType(self.selectedText))@\(self.shareUrlString!)"
print("didSelectPost: \(shareContent)")
var results = sharedDefaults?.objectForKey("ShareContents") as? [String]
if results != nil {
results!.append(shareContent)
sharedDefaults?.setObject(results, forKey: "ShareContents")
}else{
let contents = [shareContent]
sharedDefaults?.setObject(contents, forKey: "ShareContents")
}
sharedDefaults!.synchronize()
self.view.makeToast("分享成功", duration:2.0, position: CSToastPositionCenter)
}else{
self.view.makeToast("Libr说:‘您还没登录应用~", duration:3.0, position: CSToastPositionCenter)
}
NSTimer.scheduledTimerWithTimeInterval(2.0, target: self, selector: Selector("dismiss"), userInfo: nil, repeats: false)
}
override func configurationItems() -> [AnyObject]! {
// To add configuration options via table cells at the bottom of the sheet, return an array of SLComposeSheetConfigurationItem here.
let c = SLComposeSheetConfigurationItem()
c.title = "分享类型"
c.value = self.selectedText
c.tapHandler = {
[unowned self] in
let tvc = TableViewController(style: .Grouped)
tvc.selectedSize = self.selectedText
tvc.delegate = self
self.pushConfigurationViewController(tvc)
}
self.config = c
return [c]
}
func dismiss() {
self.doneWithResults(nil)
}
func getShareType(type: String) -> String {
switch (type){
case "网页": return "page"
case "文章": return "article"
default: return "page"
}
}
}