Skip to content

Instantly share code, notes, and snippets.

@sportnak
Last active October 8, 2017 23:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sportnak/93f4909ac1adbcae98219108141bfb98 to your computer and use it in GitHub Desktop.
Save sportnak/93f4909ac1adbcae98219108141bfb98 to your computer and use it in GitHub Desktop.
Context acquisition
- (void)loadView {
[super loadView];
//this is to retrieve from the app group
NSUserDefaults *bucket = [[NSUserDefaults alloc] initWithSuiteName:appGroupName];
// assign your member values
[self loadContext];
}
- (void)loadContext
{
NSExtensionContext *context = self.extensionContext;
NSExtensionItem *item = [context.inputItems firstObject];
m_contextText = self.contentText;
@try
{
NSArray *attachments = item.attachments;
[attachments enumerateObjectsUsingBlock:^(NSItemProvider *provider, NSUInteger idx, BOOL *stop)
{
if ([provider hasItemConformingToTypeIdentifier:URL_IDENTIFIER])
{
[provider
loadItemForTypeIdentifier:URL_IDENTIFIER
options:nil
completionHandler: ^(NSURL *item, NSError *error) {
m_contextUrl = item;
}];
}
else if ([provider hasItemConformingToTypeIdentifier:IMAGE_IDENTIFIER])
{
[provider
loadItemForTypeIdentifier:IMAGE_IDENTIFIER
options:nil
completionHandler: ^(id<NSSecureCoding> item, NSError *error) {
NSData *imgData;
if([(NSObject*)item isKindOfClass:[NSURL class]])
imgData = [NSData dataWithContentsOfURL:(NSURL*)item];
if([(NSObject*)item isKindOfClass:[UIImage class]])
imgData = UIImagePNGRepresentation((UIImage*)item);
[m_contextImgData addObject:imgData];
}];
}
else if ([provider hasItemConformingToTypeIdentifier:(NSString *)kUTTypePlainText])
{
[provider
loadItemForTypeIdentifier:(NSString *)kUTTypePlainText
options:nil
completionHandler: ^(id<NSSecureCoding> item, NSError *error) {
m_contextText = (NSString *)item;
}];
}
}
];
}
@catch (NSException *exception)
{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment