Created
December 14, 2015 17:33
-
-
Save hferrone/b2837525c50342b92fcb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
override func didSelectPost() { | |
let extensionItem = extensionContext?.inputItems.first as! NSExtensionItem | |
let itemProvider = extensionItem.attachments?.first as! NSItemProvider | |
let contentType = String(kUTTypePropertyList) | |
if itemProvider.hasItemConformingToTypeIdentifier(contentType) { | |
itemProvider.loadItemForTypeIdentifier(contentType, options: nil, completionHandler: { (item, error) -> Void in | |
let itemDict = item as! NSDictionary | |
NSOperationQueue.mainQueue().addOperationWithBlock({ () -> Void in | |
let results = itemDict[NSExtensionJavaScriptPreprocessingResultsKey] as! NSDictionary | |
self.articleUrlString = results.objectForKey("currentUrl") as! String | |
dispatch_async(dispatch_get_main_queue(), { () -> Void in | |
self.postArticleToFirebase() | |
}) | |
}) | |
}) | |
} else { | |
print("Error") | |
} | |
} | |
func postArticleToFirebase() { | |
let articleURL = NSURL(string: "https://co2.firebaseio.com/posts.json") | |
let postRequest = NSMutableURLRequest(URL: articleURL!) | |
let postSession = NSURLSession.sharedSession() | |
do { | |
postRequest.HTTPBody = try NSJSONSerialization.dataWithJSONObject(createPostDictionary(), options: .PrettyPrinted) | |
} catch { | |
print(error) | |
} | |
postRequest.addValue("application/json", forHTTPHeaderField: "Content-Type") | |
postRequest.HTTPMethod = "POST" | |
let sessionTask = postSession.dataTaskWithRequest(postRequest) { (data, response, postError) -> Void in | |
if postError != nil { | |
print("Failure with error: \(postError?.localizedDescription)") | |
} else { | |
let postSuccessAlertView = UIAlertController(title: "Success!", message: "Your post has been shared", preferredStyle: .Alert) | |
let cancelAction = UIAlertAction(title: "Ok", style: .Cancel, handler: { (alertAction) -> Void in | |
self.extensionContext?.completeRequestReturningItems([], completionHandler: nil) | |
}) | |
postSuccessAlertView.addAction(cancelAction) | |
self.presentViewController(postSuccessAlertView, animated: true, completion: nil) | |
print("Posted!") | |
} | |
} | |
sessionTask.resume() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment