How to detect an app extension is enabled in conta

2020-07-06 07:23发布

问题:

I am developing a custom keyboard on iOS 8 beta, and I want to tell the user that how to enabled it in containing app if my custom keyboard is not enabled, is there any way to detect an app extension is enabled ?

回答1:

  1. first of all let's set some constants to make it easy to understand each other:

    • containing app = the app that installs the extension and holds the extension binary and target
    • host app = the app that the extension is running inside (other party)
    • extension = any of iOS8's new components/modules that we can now build into system-wide use: custom keyboards, today widgets, photo editing effects, and more..
  2. Apple also released a more quiet API called App Groups API This API allows a developer to group n extensions under 1 bundle identifier, and creates a communication wire between the app and the extensions contained in it.

  3. you can share data between the extensions and the containing app using NUserDefaults, but with this new method:

    [[NSUserDefaults alloc] initWithSuiteName:@"<app group identifier>"];
    

    read/write... and sync:

    [myDefaultsObj synchronize];
    
  4. and now to the bottom line:

    use the app group's url schemes to test what you want:

    https://developer.apple.com/library/prerelease/ios/documentation/Foundation/Reference/NSExtensionContext_Class/#//apple_ref/occ/instm/NSExtensionContext/openURL:completionHandler:

    - (void)openURL:(NSURL *)URL completionHandler:(void (^)(BOOL success))completionHandler
    
    • URL - The URL to open.
    • completionHandler - A block that is called when the URL has opened.
    • this parameter - success - is a Boolean value that indicates whether the open was successful.

Good luck!!!