make the share extension more robust by looking for the first URL provider

this makes sharing from Unread work
This commit is contained in:
Sami Samhuri 2015-05-11 22:11:34 -07:00
parent e5e86193b7
commit 171a262a6e

View file

@ -13,13 +13,17 @@
@interface ShareViewController () @interface ShareViewController ()
@property (nonatomic, readonly, strong) NSItemProvider *URLProvider;
@property (nonatomic, assign) BOOL checkedForURLProvider;
@end @end
@implementation ShareViewController @implementation ShareViewController
@synthesize URLProvider = _URLProvider;
- (BOOL)isContentValid { - (BOOL)isContentValid {
// Do validation of contentText and/or NSExtensionContext attachments here return self.URLProvider != nil;
return YES;
} }
- (UIView *)loadPreviewView { - (UIView *)loadPreviewView {
@ -34,22 +38,50 @@
NSRange titleEndRange = [self.contentText rangeOfString:@"\n\n"]; NSRange titleEndRange = [self.contentText rangeOfString:@"\n\n"];
NSString *title = titleEndRange.location == NSNotFound ? self.contentText : [self.contentText substringToIndex:titleEndRange.location]; NSString *title = titleEndRange.location == NSNotFound ? self.contentText : [self.contentText substringToIndex:titleEndRange.location];
NSString *body = titleEndRange.location == NSNotFound ? @"" : [self.contentText substringFromIndex:titleEndRange.location + titleEndRange.length]; NSString *body = titleEndRange.location == NSNotFound ? @"" : [self.contentText substringFromIndex:titleEndRange.location + titleEndRange.length];
NSExtensionItem *item = self.extensionContext.inputItems.firstObject; NSLog(@"title = %@", title);
NSItemProvider *provider = item.attachments.firstObject; NSLog(@"body = %@", body);
[provider loadItemForTypeIdentifier:@"public.url" options:nil completionHandler:^(NSURL *url, NSError *error) { NSItemProvider *urlProvider = [self firstURLProvider];
BOOL reallyPost = NO;
[urlProvider loadItemForTypeIdentifier:@"public.url" options:nil completionHandler:^(NSURL *url, NSError *error) {
// TODO: image // TODO: image
NSLog(@"url = %@", url);
if (reallyPost) {
Post *post = [Post newDraftWithTitle:title body:body url:url]; Post *post = [Post newDraftWithTitle:title body:body url:url];
[blogController requestCreateDraft:post publishImmediatelyToEnvironment:@"production" waitForCompilation:NO].catch(^(NSError *error) { [blogController requestCreateDraft:post publishImmediatelyToEnvironment:@"staging" waitForCompilation:NO].catch(^(NSError *error) {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Error" message:error.localizedDescription preferredStyle:UIAlertControllerStyleAlert]; UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Error" message:error.localizedDescription preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"Dismiss" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { [alert addAction:[UIAlertAction actionWithTitle:@"Dismiss" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[self dismissViewControllerAnimated:YES completion:nil]; [self dismissViewControllerAnimated:YES completion:nil];
}]]; }]];
[self presentViewController:alert animated:YES completion:nil]; [self presentViewController:alert animated:YES completion:nil];
}); });
}
[self.extensionContext completeRequestReturningItems:@[] completionHandler:nil]; [self.extensionContext completeRequestReturningItems:@[] completionHandler:nil];
}]; }];
} }
- (NSItemProvider *)firstURLProvider {
NSExtensionItem *item = self.extensionContext.inputItems.firstObject;
NSLog(@"item = %@", item);
for (NSItemProvider *provider in item.attachments) {
NSLog(@"provider = %@", provider);
if ([provider hasItemConformingToTypeIdentifier:@"public.url"]) {
return provider;
}
}
return nil;
}
- (NSItemProvider *)URLProvider {
if (!self.checkedForURLProvider) {
_URLProvider = [self firstURLProvider];
if (!_URLProvider) {
NSLog(@"ERROR: No URL provider found");
}
self.checkedForURLProvider = YES;
}
return _URLProvider;
}
- (NSArray *)configurationItems { - (NSArray *)configurationItems {
// To add configuration options via table cells at the bottom of the sheet, return an array of SLComposeSheetConfigurationItem here. // To add configuration options via table cells at the bottom of the sheet, return an array of SLComposeSheetConfigurationItem here.
return @[]; return @[];