#48 浏览器分享插件

This commit is contained in:
ylzheng
2015-11-25 23:15:28 +08:00
parent 773cb399cf
commit 1cfe51b057
4 changed files with 115 additions and 14 deletions
+4
View File
@@ -9,6 +9,7 @@
/* Begin PBXBuildFile section */
18988A881B93641900FCB1A8 /* libiconv.2.4.0.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 18988A871B93641900FCB1A8 /* libiconv.2.4.0.dylib */; };
4501B6A21BDFA66F00B9D4AF /* LogupViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4501B6A11BDFA66F00B9D4AF /* LogupViewController.swift */; };
4502BA4A1C05FF0000EBE41A /* TableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4502BA491C05FF0000EBE41A /* TableViewController.swift */; };
450BED401C03369F0094BDCF /* NSURL-Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 450BED3F1C03369F0094BDCF /* NSURL-Extensions.swift */; };
450C3AAC1BC621F7006B5A5F /* QuartzCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 450C3AAB1BC621F7006B5A5F /* QuartzCore.framework */; };
450C3AAE1BC62201006B5A5F /* ImageIO.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 450C3AAD1BC62201006B5A5F /* ImageIO.framework */; };
@@ -148,6 +149,7 @@
18988A871B93641900FCB1A8 /* libiconv.2.4.0.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libiconv.2.4.0.dylib; path = usr/lib/libiconv.2.4.0.dylib; sourceTree = SDKROOT; };
3C589578C2D1AD931B429F24 /* Pods-librx.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-librx.release.xcconfig"; path = "Pods/Target Support Files/Pods-librx/Pods-librx.release.xcconfig"; sourceTree = "<group>"; };
4501B6A11BDFA66F00B9D4AF /* LogupViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LogupViewController.swift; sourceTree = "<group>"; };
4502BA491C05FF0000EBE41A /* TableViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TableViewController.swift; sourceTree = "<group>"; };
450BED3F1C03369F0094BDCF /* NSURL-Extensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "NSURL-Extensions.swift"; sourceTree = "<group>"; };
450C3AAB1BC621F7006B5A5F /* QuartzCore.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = QuartzCore.framework; path = System/Library/Frameworks/QuartzCore.framework; sourceTree = SDKROOT; };
450C3AAD1BC62201006B5A5F /* ImageIO.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = ImageIO.framework; path = System/Library/Frameworks/ImageIO.framework; sourceTree = SDKROOT; };
@@ -467,6 +469,7 @@
456A681A1BE39C76001B3096 /* MainInterface.storyboard */,
456A681D1BE39C76001B3096 /* Info.plist */,
456A68261BE3A893001B3096 /* LibrExtensionJavaScript.js */,
4502BA491C05FF0000EBE41A /* TableViewController.swift */,
);
path = libxShareExt;
sourceTree = "<group>";
@@ -938,6 +941,7 @@
buildActionMask = 2147483647;
files = (
456A68191BE39C76001B3096 /* ShareViewController.swift in Sources */,
4502BA4A1C05FF0000EBE41A /* TableViewController.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
+6 -2
View File
@@ -18,8 +18,11 @@ class ShareService {
if let contents = dict {
for content in contents {
print("\(content)")
let post = Post(url: String(content))
let sepContents:[String] = String(content).componentsSeparatedByString("@");
let url = sepContents[1]
let type = sepContents[0]
let post = Post(url: url, type: type)
LibrxService.createPost(post) { (json) -> () in
print(json)
};
@@ -29,5 +32,6 @@ class ShareService {
}
}
}
+45 -12
View File
@@ -10,9 +10,16 @@ import UIKit
import Social
import MobileCoreServices
class ShareViewController: SLComposeServiceViewController {
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.librme")
@@ -21,6 +28,13 @@ class ShareViewController: SLComposeServiceViewController {
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! {
@@ -45,10 +59,7 @@ class ShareViewController: SLComposeServiceViewController {
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"])
}
}
@@ -78,14 +89,16 @@ class ShareViewController: SLComposeServiceViewController {
// 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]
let shareContent = "\(getShareType(self.selectedText))@\(self.shareUrlString!)"
print("didSelectPost: \(shareContent)")
var results = sharedDefaults?.objectForKey("ShareContents") as? [String]
if results != nil {
results!.append(self.shareUrlString!)
results!.append(shareContent)
sharedDefaults?.setObject(results, forKey: "ShareContents")
}else{
let contents = [self.shareUrlString!]
let contents = [shareContent]
sharedDefaults?.setObject(contents, forKey: "ShareContents")
}
@@ -93,11 +106,31 @@ class ShareViewController: SLComposeServiceViewController {
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 []
// 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 getShareType(type: String) -> String {
switch (type){
case "网页": return "page"
case "文章": return "article"
default: return "page"
}
}
}
+60
View File
@@ -0,0 +1,60 @@
//
// TableViewController.swift
// librx
//
// Created by Yunlong Zheng on 11/25/15.
// Copyright © 2015 librx. All rights reserved.
//
import UIKit
protocol SizeDelegate : class {
var selectedText : String {get set}
}
class TableViewController: UITableViewController {
var selectedSize : String?
weak var delegate : SizeDelegate?
override func viewDidLoad() {
self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "Cell")
self.tableView.scrollEnabled = false
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 2
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
cell.textLabel!.text = {
switch indexPath.row {
case 0:
return "网页"
case 1:
return "文章"
default:
return ""
}
}()
cell.accessoryType = (cell.textLabel!.text == self.selectedSize) ? .Checkmark : .None
return cell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let cell = tableView.cellForRowAtIndexPath(indexPath)!
let s = cell.textLabel!.text!
self.selectedSize = s
self.delegate?.selectedText = s
tableView.reloadData()
}
}