IOS9-by-Tutorials-Study Notes 4: APP-slimming

Source: Internet
Author: User

IOS9-by-Tutorials-Study Notes 4: APP-slimming
IOS9-by-Tutorials-Study Notes 4: APP-slimming

The title of this article in the book is App Thinning. Here I translate it into App slimming.

This article has made some modifications to the syntax, so she is very happy to modify these things for me. She said that I will only use the turning point. However, if she says so, it will be used. However, hey.

After several years of development, the iPhone has undergone great changes, such as more diverse screens, larger sizes, larger memory, and changing CPU architecture. As the iPhone changes, iOS is also changing, such as AutoLayout, size classes, and split view controller. The changes in these technologies and devices have also caused many problems in the development process. Not only in this way, Apple is constantly introducing new technologies, we strive to help us use the same set of code to develop a Universal App that adapts to multiple devices. On the other hand, although the Universal App facilitates our developers in the development process, it is not so friendly to users. Because it needs to adapt to multiple devices, it contains the code of all devices, but when we are running, we don't need so much related code and resources.

For example, the following figure shows the various resources used by an App running on iPhone 6 +:

The resource and code used during actual running on the iPhone 6 + are checked, more is not checked out. We can imagine that we downloaded an App (provided that the App was a Universal App), and at least half of the code and resources were not required by us, occupying our space in vain. This is not good for user experience. To solve this problem, Apple provided a new solution on iOS 9:

App Slicing when you submit your iOS 9 package file to App Store, Apple compiles your resources and executable files and generates a specific executable file for each device. In the end, the device only downloads the content adapted to its features and used. These features include graphics card performance (original words: graphics capabilities), Memory Level, CPU architecture, size classes, and screen scaling. Resources of the On Demand Resouces application can be downloaded only when necessary, and can be removed if other resources need space. When you submit an App to an App Store, Bitcode can be submitted together as an intermediate product. Programs that contain bitcode configurations are compiled and linked to the App store. Bitcode allows Apple to re-optimize the binary files of our program in the future without submitting a new version to the App store.

These three technologies are collectively called App Thinning.

Getting started

Open the initial project in this chapter and select "run on iPad Air 2". The running effect is as follows:

When the simulator is started, a Finder window is opened:

This Finder window can be opened because a script is added to the program and will be executed every time it is run. The script is located as follows:

echo "App Size in KB:  `du -sk \"${CONFIGURATION_BUILD_DIR}/${EXECUTABLE_NAME}.app\"`"if [ "${CONFIGURATION}" = "Debug" ]; thenopen ${CONFIGURATION_BUILD_DIR}fi

Right-click the Finder's Old CA Maps and choose show Package content, as shown below:

Note:
1. Assets. car is the file after Assets. xcassets are compiled by Xcode.
2. The Old CA Maps are executable files that actually run on the device.
3. santa Cruz PNGs: this is an image file, but it is not compiled into Assets. the car file, because it is not placed in Assets. xcassets is stored in the top-level file of the project.
4. SD_Map.bundle: this is the map image file, but it is nearly 120 MB.

Measuring your work

This chapter describes some things related to App slimming, so we must be able to measure whether the App is reduced. A script (included in the Code above) has been built in the project to output the App size during the build process. The location is as follows:

Slicing up app slicing

App slicing contains two parts: Executable slicing and resource slicing ).

Executable slicingWhen a device downloads an App, it only downloads executable files related to the device based on the device information, and does not contain executable files of other devices and architectures, to narrow down the App installation package. And this function does not need to be done too much. App Store supports this function by default.

By default, packages submitted to the App Store contain all the content, which are in the configuration file. The App Store automatically creates executable files corresponding to each type. This is supported on iOS9 +.

Being smart with resources

Resource slicingWe need a small amount of simple work to achieve it. If Resource slicing is used, make sure that all our resources are managed by Asset Catalogs. In Xcode 7MemoryAndGraphics, As follows:

Your first fix

At the beginning, I introduced the file Santa Cruz PNGs which was put in Main bundle, so it cannot be compiled into Assets. car, and Resource slicing cannot be used. Next, let's take a look at how we modify it to make it usable:

After selecting New Image Set, name the newly added set as Santa Cruz, and then perform the following operations:

Correct that the content on the left should be deleted, including in the Finder. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Authorization/WQXNzZXQuY2FyzsS8/authorization = "lazily-downloading-content"> Lazily (down) loading content

Apple provides the On-Demand Resources technology, or ODR for short. ODR allows you to store resources on Apple's servers and then download them when your App is using them. NSBundleResourceRequest is a class for processing ODR. This class can be used to download corresponding resources through tags. ODR can be used for images, data, OpenGL shaders, SpriteKit participant, and Watchkit Complications.

