mirror of
https://github.com/samuelclay/NewsBlur.git
synced 2025-09-18 21:50:56 +00:00
iOS: tweaked #755 (sharing extension)
This commit is contained in:
parent
a5f84ecc45
commit
7cf1049f3b
3 changed files with 77 additions and 28 deletions
|
@ -8,7 +8,7 @@
|
|||
#import <MobileCoreServices/MobileCoreServices.h>
|
||||
|
||||
//#define DEBUG 1
|
||||
//#define PROD_DEBUG 1
|
||||
#define PROD_DEBUG 1
|
||||
|
||||
#ifdef DEBUG
|
||||
#define BACKGROUND_REFRESH_SECONDS -5
|
||||
|
|
|
@ -31,8 +31,15 @@
|
|||
<key>NSExtensionAttributes</key>
|
||||
<dict>
|
||||
<key>NSExtensionActivationRule</key>
|
||||
<string>TRUEPREDICATE</string>
|
||||
</dict>
|
||||
<dict>
|
||||
<key>NSExtensionActivationSupportsWebURLWithMaxCount</key>
|
||||
<integer>1</integer>
|
||||
<key>NSExtensionActivationSupportsText</key>
|
||||
<integer>1</integer>
|
||||
</dict>
|
||||
<key>NSExtensionActivationUsesStrictMatching</key>
|
||||
<integer>1</integer>
|
||||
</dict>
|
||||
<key>NSExtensionMainStoryboard</key>
|
||||
<string>MainInterface</string>
|
||||
<key>NSExtensionPointIdentifier</key>
|
||||
|
|
|
@ -18,7 +18,7 @@
|
|||
- (BOOL)isContentValid {
|
||||
for (NSExtensionItem *item in self.extensionContext.inputItems) {
|
||||
for (NSItemProvider *itemProvider in item.attachments) {
|
||||
return [itemProvider hasItemConformingToTypeIdentifier:(NSString *)kUTTypeURL];
|
||||
return [itemProvider hasItemConformingToTypeIdentifier:(NSString *)kUTTypeURL] || [itemProvider hasItemConformingToTypeIdentifier:(NSString *)kUTTypeText];
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -26,36 +26,78 @@
|
|||
}
|
||||
|
||||
- (void)didSelectPost {
|
||||
NSItemProvider *itemProvider = [self providerWithURL];
|
||||
|
||||
if (itemProvider) {
|
||||
[itemProvider loadItemForTypeIdentifier:(NSString *)kUTTypeURL options:nil completionHandler:^(NSURL *item, NSError * _Null_unspecified error) {
|
||||
if (item) {
|
||||
[self sendURL:item orText:nil];
|
||||
[self.extensionContext completeRequestReturningItems:@[] completionHandler:nil];
|
||||
}
|
||||
}];
|
||||
} else {
|
||||
itemProvider = [self providerWithText];
|
||||
|
||||
if (itemProvider) {
|
||||
[itemProvider loadItemForTypeIdentifier:(NSString *)kUTTypeText options:nil completionHandler:^(NSString *item, NSError * _Null_unspecified error) {
|
||||
if (item) {
|
||||
[self sendURL:nil orText:item];
|
||||
[self.extensionContext completeRequestReturningItems:@[] completionHandler:nil];
|
||||
}
|
||||
}];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
- (NSItemProvider *)providerWithURL {
|
||||
for (NSExtensionItem *extensionItem in self.extensionContext.inputItems) {
|
||||
for (NSItemProvider *itemProvider in extensionItem.attachments) {
|
||||
if ([itemProvider hasItemConformingToTypeIdentifier:(NSString *)kUTTypeURL]) {
|
||||
[itemProvider loadItemForTypeIdentifier:(NSString *)kUTTypeURL options:nil completionHandler:^(NSURL *item, NSError * _Null_unspecified error) {
|
||||
if (item) {
|
||||
NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.com.newsblur.NewsBlur-Group"];
|
||||
NSString *host = [defaults objectForKey:@"share:host"];
|
||||
NSString *token = [defaults objectForKey:@"share:token"];
|
||||
NSCharacterSet *characterSet = [NSCharacterSet URLQueryAllowedCharacterSet];
|
||||
NSString *encodedURL = [item.absoluteString stringByAddingPercentEncodingWithAllowedCharacters:characterSet];
|
||||
NSString *encodedComments = [self.contentText stringByAddingPercentEncodingWithAllowedCharacters:characterSet];
|
||||
// NSInteger time = [[NSDate date] timeIntervalSince1970];
|
||||
NSURLSession *mySession = [self configureMySession];
|
||||
// NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://%@/api/add_site_load_script/%@/?url=%@&time=%@&comments=%@", host, token, encodedURL, @(time), encodedComments]];
|
||||
// NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://%@/api/share_story/%@", host, token]];
|
||||
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://%@/api/share_story", host]];
|
||||
NSLog(@"Host: %@; secret token: %@; url: %@", host, token, url);
|
||||
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
|
||||
[request setHTTPMethod:@"POST"];
|
||||
NSString *postBody = [NSString stringWithFormat:@"story_url=%@&comments=%@", encodedURL, encodedComments];
|
||||
[request setHTTPBody:[postBody dataUsingEncoding:NSUTF8StringEncoding]];
|
||||
NSURLSessionTask *myTask = [mySession dataTaskWithRequest:request];
|
||||
[myTask resume];
|
||||
|
||||
[self.extensionContext completeRequestReturningItems:@[] completionHandler:nil];
|
||||
}
|
||||
}];
|
||||
return itemProvider;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil;
|
||||
}
|
||||
|
||||
- (NSItemProvider *)providerWithText {
|
||||
for (NSExtensionItem *extensionItem in self.extensionContext.inputItems) {
|
||||
for (NSItemProvider *itemProvider in extensionItem.attachments) {
|
||||
if ([itemProvider hasItemConformingToTypeIdentifier:(NSString *)kUTTypeText]) {
|
||||
return itemProvider;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil;
|
||||
}
|
||||
|
||||
- (void)sendURL:(NSURL *)url orText:(NSString *)text {
|
||||
NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.com.newsblur.NewsBlur-Group"];
|
||||
NSString *host = [defaults objectForKey:@"share:host"];
|
||||
// NSString *token = [defaults objectForKey:@"share:token"];
|
||||
NSString *comments = self.contentText;
|
||||
|
||||
if (text && [comments isEqualToString:text]) {
|
||||
comments = @"";
|
||||
}
|
||||
|
||||
NSCharacterSet *characterSet = [NSCharacterSet URLQueryAllowedCharacterSet];
|
||||
NSString *encodedURL = url ? [url.absoluteString stringByAddingPercentEncodingWithAllowedCharacters:characterSet] : @"";
|
||||
NSString *encodedContent = text ? [text stringByAddingPercentEncodingWithAllowedCharacters:characterSet] : @"";
|
||||
NSString *encodedComments = [comments stringByAddingPercentEncodingWithAllowedCharacters:characterSet];
|
||||
// NSInteger time = [[NSDate date] timeIntervalSince1970];
|
||||
NSURLSession *mySession = [self configureMySession];
|
||||
// NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://%@/api/add_site_load_script/%@/?url=%@&time=%@&comments=%@", host, token, encodedURL, @(time), encodedComments]];
|
||||
// NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://%@/api/share_story/%@", host, token]];
|
||||
NSURL *requestURL = [NSURL URLWithString:[NSString stringWithFormat:@"https://%@/api/share_story", host]];
|
||||
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:requestURL];
|
||||
request.HTTPMethod = @"POST";
|
||||
NSString *postBody = [NSString stringWithFormat:@"story_url=%@&title=&content=%@&comments=%@", encodedURL, encodedContent, encodedComments];
|
||||
request.HTTPBody = [postBody dataUsingEncoding:NSUTF8StringEncoding];
|
||||
NSURLSessionTask *myTask = [mySession dataTaskWithRequest:request];
|
||||
[myTask resume];
|
||||
}
|
||||
|
||||
- (NSURLSession *)configureMySession {
|
||||
|
|
Loading…
Add table
Reference in a new issue