优化iOS应用:多媒体、位置附件与链接处理
1. 多媒体与位置附件功能实现
在iOS应用开发中,为应用增添多媒体和位置附件功能可以显著提升用户体验。以下将详细介绍如何实现这些功能。
1.1 开启画中画模式
通过将应用的音频会话类别设置为 AVAudioSessionCategoryPlayback ,可以向系统表明应用仅用于播放内容,从而为播放器启用画中画模式。用户在观看视频时点击画中画按钮,视频将缩小至角落,即使离开应用,该视图仍会保留。
1.2 位置附件功能
iOS设备具备多种传感器,其中GPS在确定设备位置时起着重要作用。不过,iOS并非仅依赖GPS,还可通过其他方式更精准、快速地定位,主要有以下三种:
- 卫星定位 :通过接收轨道卫星的GPS或GLONASS信号来确定位置。
- WiFi定位 :利用众包的热点物理位置数据库,根据设备能检测到的热点来估算位置。
- 基站定位 :原理与WiFi定位类似,借助提供手机和数据覆盖的基站来确定位置。
Core Location框架提供了一系列基于位置的功能,我们将使用其中一小部分来实现位置附件功能。
1.2.1 准备工作
用户的位置信息属于隐私内容,应用需获得用户许可才能访问。因此,在应用中添加位置附件功能时,需进行如下设置:
1. 打开应用的 Info.plist 文件。
2. 在字典中添加新的字符串值 NSLocationWhenInUseUsageDescription ,并将其值设置为 We'll use your position to show where you were when you created your notes. 。此字符串将在应用首次尝试确定位置时以弹窗形式展示给用户。
3. 打开 Assets.xcassets 文件。
4. 将 Current Location.pdf 图像拖入图像列表。
5. 将其重命名为 Position 。
1.2.2 文档模型代码添加
接下来,需要在文档模型中添加处理位置JSON文件的代码:
1. 打开 Document.swift 文件。
2. 在 Document 类中添加新的 FileWrapper 属性:
var locationWrapper : FileWrapper?
- 在
contents(forType typeName: String)方法中添加以下代码:
override func contents(forType typeName: String) throws -> Any {
let textRTFData = try self.text.data(
from: NSRange(0..<self.text.length),
documentAttributes:
[NSDocumentTypeDocumentAttribute: NSRTFTextDocumentType])
if let oldTextFileWrapper = self.documentFileWrapper
.fileWrappers?[NoteDocumentFileNames.TextFile.rawValue] {
self.documentFileWrapper.removeFileWrapper(oldTextFileWrapper)
}
// Create the QuickLook folder
let thumbnailImageData =
self.iconImageDataWithSize(CGSize(width: 512, height: 512))!
let thumbnailWrapper =
FileWrapper(regularFileWithContents: thumbnailImageData)
let quicklookPreview =
FileWrapper(regularFileWithContents: textRTFData)
let quickLookFolderFileWrapper =
FileWrapper(directoryWithFileWrappers: [
NoteDocumentFileNames.QuickLookTextFile.rawValue: quicklookPreview,
NoteDocumentFileNames.QuickLookThumbnail.rawValue: thumbnailWrapper
])
quickLookFolderFileWrapper.preferredFilename =
NoteDocumentFileNames.QuickLookDirectory.rawValue
// Remove the old QuickLook folder if it existed
if let oldQuickLookFolder = self.documentFileWrapper
.fileWrappers?[NoteDocumentFileNames.QuickLookDirectory.rawValue] {
self.documentFileWrapper.removeFileWrapper(oldQuickLookFolder)
}
// Add the new QuickLook folder
self.documentFileWrapper.addFileWrapper(quickLookFolderFileWrapper)
// checking if there is already a location saved
let rawLocationVal = NoteDocumentFileNames.locationAttachment.rawValue
if self.documentFileWrapper.fileWrappers?[rawLocationVal] == nil {
// saving the location if there is one
if let location = self.locationWrapper {
self.documentFileWrapper.addFileWrapper(location)
}
}
self.documentFileWrapper.addRegularFile(withContents: textRTFData,
preferredFilename: NoteDocumentFileNames.TextFile.rawValue)
return self.documentFileWrapper
}
- 在
load(fromContents contents: Any, ofType typeName: String?)方法中添加以下代码:
override func load(fromContents contents: Any,
ofType typeName: String?) throws {
// Ensure that we've been given a file wrapper
guard let fileWrapper = contents as? FileWrapper else {
throw err(.cannotLoadFileWrappers)
}
// Ensure that this file wrapper contains the text file,
// and that we can read it
guard let textFileWrapper = fileWrapper
.fileWrappers?[NoteDocumentFileNames.TextFile.rawValue],
let textFileData = textFileWrapper.regularFileContents else {
throw err(.cannotLoadText)
}
// Read in the RTF
self.text = try NSAttributedString(data: textFileData,
options: [NSDocumentTypeDocumentAttribute: NSRTFTextDocumentType],
documentAttributes: nil)
// Keep a reference to the file wrapper
self.documentFileWrapper = fileWrapper
// opening the location filewrapper
let rawLocationVal = NoteDocumentFileNames.locationAttachment.rawValue
self.locationWrapper = fileWrapper.fileWrappers?[rawLocationVal]
}
- 在
Document类中添加新方法:
func addLocation(withData data: Data) {
// making sure we don't already have a location
guard self.locationWrapper == nil else {
return
}
let newLocation = FileWrapper(regularFileWithContents: data)
newLocation.preferredFilename
= NoteDocumentFileNames.locationAttachment.rawValue
self.locationWrapper = newLocation
self.updateChangeCount(.done)
}
1.2.3 创建视图控制器
创建用于显示位置附件的视图控制器,步骤如下:
1. 选择 File 菜单,选择 File→New 创建新文件。
2. 选择 Cocoa Touch Class 并点击 Next 。
3. 将新类命名为 LocationAttachmentViewController ,并使其成为 UIViewController 的子类。
4. 打开添加到项目中的 LocationAttachmentViewController.swift 文件。
5. 导入 MapKit 框架:
import MapKit
- 创建位置附件属性的出口:
var locationAttachment: FileWrapper?
- 创建地图视图的出口:
@IBOutlet weak var mapview: MKMapView?
- 打开
Main.storyboard,拖入新的UIViewController。 - 打开
Identity Inspector,将视图控制器的类更改为LocationAttachmentViewController。 - 将
MKMapView拖入视图控制器的界面。 - 添加约束使其填满整个界面。
- 打开
Attributes Inspector,选中Shows User Location复选框。 - 按住
Control键并从视图控制器拖动到地图视图,从出现的菜单中选择mapView。
1.2.4 实现地图注解代码
在 LocationAttachmentViewController.swift 中实现 viewWillAppear 方法,根据传入的JSON文件在地图上绘制注解:
override func viewWillAppear(_ animated: Bool) {
if let data = locationAttachment?.regularFileContents {
do {
guard let loadedData =
try JSONSerialization.jsonObject(with: data,
options: JSONSerialization.ReadingOptions())
as? [String:CLLocationDegrees] else {
return
}
if let latitude = loadedData["lat"],
let longitude = loadedData["long"] {
let coordinate = CLLocationCoordinate2D(latitude: latitude,
longitude: longitude)
// create a new annotation to show on the map
let annotation = MKPointAnnotation()
annotation.coordinate = coordinate
annotation.title = "Note created here"
self.mapview?.addAnnotation(annotation)
// moving the map to focus on the annotation
self.mapview?.setCenter(coordinate, animated: true)
}
}
catch let error as NSError {
print("failed to load location: \(error)")
}
}
}
1.2.5 确定用户位置并保存
在 DocumentViewController.swift 中实现确定用户位置并保存为JSON附件的功能:
1. 导入 CoreLocation 框架:
import CoreLocation
- 遵循
CLLocationManagerDelegate协议:
extension DocumentViewController: CLLocationManagerDelegate {
- 添加新属性:
var locationManager : CLLocationManager?
- 在
viewWillAppear方法的document.open闭包底部添加以下代码:
// checking if there isn't already a location file
if self.document?.locationWrapper == nil {
// determining our location permission status
let status = CLLocationManager.authorizationStatus()
if status != .denied && status != .restricted {
self.locationManager = CLLocationManager()
self.locationManager?.delegate = self
if status == .notDetermined {
self.locationManager?.requestWhenInUseAuthorization()
}
else {
self.locationManager?.desiredAccuracy
= kCLLocationAccuracyBest
self.locationManager?.startUpdatingLocation()
}
}
}
self.updateBarItems()
- 更新
updateBarItems方法:
// the button to segue to the attachment view controller
let image = UIImage(named: "Position")
let showButton = UIBarButtonItem(image: image, style: .plain,
target: self, action: #selector(showLocation))
// if there is already a location
if self.document?.locationWrapper != nil {
// we show the segue button
rightButtonItems.append(showButton)
} else {
// if we don't have permission or permission is denied
let status = CLLocationManager.authorizationStatus()
if status == .denied || status == .restricted {
// we don't have permission
rightButtonItems.append(showButton)
} else {
// the activity indicator to show when determining location
let spinner = UIActivityIndicatorView(activityIndicatorStyle: .gray)
spinner.startAnimating()
let spinItem = UIBarButtonItem(customView: spinner)
rightButtonItems.append(spinItem)
}
}
- 实现位置管理器委托方法:
func locationManager(_ manager: CLLocationManager,
didChangeAuthorization status: CLAuthorizationStatus) {
if status == .authorizedWhenInUse {
self.locationManager?.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager?.startUpdatingLocation()
}
self.updateBarItems()
}
func locationManager(_ manager: CLLocationManager,
didUpdateLocations locations: [CLLocation]) {
self.locationManager?.stopUpdatingLocation()
guard let location = locations.last else {
return
}
// creating a json representation of our location
let latitude = location.coordinate.latitude
let longitude = location.coordinate.longitude
let locationData = ["lat":latitude, "long":longitude]
do {
let json = try JSONSerialization.data(withJSONObject: locationData,
options: JSONSerialization.WritingOptions())
// saving our location to the document
self.document?.addLocation(withData: json)
}
catch let error as NSError
{
print("unable to save location: \(error)")
self.locationManager?.startUpdatingLocation()
}
self.updateBarItems()
}
- 实现
showLocation方法:
func showLocation() {
self.performSegue(withIdentifier: "ShowLocationSegue", sender: self)
}
- 设置
showLocationSegue:- 打开
main.storyboard。 - 按住
Control键并从文档视图控制器拖动到位置附件视图控制器,从列表中选择show。 - 为该转场添加标识符
ShowLocationSegue。 - 在
prepare(for segue: sender:)方法中添加以下代码:
- 打开
else if segue.identifier == "ShowLocationSegue" {
if let destination = segue.destination as?
LocationAttachmentViewController {
destination.locationAttachment = self.document?.locationWrapper
}
}
2. 优化链接打开功能
当前应用中文本中的链接存在一些问题,如仅在文本视图不可编辑时可用,且点击链接会打开Safari,将用户带出应用。为解决这些问题,我们将实现以下功能。
2.1 模式切换
为 DocumentViewController 添加“编辑”和“查看”模式切换功能:
1. 在 viewDidLoad 方法中添加以下代码:
override func viewDidLoad() {
super.viewDidLoad()
self.editing = false
}
- 重写
setEditing方法:
override func setEditing(_ editing: Bool, animated: Bool) {
super.setEditing(editing, animated: animated)
self.textView.isEditable = editing
if editing {
// If we are now editing, make the text view take
// focus and display the keyboard
self.textView.becomeFirstResponder()
}
updateBarItems()
}
2.2 链接检测
使文本视图能够检测链接:
1. 打开 Main.storyboard ,进入文档视图控制器。
2. 选择文本视图,打开 Attributes Inspector 。
3. 在 Detection 部分启用 Links 。
2.3 拦截链接并在SFSafariViewController中打开
最后,拦截链接点击事件,并在 SFSafariViewController 中打开链接:
1. 打开 DocumentViewController.swift 。
2. 导入 SafariServices 框架:
import SafariServices
- 实现
textView(_, shouldInteractWith URL:, inRange:)方法:
func textView(_ textView: UITextView, shouldInteractWith URL: URL,
in characterRange: NSRange) -> Bool {
let safari = SFSafariViewController(url: URL)
self.present(safari, animated: true, completion: nil)
// return false to not launch in Safari
return false
}
通过以上步骤,我们成功为iOS应用添加了多媒体和位置附件功能,并优化了链接打开方式,提升了用户体验。
优化iOS应用:多媒体、位置附件与链接处理
3. 整体功能总结与流程图展示
为了更清晰地理解整个应用的功能实现流程,下面我们用 mermaid 流程图展示位置附件功能的主要流程:
graph TD;
A[应用启动] --> B[检查权限设置];
B -->|未设置| C[设置权限信息];
C --> D[准备位置附件相关资源];
B -->|已设置| D;
D --> E[文档模型添加位置处理代码];
E --> F[创建位置附件视图控制器];
F --> G[实现地图注解代码];
G --> H[确定用户位置并保存];
H --> I[处理用户查看位置请求];
从这个流程图可以看出,整个位置附件功能的实现是一个循序渐进的过程,从权限设置到最终用户查看位置,每个步骤都紧密相连。
同时,我们可以用表格总结一下位置附件功能实现的关键步骤和对应的文件:
| 步骤 | 操作内容 | 文件 |
|---|---|---|
| 准备工作 | 设置权限、准备图像资源 | Info.plist 、 Assets.xcassets |
| 文档模型处理 | 添加位置处理属性和方法 | Document.swift |
| 视图控制器创建 | 创建并配置视图控制器 | LocationAttachmentViewController.swift 、 Main.storyboard |
| 地图注解实现 | 根据JSON文件在地图上绘制注解 | LocationAttachmentViewController.swift |
| 位置确定与保存 | 确定用户位置并保存为JSON附件 | DocumentViewController.swift |
4. 链接处理功能总结与注意事项
链接处理功能的实现主要是为了解决当前应用中文本链接存在的问题,通过模式切换、链接检测和在 SFSafariViewController 中打开链接,提升了用户在处理链接时的体验。下面是链接处理功能的步骤总结:
- 模式切换 :
- 在
viewDidLoad方法中设置初始编辑状态为false。
swift override func viewDidLoad() { super.viewDidLoad() self.editing = false } - 重写
setEditing方法,根据编辑状态设置文本视图的可编辑性。
swift override func setEditing(_ editing: Bool, animated: Bool) { super.setEditing(editing, animated: animated) self.textView.isEditable = editing if editing { self.textView.becomeFirstResponder() } updateBarItems() }
- 在
- 链接检测 :
- 在
Main.storyboard中为文本视图启用链接检测。
- 在
- 拦截链接并打开 :
- 导入
SafariServices框架。
swift import SafariServices - 实现
textView(_, shouldInteractWith URL:, inRange:)方法,在SFSafariViewController中打开链接。
swift func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange) -> Bool { let safari = SFSafariViewController(url: URL) self.present(safari, animated: true, completion: nil) return false }
- 导入
在实现链接处理功能时,需要注意以下几点:
- 确保 textView 的代理已经正确设置,否则 textView(_, shouldInteractWith URL:, inRange:) 方法可能不会被调用。
- 在使用 SFSafariViewController 时,要考虑到不同网络环境和页面加载情况,可能需要添加一些错误处理和加载提示。
5. 未来优化方向与建议
虽然我们已经为应用添加了多媒体、位置附件和优化了链接处理功能,但仍然有一些可以优化的方向:
- 性能优化 :在确定用户位置时,由于使用了位置硬件,可能会消耗较多的电量。可以考虑添加一些策略,如在用户长时间静止时减少位置更新频率,或者在应用进入后台时暂停位置更新。
- 用户体验优化 :
- 在地图显示方面,可以添加更多的交互功能,如缩放控制、地图类型切换等,让用户有更好的地图查看体验。
- 在链接打开时,可以添加一些过渡动画,让页面切换更加流畅。
- 功能扩展 :
- 可以添加更多类型的多媒体附件,如视频、音频的编辑功能。
- 对于位置附件,可以结合更多的地理信息服务,如周边搜索、导航等。
通过不断地优化和扩展,我们可以让应用更加完善,为用户提供更好的使用体验。
通过以上的实现和优化,我们的 iOS 应用在多媒体、位置附件和链接处理方面都有了显著的提升,不仅功能更加丰富,而且用户体验也得到了极大的改善。希望这些内容能为开发者在开发类似应用时提供一些参考和帮助。
超级会员免费看
21

被折叠的 条评论
为什么被折叠?



