iOS 9的新内容

https://www.hackingwithswift.com/ios9

Search extensibility

Update: I wrote a tutorial on Core Spotlight in iOS 9 as Hacking with Swift project 32 – check it out!

Finally (finally!) you can hook your apps directly into Spotlight for integrated, system-wide search, and it comes in the form of CoreSpotlight. But don‘t get your hopes up: Spotlight doesn‘t just magically index all your data while you sit back and snooze. Instead, you need to specify exactly what you want to expose to the outside world.

What‘s more, Apple is specifically focusing this technology on things that are going to be directly useful to the user, so you need to be careful what you index in Spotlight. We don‘t know how the algorithm works, but Apple has said that iOS will automatically monitor how frequently users interact with your search results, and if you consistently serve up unhelpful results because you indexed your data badly then your results may stop appearing.

To get started, create a new instance of the CSSearchableItemAttributeSet class, then give it a title and acontentDescription. You then wrap that inside a CSSearchableItem, before passing it to CSSearchableIndex using itsindexSearchableItems method.

If a user taps on one of your search results in Spotlight, your app will get launched with a new app delegate callback:application:continueUserActivity:restorationHandler:. If the activity type of the NSUserActivity that gets passed in matches CSSearchableItemActionType, you can immediately direct the user to the relevant content by pulling out theCSSearchableItemActivityIdentifier key from the activity‘s userInfo dictionary.

If you prefer to read code, here‘s an example:

let attributeSet = CSSearchableItemAttributeSet(itemContentType: kUTTypeImage as String)
attributeSet.title = "My Great Content"
attributeSet.contentDescription = "This is something you‘ll search for."

let item = CSSearchableItem(uniqueIdentifier: "YOUR UNIQUE CONTENT IDENTIFIER HERE", domainIdentifier: "com.hackingwithswift", attributeSet: attributeSet)
CSSearchableIndex.defaultSearchableIndex().indexSearchableItems([item]) { (err: NSError?) -> Void in
    print("Search item successfully indexed!")
}

You‘ll need to import both CoreSpotlight and MobileCoreServices to make that code work. You will also need to replace "YOUR UNIQUE CONTENT IDENTIFIER HERE" with something meaningful to you that uniquely identifies this content, so that if the user launches your app as a result of doing a Spotlight search, you know what to look for.

Speaking of which, here‘s the code to put in your app delegate to load some content after a Spotlight search:

func application(application: UIApplication, continueUserActivity userActivity: NSUserActivity, restorationHandler: ([AnyObject]?) -> Void) -> Bool {
    if userActivity.activityType == CSSearchableItemActionType {
        let uniqueIdentifier = userActivity.userInfo?[CSSearchableItemActivityIdentifier]
        print("Do stuff with \(uniqueIdentifier)")
    }

    return true
}

Again, make sure you import CoreSpotlight so your code compiles. Hopefully you can now see why the unique identifier is so important – it‘s what your app gets handed so you can load the correct content.

There are various extensions to CSSearchableItemAttributeSet to encode data such as events and map co-ordinates, so this could prove to be one of the biggest new features in iOS 9. To see just how much information you can encode (focal length for images! media copyright information! contact phone numbers!) press Shift+Cmd+O and type CSSearchableItemAttributeSet then wait for the completions to appear - you‘ll see media, events, images, places and more.

App Thinning

Universal apps (aka "fat binaries") have always meant that Apple ships a single app package that contains all the assets and code for it to run on any supported device. So, since the iPad launched that meant every iPhone app automatically included all the iPad imagery even though it wasn‘t used.

When the number of supported devices was low (non-retina iPhone and non-retina iPad) this wasn‘t a huge problem, and if you were smart about your assets most users wouldn‘t care. But as devices have proliferated, each app now needs to include iPhone retina, iPhone retina HD, iPad non-retina and iPad retina, all in one package, even though each device will only be showing one set of assets.

Apple‘s solution is something developers have been asking for for a long time: app thinning ensures that users download only the app components required to run the app on their specific device, which means an iPhone retina device (4s, 5, 5s, 6) will download only retina assets and not retina HD devices. The result: smaller apps, faster downloads, and happier users.

But App Thinning is even cleverer, because with Xcode 7 you upload your app to iTunes connect in an intermediate state called Bitcode. When users download your app, the App Store will automatically produce an optimized version of your app (i.e., 64-bit if needed) to match the device the user is using. This means they get only the assets they need and only the binary code they need, making the download even smaller.

Still think that‘s not enough? Apparently Apple agrees, because as part of App Thinning you can now host content for your app in iTunes Connect, and request it inside your app only when needed. This has been possible with in-app purchases for a few years, but being able to tag and fetch content on demand is new and very interesting.

I wonder whether Apple is shipping app thinning now in preparation to launch an @3x iPad – such a device would have huge images, so old school universal binaries would be more wasteful than ever.

GameplayKit

Update: I wrote a tutorial on GameplayKit and GKMinmaxStrategist in iOS 9 as Hacking with Swift project 34 – check it out!

