On iOS 8 we have App Extension, which extends app functionality beyond original app. It allows people to use some part when they run other apps.

Some nice App Extensions: Custom Keyboard, Today Widget.

Extension Types (8)

At the moment, there are 8 extension points (devs have to choose which extension point to use:

Screen Shot 2015-06-17 at 2.27.50 pm

Development

Each app extension is a separate binary -> we add new Target to an app. (Should use the template that Xcode provides for each Extension point, the binary produced by these templates will get added to the containing app). If you install the app -> you install all extensions.

User need to enable these Extensions: can be Notification Center (Today), Settings /SystemPreferences(OS X) for other.

Life Cycle:

  1. The user choose an extension (like Share, then choose the App Extension in the Share list).
  2. System run chosen Extension, display it’s view in the context of the host app.
  3. User perform something. Extension can run or schedule background task if needed.
  4. Extension terminated.

Communication

App Extension can run alone, without the Containing App running. (Most of the time the containing app isn’t even running).

App Extension communicates directly with Host app. (It can talk with the Containing app to ask for something if needed, using openURL:completionHandler: of NSExtensionContext class).

All Extensions and the containing app can access shared data in a shared container (within the containing app sandbox).

Shared Resource can be accessed by containing app and any extension:

Today Widget

Today App Extensions are called widgets. Widget provides quick look at the important, current information !!! Actions can be performed on the widgets (but limited and quick).

Widget’ standard template contains:

  1. TodayViewController
  2. Storyboard
  3. Info.plist

System occasionally captures snapshot of the widget view -> displays snapshot first while the current view get updated.

Notification Center Framework is used for Today Widget:

  1. Class: NCWidgetController
  2. Protocol: NCWidgetProviding

WWDC 14 (205) Creating Extension for iOS and OS X

WWDC 14 has a video (205) introducing us how to create Extension.

  1. Extensions & Extension Points
    1. What are Extensions? What you can do more with your apps, outside of the apps, inside another app…
    2. Notification Extension: Today Widget – quick glance at the information you need. Also on OS X – right-top corner! 
    3. Share Extension, Markup Extension (OSX), Photo Extension (iOS), Document Provider, Finder Extension (OSX), Keyboard Extension (iOS)
    4. Delivery: part of your app (Extension Container). 
    5. Extension Points: not apps, purpose-built binaries, accessed via Apple frameworks code (you never launch it directly, but the system discovers it and run for you), packaged in System Frameworks
    6. New process will be launched for each extension used by different host-app (Share Ext in Safari will differ from same Share Ext in Mail). Safari is not directly launching Extension -> Apple frameworks do it. 
  2. Today Widgets – Notification Center Extensions (Matt Gamble iOS Notifications Engineer)
    1. Widgets are View Controllers
    2. Appearance: viewWill/DidAppear:(BOOL)animated, viewWill/DidDisappear
    3. Performance: 
      1. load cached data !
      2. kick off expensive operations early and in background
      3. cache the data (so can show next time)
    4. Handling Layout
      1. Notification Center sets the frames!
      2. Height: Autolayout or setPreferredContentSize
      3. Animation for height changes: 
        viewWillTransitionToSize:(CGSize) withTransitionCoordinator:(id )coordinator {
            [coordinator animateAlongsideTransitions: ^(id context) {
            [view setAlpha:0.0] } completion: ^(id context) { [view removeFromSuperview]; } ];
        }
        
    5. iOS8 : NCWidgetProviding protocol – (void)widgetPerformUpdateWithCompletionHandler: (^)(NCUpdateResult)completion // pass the result to completion block. 
  3. Today Sample Code:
    1. Add New Target (Today Extension)
    2. Ensure Widget has right Height: 
      1. inside -(void)awakeFromNib, calculate how many items we want to show
      2. setPreferredContentSize:[[self tableView] contentSize]
    3. Add ShowMore button & action: 
      1. get prefH = 
      2. [self setPreferredContentSize: CGSizeMake(0.0, prefH)];
      3. [self setUpdateRequired: YES];
    4. Animation: viewWillTransitionToSize -> updateTableAnimated:YES & then change MoreButton title
    5. Updated content: widgetPerformUpdateWithCompletionHandler 
      1. if (result == NCUpdateResultNewData) { self updateTableAnimated, setPreferredContentSize} …
    6. Open to the App with URL Scheme: 
      1. use tableView:didSelectRowAtIndexPath: 
      2. get the row object by the indexPath
      3. [self extensionContext] openURL:[NSURL URLWithString:….] // use the URL Scheme here
  4. Share Extension: share some content to our app’s site
    1. Display Name: CFBundleDisplayName
    2. Activation Rules: video? image?
      1. NSExtensionActivationRule predicate
      2. NSExtensionActivationRule specified condense rule
    3. Implement a ViewController 
      1. UI/NS ViewController
      2. SLComposeServiceViewController
    4. Upload: NSURLSession 
      1. use backgroundSession
Advertisements