Wire things up to use tags

The following code is modified to download the resource. The method for modifying MapChromeViewController. swift is as follows:

  private func downloadAndDisplayMapOverlay() {//    displayOverlayFromBundle(NSBundle.mainBundle())    guard let bundleTitle = mapOverlayData?.bundleTitle else {      return    }    let bundleResource = NSBundleResourceRequest(tags: [bundleTitle])    bundleResource.beginAccessingResourcesWithCompletionHandler { [weak self] (error) -> Void in      NSOperationQueue.mainQueue().addOperationWithBlock({ () -> Void in        if error == nil {          self?.displayOverlayFromBundle(bundleResource.bundle)        }      })    }  }

At this time, we run the Code and may output an error in the console. This is because the corresponding bundle does not have a tag. We need to add a tag to the bundle:

Then we re-compile and run our program, and then check the size of the program compiled and run according to the above, and found that it is much smaller. Compare the files generated by the previous compilation and find that the running file does not contain bundle.

If your App is in the App Store, the download of this resource file may be slow. However, during the development process, Xcode uses the local network as the server and can be downloaded from the device. Therefore, if the computer is disabled during the development process, then the ODR cannot be used.

Make it download faster

In the process of using ODR, if the bundle is large, it may be time-consuming to download again, and the user does not know during the download process, so that the user experience is poor. We can give the user some tips during the Resource download process and modify the following code:

// Add the newly added ProgressView is the private func downloadAndDisplayMapOverlay () {// displayOverlayFromBundle (NSBundle. mainBundle () guard let bundleTitle = mapOverlayData ?. BundleTitle else {return} let bundleResource = NSBundleResourceRequest (tags: [bundleTitle]) bundleResource. loadingPriority = NSBundleResourceRequestLoadingPriorityUrgent // add loadingProgressView. observedProgress = bundleResource. progress // add loadingProgressView. hidden = false // add UIApplication. sharedApplication (). networkActivityIndicatorVisible = true // add bundleResource. beginAccessingRe SourcesWithCompletionHandler {[weak self] (error)-> Void in NSOperationQueue. mainQueue (). addOperationWithBlock ({()-> Void in self ?. LoadingProgressView. hidden = true // add UIApplication. sharedApplication (). networkActivityIndicatorVisible = false // add if error = nil {self ?. DisplayOverlayFromBundle (bundleResource. bundle )}})}}

If you have already downloaded a bundle, you will not download it again the next time you use it.

The specified flavors of tagging

Although the ProgressView is added, the experience is better, but you need to note that the test is based on the local network, so it is faster, but if you submit it to the App Store, the download may be slow. If the user does not have WiFi, it may not work, so we need to make other adjustments.

Initial install tags

With Initial install tags, we can set which bundle will be downloaded during App initialization and installation. The following describes how to download ODR:
*Initial Install TagsDownload together during ipa download
*Prefetched Tag OrderAfter the program is downloaded, download the corresponding resources and arrange them in sequence.
*Prefetched Tag OrderDownload as needed
The configuration is as follows:

Purging content

The application downloads the corresponding bundle through ODR during usage, but sometimes we need to clear some bundle that has been downloaded and not used. Before introducing how to delete an object, let's take a look at how to view the downloaded ODR:

Set a resource to be purged

Add the following code in MapChromeViewController. swift:

// New add is the newly added code var overlayBundleResource: NSBundleResourceRequest? // New add private func downloadAndDisplayMapOverlay () {// displayOverlayFromBundle (NSBundle. mainBundle () guard let bundleTitle = mapOverlayData ?. BundleTitle else {return} let bundleResource = NSBundleResourceRequest (tags: [bundleTitle]) overlayBundleResource = bundleResource // new add bundleResource. loadingPriority = NSBundleResourceRequestLoadingPriorityUrgent // add loadingProgressView. observedProgress = bundleResource. progress // add loadingProgressView. hidden = false // add UIApplication. sharedApplication (). networkActivityIndicatorVis Visible = true // add bundleResource. Handle {[weak self] (error)-> Void in NSOperationQueue. mainQueue (). addOperationWithBlock ({()-> Void in self ?. LoadingProgressView. hidden = true // add UIApplication. sharedApplication (). networkActivityIndicatorVisible = false // add if error = nil {self ?. DisplayOverlayFromBundle (bundleResource. bundle) }}}// new add override func viewDidDisappear (animated: Bool) {super. viewDidDisappear (animated) // tells the system that resource access overlayBundleResource is terminated ?. EndAccessingResources ()}

The above code, I do not know when it will be deleted during the test, I also simulated the memory warning, if anyone knows, please tell me, thank you.

I have finished writing this note for several days at noon. It is very tiring to take 13 notes.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.