From 2324cd61e894291ccbf48b711adc2e682eb03391 Mon Sep 17 00:00:00 2001 From: ylzheng Date: Mon, 23 Nov 2015 19:36:37 +0800 Subject: [PATCH] share plugin --- librx.xcodeproj/project.pbxproj | 4 +++ librx/AppDelegate.swift | 1 + librx/Base.lproj/LaunchScreen.xib | 4 +-- librx/Base.lproj/Main.storyboard | 4 +-- .../AppIcon.appiconset/Contents.json | 7 ++++ librx/LibrxService.swift | 2 ++ librx/LocalStore.swift | 2 ++ librx/ProfileViewController.swift | 7 +++- librx/ShareService.swift | 33 ++++++++++++++++++ libxShareExt/ShareViewController.swift | 34 ++++++++++++++----- 10 files changed, 85 insertions(+), 13 deletions(-) create mode 100644 librx/ShareService.swift diff --git a/librx.xcodeproj/project.pbxproj b/librx.xcodeproj/project.pbxproj index 309a65e..ec18476 100644 --- a/librx.xcodeproj/project.pbxproj +++ b/librx.xcodeproj/project.pbxproj @@ -62,6 +62,7 @@ 45A50FC01BC96608009897FF /* Profile.swift in Sources */ = {isa = PBXBuildFile; fileRef = 45A50FBF1BC96608009897FF /* Profile.swift */; }; 45AD0E2E1B857EA600283FCC /* WebViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 45AD0E2D1B857EA600283FCC /* WebViewController.swift */; }; 45B11CD81B6FD12A008D6049 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 45B11CD71B6FD12A008D6049 /* Images.xcassets */; }; + 45C011E41C01FC9900A9BACF /* ShareService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 45C011E31C01FC9900A9BACF /* ShareService.swift */; }; 45C24EFB1BAD899D00BACB81 /* DashViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 45C24EFA1BAD899D00BACB81 /* DashViewController.swift */; }; 45C24EFE1BADA48E00BACB81 /* UIColorExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 45C24EFD1BADA48E00BACB81 /* UIColorExtension.swift */; }; 45CE21A31BC2797300642186 /* CoreTelephony.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 45CE21A21BC2797300642186 /* CoreTelephony.framework */; }; @@ -221,6 +222,7 @@ 45A50FBF1BC96608009897FF /* Profile.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Profile.swift; sourceTree = ""; }; 45AD0E2D1B857EA600283FCC /* WebViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = WebViewController.swift; sourceTree = ""; }; 45B11CD71B6FD12A008D6049 /* Images.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Images.xcassets; sourceTree = ""; }; + 45C011E31C01FC9900A9BACF /* ShareService.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ShareService.swift; sourceTree = ""; }; 45C24EFA1BAD899D00BACB81 /* DashViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DashViewController.swift; sourceTree = ""; }; 45C24EFD1BADA48E00BACB81 /* UIColorExtension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIColorExtension.swift; sourceTree = ""; }; 45CE21A21BC2797300642186 /* CoreTelephony.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreTelephony.framework; path = System/Library/Frameworks/CoreTelephony.framework; sourceTree = SDKROOT; }; @@ -526,6 +528,7 @@ 453B01521BAADC2400E285A3 /* JsonPaser.swift */, 45CE21A41BC35D7800642186 /* DateUtil.swift */, 4599CCE01BFCAC7E00F263BC /* PostsStore.swift */, + 45C011E31C01FC9900A9BACF /* ShareService.swift */, ); name = services; sourceTree = ""; @@ -884,6 +887,7 @@ 45A50FC01BC96608009897FF /* Profile.swift in Sources */, 452DC1191B6287E600847691 /* AppDelegate.swift in Sources */, 45681F341B7F7C8A00D1E096 /* User.swift in Sources */, + 45C011E41C01FC9900A9BACF /* ShareService.swift in Sources */, 457BF0E11B9C905B00A491D6 /* ExploreCollectionReusableView.swift in Sources */, 45681F301B7F79AF00D1E096 /* Shareable.swift in Sources */, 4544DAD61BCD4358002B6666 /* SettingTableViewController.swift in Sources */, diff --git a/librx/AppDelegate.swift b/librx/AppDelegate.swift index 0d2399a..2fef87d 100644 --- a/librx/AppDelegate.swift +++ b/librx/AppDelegate.swift @@ -40,6 +40,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate { func applicationWillEnterForeground(application: UIApplication) { // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. + ShareService().fetchAndSaveNewMemos() } func applicationDidBecomeActive(application: UIApplication) { diff --git a/librx/Base.lproj/LaunchScreen.xib b/librx/Base.lproj/LaunchScreen.xib index 6e9a2dc..e9a3752 100644 --- a/librx/Base.lproj/LaunchScreen.xib +++ b/librx/Base.lproj/LaunchScreen.xib @@ -1,7 +1,7 @@ - + - + diff --git a/librx/Base.lproj/Main.storyboard b/librx/Base.lproj/Main.storyboard index 0b07f04..28b236b 100644 --- a/librx/Base.lproj/Main.storyboard +++ b/librx/Base.lproj/Main.storyboard @@ -444,7 +444,7 @@ - + @@ -835,7 +835,7 @@ - + diff --git a/librx/Images.xcassets/AppIcon.appiconset/Contents.json b/librx/Images.xcassets/AppIcon.appiconset/Contents.json index acfc218..b76cefa 100644 --- a/librx/Images.xcassets/AppIcon.appiconset/Contents.json +++ b/librx/Images.xcassets/AppIcon.appiconset/Contents.json @@ -103,6 +103,13 @@ "role" : "appLauncher", "subtype" : "38mm" }, + { + "size" : "44x44", + "idiom" : "watch", + "scale" : "2x", + "role" : "longLook", + "subtype" : "42mm" + }, { "size" : "86x86", "idiom" : "watch", diff --git a/librx/LibrxService.swift b/librx/LibrxService.swift index 6c6f977..3c971df 100644 --- a/librx/LibrxService.swift +++ b/librx/LibrxService.swift @@ -476,4 +476,6 @@ struct LibrxService { Alamofire.request(.POST, urlString, headers: headers) } + + } \ No newline at end of file diff --git a/librx/LocalStore.swift b/librx/LocalStore.swift index 4dd7e25..ced80ee 100644 --- a/librx/LocalStore.swift +++ b/librx/LocalStore.swift @@ -62,4 +62,6 @@ struct LocalStore { self.deleteAccessUserId() } + + } diff --git a/librx/ProfileViewController.swift b/librx/ProfileViewController.swift index 3818b1f..f544aae 100644 --- a/librx/ProfileViewController.swift +++ b/librx/ProfileViewController.swift @@ -20,13 +20,14 @@ class ProfileViewController: UIViewController{ @IBOutlet weak var settingButton: UIButton! @IBOutlet weak var backButton: UIButton! - private var containerViewController: ContainerViewController! private var screenSize: CGRect! private var screenWidth: CGFloat! private var screenHeight: CGFloat! private var isFollowed: Bool = true + private var profile: Profile? + // 其他用户个人中心 var forceHiddenBackButton: Bool = false var isMe:Bool = true @@ -92,6 +93,7 @@ class ProfileViewController: UIViewController{ if let selfView = self { if let thatProfile = profile { + selfView.profile = thatProfile let avatar = NSURL(string: thatProfile.avatar) selfView.avatarImageView.sd_setImageWithURL(avatar, placeholderImage: UIImage(named: "default")) selfView.nameLabel.text = thatProfile.name @@ -127,6 +129,7 @@ class ProfileViewController: UIViewController{ if let selfView = self { if let _ = profile { + self?.profile = profile let avatar = NSURL(string: profile!.avatar) selfView.avatarImageView.sd_setImageWithURL(avatar, placeholderImage: UIImage(named: "default")) selfView.nameLabel.text = profile!.name @@ -157,11 +160,13 @@ class ProfileViewController: UIViewController{ if self.isFollowed { self.isFollowed = false self.followButton.setTitle("关注", forState: .Normal) + self.follosNumLabel.text = String(self.profile!.followsCount-1) LibrxService.unFollow(userId!) return } else { self.isFollowed = true self.followButton.setTitle("已关注", forState: .Normal) + self.follosNumLabel.text = String(self.profile!.followsCount+1) LibrxService.follow(userId!) return } diff --git a/librx/ShareService.swift b/librx/ShareService.swift new file mode 100644 index 0000000..1b835f6 --- /dev/null +++ b/librx/ShareService.swift @@ -0,0 +1,33 @@ +// +// ShareService.swift +// librx +// +// Created by Yunlong Zheng on 11/22/15. +// Copyright © 2015 librx. All rights reserved. +// + +import Foundation + +class ShareService { + + func fetchAndSaveNewMemos(){ + + let sharedDefaults = NSUserDefaults(suiteName: "group.librme") + print("load share content from share storage") + let dict = sharedDefaults?.objectForKey("ShareContents") as? [AnyObject] + if let contents = dict { + + for content in contents { + print("\(content)") + let post = Post(url: String(content)) + LibrxService.createPost(post) { (json) -> () in + print(json) + }; + } + + sharedDefaults?.removeObjectForKey("ShareContents") + } + + } + +} diff --git a/libxShareExt/ShareViewController.swift b/libxShareExt/ShareViewController.swift index 2595d39..dfe1339 100644 --- a/libxShareExt/ShareViewController.swift +++ b/libxShareExt/ShareViewController.swift @@ -12,12 +12,19 @@ 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 @@ -31,29 +38,30 @@ class ShareViewController: SLComposeServiceViewController { } }) } + } } } func itemLoadCompletedWithPreprocessingResults(javaScriptPreprocessingResults: NSDictionary) { - if let text = javaScriptPreprocessingResults["baseURI"] as? String { - let userDefaults = NSUserDefaults.init(suiteName: "group.librme") - print(">>>>>\(userDefaults!.integerForKey("accessTokenKey"))\n") - print("share url\(text)\n") - self.doneWithResults(["message": "Successfully added to the note app"]) + 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 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) @@ -70,8 +78,18 @@ 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: \(contentText)") + 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) }