Update 2: I wrote a tutorial on GameplayKit and GKRandomSource in iOS 9 as Hacking with Swift project 35 – check that out too!

This is easily one of the most surprising changes in iOS 9, and it was rushed over so quickly in the keynote that if you blinked you probably missed it. GameplayKit is surprising because of what it does: it‘s designed to make it easy to add high-level gameplay logic without having to code the algorithms yourself. State machines? Done. Randomization? Check. Path finding? Got it.

To give you an idea of just how powerful GameplayKit is, Apple has shipped demo code for a four-in-a-row game that uses a new class called GKMinmaxStrategist. This is a class that can, under certain conditions, provide AI responses to automatically win in a game. Those conditions are pretty simple: the game must be sequential (i.e., players take turns), the game must be zero sum (one player loses when the other wins), the game must not be based on chance, and you must be able to provide GKMinmaxStrategist with perfect information – that is, all information required to play and win is openly visible.

SFSafariViewController

Update: I wrote a tutorial on SFSafariViewController in iOS 9 as Hacking with Swift project 32 – check it out!

iOS 8 gave us WKWebView, the super-fast new way of using WebKit inside our apps that also included some UI elements such as swiping between the page history. iOS 9 goes one step further: SFSafariViewController is a wholly embedded Safari inside your app, exposing even more behaviour that users have come to expect, such as shared cookies, AutoFill of forms, and Reader Mode.

This might seem like a strange thing to do, but think about it: lots of apps have embedded web browsers inside them, such as when you tap a link inside Twitter. But frequently these embedded web views just aren‘t as good as Safari, so you end up tapping the inevitable "Open in Safari" button just to escape. Well, with iOS 9 is no longer needed: SFSafariViewController is a full-screen Safari user interface controller by Apple, with a simple Done button to return to your app.

To try it out now, you need to do four things. First, pull in the SafariServices framework like this:

import SafariServices

Second, make your view controller conform to the SFSafariViewControllerDelegate delegate, using something like this:

class ViewController: UIViewController, SFSafariViewControllerDelegate {

Third, create and show the SFSafariViewController, pointing at a URL of your choosing:

let sfc = SFSafariViewController(URL: NSURL(string: "http://www.slashdot.org")!)
sfc.delegate = self
presentViewController(sfc, animated: true, completion: nil)

Finally, catch the delegate callback safariViewControllerDidFinish(), which is triggered when the user taps the built-in Done button:

func safariViewControllerDidFinish(controller: SFSafariViewController) {
    controller.dismissViewControllerAnimated(true, completion: nil)
}

One of the neat features of SFSafariViewController is that it comes with reader mode built in, and you can actually navigate straight to that view by using this code:

let sfc = SFSafariViewController(URL: NSURL(string: "http://www.slashdot.org")!, entersReaderIfAvailable: true)

UIStackView

Update: I wrote a tutorial on UIStackView and iPad multitasking in iOS 9 as Hacking with Swift project 31 – check it out!

Android developers have long had a layout system called LinearLayout, where you add views to a superview and they automatically get placed either beneath each other or side by side. We haven‘t really had a good solution like this on iOS – table views are greatly at stacking information vertically, but are of course hugely limited in terms of their content.

With UIStackView, that‘s all changed: you can now add a series of views to a superview, specify how much spacing you want between them, and Auto Layout handles the rest for you. You get to choose whether you want the subviews to have equal sizes (.FillEqually) or proportional sizes (.FillProportionally) based on their intrinsic content sizes. You can also choose whether you want the subviews to fill horizontally or vertically, and you can change your mind later.

Important note: UIStackViews don‘t scroll, they just act as containers that automatically fit their content.

UICollectionViews can now behave more like UITableViews

With one simple boolean property for UICollectionViewFlowLayoutsectionHeadersPinToVisibleBounds, you can now make your flow layout work the same way as UITableView has always done: your section headers stick to the top of the screen while the user is scrolling inside that section, and only get pushed off when the next section comes in. Way, way overdue, but at least it‘s here now.

UICollectionViews now have easy re-ordering

Set the installsStandardGestureForInteractiveMovement property to true on your UICollectionViewController and give users the ability to re-order your items.

SKAudioNode

One of the major SpriteKit annoyances has been its simplistic audio system. In iOS 8, for example, you can play sounds but not stop them, and heaven forbid if you want to control the sound in any way. Well, in iOS 9 we have SKAudioNode to the rescue: not only can you pause and stop sounds (hurray!) but you can also set its positional property to true to have its sound adjusted based on the node‘s position in your scene. Yes: SpriteKit now has 3D audio.

Lots of tiny changes

One of my favorite things to do when Apple flicks the switch on a new iOS release is to read through the complete list of API changes, looking for all the tiny improvements and tweaks Apple makes each year. Here are some of the changes that jumped out to me this year:

