Swift-based iOS App Extension using NSExtension

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Swift-based iOS App Extension using NSExtension



I am experimenting with daemon processes on iOS using the NSExtension private API.



I have been following Ian McDowell's article Multi-Process iOS App Using NSExtension. A sample project is available for download at the end of the article.



My goal is to port this sample project from Objective-C to Swift. It has been simple enough porting the AppDelegate and ViewController to swift for the host application. XCode 9.2 seems to map the NSExtension method declarations in PrivateHeaders.h to Swift for use.



PrivateHeaders.h


@interface NSExtension : NSObject

+ (instancetype)extensionWithIdentifier:(NSString *)identifier error:(NSError **)error;
- (void)beginExtensionRequestWithInputItems:(NSArray *)inputItems completion:(void (^)(NSUUID *requestIdentifier))completion;
- (int)pidForRequestIdentifier:(NSUUID *)requestIdentifier;
- (void)cancelExtensionRequestWithIdentifier:(NSUUID *)requestIdentifier;
- (void)setRequestCancellationBlock:(void (^)(NSUUID *uuid, NSError *error))cancellationBlock;
- (void)setRequestCompletionBlock:(void (^)(NSUUID *uuid, NSArray *extensionItems))completionBlock;
- (void)setRequestInterruptionBlock:(void (^)(NSUUID *uuid))interruptionBlock;

@end



I run into trouble when trying to port the application extension class from Objective-C to Swift. Below is a simplification of the original class and my port:



Extension.h


#import <Foundation/Foundation.h>

@interface Extension : NSObject <NSExtensionRequestHandling>

@end



Extension.m


#import "Extension.h"

@implementation Extension

- (void)beginRequestWithExtensionContext:(NSExtensionContext *)context
NSLog(@"Beginning request with context: %@", [context description]);


@end



Extension.swift


import Foundation

class Extension: NSObject, NSExtensionRequestHandling
func beginRequest(with context: NSExtensionContext)
print("Beginning request with context: (context.description)")




Attempting to launch the Swift Extension process result in the following error in XCode:


Extension



Unable to setup extension context - error: Couldn't communicate with a helper application.



Inspecting the iPhone log via Apple Configurator 2 reveals the following error:



iPhone Extension(CoreFoundation)[366] : *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** - [__NSDictionaryM setObject:forKey:]: object cannot be nil



Is anyone able to provide any additional insight into what might be going wrong here? The swap from Objective-C to Swift seems to be straightforward. I cannot even hit a breakpoint on the Extension application before it seems to crash.



Note: I understand that this likely wouldn't make it past app store review. It will never be going near the app store.





Update for anyone playing along at home. I didn't solve this, but keeping 'Extension' class as Objective-C running Swift code from there works okay.
– Jordan Johnson
Sep 20 at 6:30









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard