Skip to content

Commit

Permalink
#1231 (sharing crash)
Browse files Browse the repository at this point in the history
- Now extracts the URL title and sets that when sharing, in addition to any user comments.
  • Loading branch information
Dejal authored and samuelclay committed Jun 16, 2020
1 parent 3f4c084 commit 599ad28
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion clients/ios/Share Extension/ShareViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

@interface ShareViewController () <NSURLSessionDelegate>

@property (nonatomic, strong) NSString *itemTitle;

@end

@implementation ShareViewController
Expand All @@ -26,6 +28,8 @@ - (BOOL)isContentValid {
}

- (void)didSelectPost {
self.itemTitle = nil;

NSItemProvider *itemProvider = [self providerWithURL];

NSLog(@"ShareExt: didSelectPost");
Expand Down Expand Up @@ -55,6 +59,12 @@ - (NSItemProvider *)providerWithURL {
for (NSExtensionItem *extensionItem in self.extensionContext.inputItems) {
for (NSItemProvider *itemProvider in extensionItem.attachments) {
if ([itemProvider hasItemConformingToTypeIdentifier:(NSString *)kUTTypeURL]) {
self.itemTitle = extensionItem.attributedTitle.string;

if (!self.itemTitle.length) {
self.itemTitle = extensionItem.attributedContentText.string;
}

return itemProvider;
}
}
Expand All @@ -79,14 +89,20 @@ - (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 *title = self.itemTitle;
NSString *comments = self.contentText;

if (title && [comments isEqualToString:title]) {
comments = @"";
}

if (text && [comments isEqualToString:text]) {
comments = @"";
}

NSCharacterSet *characterSet = [NSCharacterSet URLQueryAllowedCharacterSet];
NSString *encodedURL = url ? [url.absoluteString stringByAddingPercentEncodingWithAllowedCharacters:characterSet] : @"";
NSString *encodedTitle = title ? [title stringByAddingPercentEncodingWithAllowedCharacters:characterSet] : @"";
NSString *encodedContent = text ? [text stringByAddingPercentEncodingWithAllowedCharacters:characterSet] : @"";
NSString *encodedComments = [comments stringByAddingPercentEncodingWithAllowedCharacters:characterSet];
// NSInteger time = [[NSDate date] timeIntervalSince1970];
Expand All @@ -96,7 +112,7 @@ - (void)sendURL:(NSURL *)url orText:(NSString *)text {
NSURL *requestURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@/api/share_story/%@", host, token]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:requestURL];
request.HTTPMethod = @"POST";
NSString *postBody = [NSString stringWithFormat:@"story_url=%@&title=&content=%@&comments=%@", encodedURL, encodedContent, encodedComments];
NSString *postBody = [NSString stringWithFormat:@"story_url=%@&title=%@&content=%@&comments=%@", encodedURL, encodedTitle, encodedContent, encodedComments];
request.HTTPBody = [postBody dataUsingEncoding:NSUTF8StringEncoding];
NSURLSessionTask *myTask = [mySession dataTaskWithRequest:request];
[myTask resume];
Expand Down

0 comments on commit 599ad28

Please sign in to comment.