  • UIViewController now has an addKeyCommand() method to register UIKeyCommands on your view controllers – manna from heaven for users of external keyboards.
  • MKMapView.showsTraffic: set it to true, and it, er, shows the traffic. Simple, but such a huge feature to have.
  • CIFeatureTypeText sounds like Apple added some sort of OCR to iOS, but sadly it just gives you the bounds where text was located. It‘s a step forward, but only a small one.
  • Just look at AVFoundation. Go on, look at it and cry tears of joy.
时间: 2024-10-02 02:40:00

iOS 9的新内容的相关文章

WWDC三大看点:Mac新系统Yosemite、iOS 8及新编程语言Swift

导语:苹果公司专门针对开发人员公布全新的 Swift 编程语言以及新版的 Xcode,苹果公司称其拥有"快速.现代.安全.互动"等特性 苹果在今年的 WWDC 开发者大会上发布了新版 Mac OS X 系统及 iOS 系统,带来多项改进或体验优化.本次大会是苹果第 25 届开发者大会,苹果注册开发者已达 900 万名. 据苹果 CEO 库克介绍,全球 PC 行业下滑5% 的情况下,苹果 Mac 却实现了 12% 的增长,总装机量达到 8000 万台,而最新一代操作系统 Maverick

iOS 9的新的改变 iOS SDK Release Notes for iOS 9 说了些改变

iOS 9的新的改变 iOS SDK Release Notes for iOS 9 说了些改变 看了下还算能理解!!!有兴趣可以看看哈!!!不喜勿喷!!后面的对于废除的方法什么有用感觉!!! ios9 iOS SDK更新内容介绍 重点介绍: 这是一个初步的文档的API或技术发展.苹果是提供这一信息来帮助你计划的采用所述技术和编程接口使用品牌产品.此信息可能发生变化,根据这个文档和软件实现应与最后的操作系统软件测试和最终的文档.新版本的文档可以提供未来贝塔的API或技术. 内容: 介绍 iOS

WWDC总结:开发者需要知道的iOS 9 SDK新特性

http://blog.csdn.net/uxyheaven/article/details/46470801 编者按:WWDC 2015苹果开发者大会是移动开发者一年一度的盛会,InfoQ中文站除了第一时间整理Keynote内容分享给大家之外,还邀请了资深的一线开发者分享他们的收获.本文为王巍(@onevcat)对WWDC上透露的iOS 9 SDK新特性的总结,分享给广大的iOS开发者. 年年岁岁花相似,岁岁年年人不同.今年的 WWDC 一如既往的热闹,得益于 Apple 的随机抽选机制,这两

学习Python总是学了新内容又忘了旧的咋办?

学习Python总是学了新内容又忘了旧的咋办? 比如爬虫的几个解析库,学了pyquery后感觉beautifulsoup又有点忘了,只能大概记得有哪些功能,xpath感觉基本忘光了,最近看了一些selenium的说明文档,感觉也是脑袋里乱乱的,用起来还要到网上查. 1.不仅仅是学习Python,任何一门知识都是,记忆差是学习的大敌. 很多初学者在接触新知识的时候都会给自己很大的压力,这种“习惯性压力思维”从人很小的时候就已经存在了,学习识字的时候,家长们往往最头疼的就是孩子今天学十个字,明早睡醒

在WWDC 2014上,没提到的iOS 8 八大新特性

title: The following iterative sequence is defined for the set of positive integers: n n/2 (n is even) n 3n + 1 (n is odd) Using the rule above and starting with 13, we generate the following sequence: 13 40 20 10 5 16 8 4 2 1 It can be seen that thi

用angular方法简单实现了吃了么搜索小功能,还不太完善,后续会继续添加新内容。

最近接触了Angular框架,今天用里面的http请求方法做了一个小的案例,是一个查询地名获取附近美食的小案例.还不太完善,后面面有时间会继续添加新的内容.这个小案例没有用到任何的jQuery与原生js方法. 先上HTML结构代码与HTML结构中的angular指令. <body ng-app="app"> <div ng-controller="con" id="con"> <h1>吃了么</h1>

(转) IOS程序名称及内容国际化(本地化)

1.IOS程序名称国际化 1.1 新建一个Single View app模版项目,命名为Localization. 1.2 新建后,可以看到工作目录结构文件如下,单击InfoPlist.strings,查看右边的属性,在Localization栏添加语言.      1.3 添加完成后打开对应语言文件,比如English的添加 CFBundleDisplayName="China"; Chinese文件添加 CFBundleDisplayName="中国"; 运行,

替换文件中某个字符串并写入新内容(Java代码实现)

import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileWriter; import java.io.InputStream; import java.io.InputStreamReader; /* * 替换文件(如果该文件含有子目录,则包括子目录所有文件)中某个字符串并写入新内容(J

添加新内容的四个 jQuery 方法:append,prepend,after,before

添加新内容的四个 jQuery 方法区别如下: append() - 在被选元素(里面)的结尾插入内容prepend() - 在被选元素(里面)的开头插入内容 //jQuery append() 方法在被选元素的结尾插入内容. $("p").append("Some appended text."); //jQuery prepend() 方法在被选元素的开头插入内容. $("p").prepend("Some prepended te