iOS Developer Library — Pre-Release

Developer

Foundation Framework Reference NSExtensionContext Class Reference

Options
Deployment Target:

On This Page
Language:

NSExtensionContext

An NSExtensionContext object represents the host app context from which an app extension is invoked. More...

Inheritance


Conforms To


Import Statement


Swift

import Foundation

Objective-C

@import Foundation;

Availability


Available in iOS 8.0 and later.
  • Tells the host app to cancel the app extension request, with a supplied error.

    Declaration

    Swift

    func cancelRequestWithError(_ error: NSError)

    Objective-C

    - (void)cancelRequestWithError:(NSError *)error

    Parameters

    error

    The error object to return. It must be non-nil.

    Discussion

    On return, the userInfo dictionary of the NSError object contains a key named NSExtensionItemsAndErrorsKey which has as its value a dictionary of NSExtensionItem objects and associated NSError instances.

    Import Statement

    Swift

    import Foundation

    Availability

    Available in iOS 8.0 and later.

  • Tells the host app to complete the app extension request with an array of result items.

    Declaration

    Swift

    func completeRequestReturningItems(_ items: [AnyObject]?, completionHandler completionHandler: ((Bool) -> Void)?)

    Objective-C

    - (void)completeRequestReturningItems:(NSArray *)items completionHandler:(void (^)(BOOL expired))completionHandler

    Parameters

    items

    An array of result items, each an NSExtensionItem object, to return to the host app.

    completionHandler

    An optional block to be called when the request completes, performed as a background priority task.

    The block takes the following parameter:

    expired

    A Boolean value that indicates whether the system is terminating a previous invocation of the completionHandler block.

    This parameter is YEStrue when the system prematurely terminates a completionHandler block that was previously invoked and had not otherwise expired.

    Discussion

    Calling this method eventually dismisses the app extension’s view controller.

    Import Statement

    Swift

    import Foundation

    Availability

    Available in iOS 8.0 and later.

  • Asks the system open a URL on behalf of the currently running app extension.

    Declaration

    Swift

    func openURL(_ URL: NSURL, completionHandler completionHandler: ((Bool) -> Void)?)

    Objective-C

    - (void)openURL:(NSURL *)URL completionHandler:(void (^)(BOOL success))completionHandler

    Parameters

    URL

    The URL to open.

    completionHandler

    A block to be called when the URL has opened.

    The block takes the following parameter:

    success

    A Boolean value that indicates whether the open operation was successful.

    Discussion

    Each extension point determines whether to support this method, or under which conditions to support this method. In iOS 8, only the Today extension point (used for creating Today widgets) supports this method.

    Import Statement

    Swift

    import Foundation

    Availability

    Available in iOS 8.0 and later.

  • The list of input NSExtensionItem objects associated with the context. (read-only)

    Declaration

    Swift

    var inputItems: [AnyObject] { get }

    Objective-C

    @property(readonly, copy, nonatomic) NSArray *inputItems

    Discussion

    If the context has no input items, this array is empty.

    Import Statement

    Swift

    import Foundation

    Availability

    Available in iOS 8.0 and later.

  • This key is used in the userInfo dictionary of the NSError object returned by cancelRequestWithError:.

    Declaration

    Swift

    let NSExtensionItemsAndErrorsKey: String

    Objective-C

    NSString *const NSExtensionItemsAndErrorsKey;

    Constants

    • NSExtensionItemsAndErrorsKey

      NSExtensionItemsAndErrorsKey

      A key whose value is a dictionary of NSExtensionItem objects and associated NSError instances.

      Available in iOS 8.0 and later.