You've already forked hotpocket
BTHLABS-58: Share Extension in Apple Apps
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
//
|
||||
// HPShareExtensionHelper.h
|
||||
// HotPocket
|
||||
//
|
||||
// Created by Tomek Wójcik on 27/09/2025.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@class HPSharedItemsContainer;
|
||||
|
||||
typedef void (^HPShareExtensionHelperHandleItemsCompletionHandler)(NSURL * _Nullable url);
|
||||
|
||||
@interface HPShareExtensionHelper : NSObject
|
||||
|
||||
@property NSExtensionContext *context;
|
||||
@property HPSharedItemsContainer *items;
|
||||
|
||||
-(instancetype)initWithContext:(NSExtensionContext *)context;
|
||||
|
||||
-(void)processItems:(HPShareExtensionHelperHandleItemsCompletionHandler)completionHandler;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
102
services/apple/Shared (Share Extension)/HPShareExtensionHelper.m
Normal file
102
services/apple/Shared (Share Extension)/HPShareExtensionHelper.m
Normal file
@@ -0,0 +1,102 @@
|
||||
//
|
||||
// HPShareExtensionHelper.m
|
||||
// HotPocket
|
||||
//
|
||||
// Created by Tomek Wójcik on 27/09/2025.
|
||||
//
|
||||
|
||||
#import <UniformTypeIdentifiers/UniformTypeIdentifiers.h>
|
||||
|
||||
#import "HPShareExtensionHelper.h"
|
||||
|
||||
#import "HPSharedItem.h"
|
||||
#import "HPSharedItemsContainer.h"
|
||||
|
||||
@implementation HPShareExtensionHelper (HPShareExtensionHelperPrivate)
|
||||
|
||||
@end
|
||||
|
||||
@implementation HPShareExtensionHelper
|
||||
|
||||
-(instancetype)initWithContext:(NSExtensionContext *)context {
|
||||
if (self = [super init]) {
|
||||
self.context = context;
|
||||
self.items = [[HPSharedItemsContainer alloc] init];
|
||||
}
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
-(void)processItems:(HPShareExtensionHelperHandleItemsCompletionHandler)completionHandler {
|
||||
// Depending on the app, the URL might be stored in `public.url` attachment or elsewhere.
|
||||
// For example, the YouTube app passes it in `public.plain-text`. Because of course it does.
|
||||
// Furthermore, for some bizarre reason the recommended way of extracting the URL when sharing from a browser
|
||||
// is to run a JS snippet and examine its output.
|
||||
// This method will iterate through all the shared items and their attachments and attempt to extract
|
||||
// the URL candidates.
|
||||
//
|
||||
// Also note that handler for `public.url` explicitly requests the payload to be corced to `NSURL *`. Leaving it
|
||||
// at `NSData *` would cause iOS to, wait for it!, fetch the URL and dump the response body in the payload :D.
|
||||
//
|
||||
// This is so _so_ *so* dumb. But hey, at least I learned how to to "chords" in CGD ¯\_(ツ)_/¯
|
||||
UTType *propertyListType = [UTType typeWithFilenameExtension:@"plist"];
|
||||
|
||||
dispatch_group_t dispatchGroup = dispatch_group_create();
|
||||
dispatch_queue_t queue = dispatch_queue_create("HPShareExtensionHelper.processItems.queue", DISPATCH_QUEUE_SERIAL);
|
||||
|
||||
for (NSExtensionItem *inputItem in self.context.inputItems) {
|
||||
#ifdef DEBUG
|
||||
NSLog(@"-[HPShareExtensionHelper processItems:] inputItem.userInfo=`%@`", inputItem);
|
||||
#endif
|
||||
[inputItem.attachments enumerateObjectsUsingBlock:^(NSItemProvider *attachment, NSUInteger index, BOOL *stop) {
|
||||
dispatch_group_enter(dispatchGroup);
|
||||
|
||||
if ([attachment hasItemConformingToTypeIdentifier:propertyListType.identifier] == YES) {
|
||||
[attachment loadItemForTypeIdentifier:propertyListType.identifier
|
||||
options:nil
|
||||
completionHandler:^(NSDictionary *payload, NSError *error) {
|
||||
dispatch_async(queue, ^{
|
||||
self.items.primaryItem = [[HPSharedItem alloc] initWithPayload:payload
|
||||
typeIdentifier:propertyListType.identifier
|
||||
error:error];
|
||||
|
||||
dispatch_group_leave(dispatchGroup);
|
||||
});
|
||||
}];
|
||||
} else if ([attachment hasItemConformingToTypeIdentifier:@"public.url"] == YES) {
|
||||
[attachment loadItemForTypeIdentifier:@"public.url"
|
||||
options:nil
|
||||
completionHandler:^(NSURL *payload, NSError *error) {
|
||||
dispatch_async(queue, ^{
|
||||
[self.items.candidateItems addObject:[[HPSharedItem alloc] initWithPayload:payload
|
||||
typeIdentifier:@"public.url"
|
||||
error:error]];
|
||||
|
||||
dispatch_group_leave(dispatchGroup);
|
||||
});
|
||||
}];
|
||||
} else if ([attachment hasItemConformingToTypeIdentifier:@"public.plain-text"] == YES) {
|
||||
[attachment loadItemForTypeIdentifier:@"public.plain-text"
|
||||
options:nil
|
||||
completionHandler:^(NSString *payload, NSError *error) {
|
||||
dispatch_async(queue, ^{
|
||||
[self.items.candidateItems addObject:[[HPSharedItem alloc] initWithPayload:payload
|
||||
typeIdentifier:@"public.plain-text"
|
||||
error:error]];
|
||||
|
||||
dispatch_group_leave(dispatchGroup);
|
||||
});
|
||||
}];
|
||||
} else {
|
||||
dispatch_group_leave(dispatchGroup);
|
||||
}
|
||||
}];
|
||||
|
||||
dispatch_group_notify(dispatchGroup, dispatch_get_main_queue(), ^{
|
||||
NSURL *result = [self.items resolveURL];
|
||||
completionHandler(result);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@end
|
||||
24
services/apple/Shared (Share Extension)/HPSharedItem.h
Normal file
24
services/apple/Shared (Share Extension)/HPSharedItem.h
Normal file
@@ -0,0 +1,24 @@
|
||||
//
|
||||
// HPSharedItem.h
|
||||
// HotPocket
|
||||
//
|
||||
// Created by Tomek Wójcik on 27/09/2025.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface HPSharedItem : NSObject
|
||||
|
||||
@property (nullable) id payload;
|
||||
@property NSString *typeIdentifier;
|
||||
@property (nullable) NSError *error;
|
||||
|
||||
-(instancetype)initWithPayload:(nullable id)payload typeIdentifier:(NSString *)typeIdentifier error:(nullable NSError *)error;
|
||||
|
||||
-(NSURL *)maybeURL;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
47
services/apple/Shared (Share Extension)/HPSharedItem.m
Normal file
47
services/apple/Shared (Share Extension)/HPSharedItem.m
Normal file
@@ -0,0 +1,47 @@
|
||||
//
|
||||
// HPSharedItem.m
|
||||
// HotPocket
|
||||
//
|
||||
// Created by Tomek Wójcik on 27/09/2025.
|
||||
//
|
||||
|
||||
#import <UniformTypeIdentifiers/UniformTypeIdentifiers.h>
|
||||
|
||||
#import "HPSharedItem.h"
|
||||
|
||||
@implementation HPSharedItem
|
||||
|
||||
-(instancetype)initWithPayload:(id)payload typeIdentifier:(NSString *)typeIdentifier error:(NSError *)error {
|
||||
if (self = [super init]) {
|
||||
self.payload = payload;
|
||||
self.typeIdentifier = typeIdentifier;
|
||||
self.error = error;
|
||||
}
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
-(NSURL *)maybeURL {
|
||||
if (self.error != nil) {
|
||||
return nil;
|
||||
}
|
||||
|
||||
if ([self.typeIdentifier isEqualToString:[UTType typeWithFilenameExtension:@"plist"].identifier] == YES) {
|
||||
NSDictionary *propertyList = self.payload;
|
||||
NSDictionary *jsHelperResult = [propertyList valueForKey:NSExtensionJavaScriptPreprocessingResultsKey];
|
||||
|
||||
if ([jsHelperResult valueForKey:@"iHateComputers"] == nil) {
|
||||
return nil;
|
||||
}
|
||||
|
||||
return [NSURL URLWithString:[jsHelperResult valueForKey:@"url"]];
|
||||
} else if ([self.typeIdentifier isEqualToString:@"public.url"] == YES) {
|
||||
return self.payload;
|
||||
} if ([self.typeIdentifier isEqualToString:@"public.plain-text"] == YES) {
|
||||
return [NSURL URLWithString:self.payload];
|
||||
}
|
||||
|
||||
return nil;
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,23 @@
|
||||
//
|
||||
// HPSharedItemsContainer.h
|
||||
// HotPocket
|
||||
//
|
||||
// Created by Tomek Wójcik on 27/09/2025.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@class HPSharedItem;
|
||||
|
||||
@interface HPSharedItemsContainer : NSObject
|
||||
|
||||
@property (nullable) HPSharedItem *primaryItem;
|
||||
@property NSMutableArray<HPSharedItem *> *candidateItems;
|
||||
|
||||
-(NSURL *)resolveURL;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
@@ -0,0 +1,63 @@
|
||||
//
|
||||
// HPSharedItemsContainer.m
|
||||
// HotPocket
|
||||
//
|
||||
// Created by Tomek Wójcik on 27/09/2025.
|
||||
//
|
||||
|
||||
#import "HPSharedItemsContainer.h"
|
||||
|
||||
#import "HPSharedItem.h"
|
||||
#import "NSURL+HotPocketExtensions.h"
|
||||
|
||||
@implementation HPSharedItemsContainer (HPSharedItemsContainerPrivate)
|
||||
|
||||
-(NSURL *)validatedURL:(NSURL *)url {
|
||||
if (url.isUsableInHotPocket == NO) {
|
||||
return nil;
|
||||
}
|
||||
|
||||
return url;
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@implementation HPSharedItemsContainer
|
||||
|
||||
-(instancetype)init {
|
||||
if (self = [super init]) {
|
||||
self.primaryItem = nil;
|
||||
self.candidateItems = [[NSMutableArray alloc] initWithCapacity:1];
|
||||
}
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
-(NSURL *)resolveURL {
|
||||
NSURL *result = nil;
|
||||
|
||||
if (self.primaryItem != nil) {
|
||||
result = [self validatedURL:[self.primaryItem maybeURL]];
|
||||
}
|
||||
|
||||
if ([self.candidateItems count] > 0) {
|
||||
NSUInteger itemCandidateIndex = 0;
|
||||
while (result == nil) {
|
||||
HPSharedItem *itemCandidate = [self.candidateItems objectAtIndex:itemCandidateIndex];
|
||||
|
||||
result = [self validatedURL:itemCandidate.maybeURL];
|
||||
if (result != nil) {
|
||||
break;
|
||||
}
|
||||
|
||||
itemCandidateIndex += 1;
|
||||
if (itemCandidateIndex >= [self.candidateItems count]) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@end
|
||||
Reference in New Issue
Block a user