Skip to content

Commit

Permalink
Release 1.1.1, handle crash on nil title, author, artwork
Browse files Browse the repository at this point in the history
  • Loading branch information
GianniCarlo committed Dec 1, 2016
1 parent 4d6b011 commit 337306a
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 16 deletions.
6 changes: 4 additions & 2 deletions Audiobook Player.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Distribution";
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
COPY_PHASE_STRIP = NO;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
ENABLE_NS_ASSERTIONS = NO;
Expand Down Expand Up @@ -423,6 +423,7 @@
CODE_SIGN_ENTITLEMENTS = "Audiobook Player/Audiobook Player.entitlements";
CODE_SIGN_IDENTITY = "iPhone Developer";
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
DEVELOPMENT_TEAM = S7TJSJXWUZ;
FRAMEWORK_SEARCH_PATHS = (
"$(inherited)",
"$(PROJECT_DIR)/Carthage/Build/iOS",
Expand All @@ -442,7 +443,8 @@
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_ENTITLEMENTS = "Audiobook Player/Audiobook Player.entitlements";
CODE_SIGN_IDENTITY = "iPhone Distribution";
CODE_SIGN_IDENTITY = "iPhone Developer";
DEVELOPMENT_TEAM = S7TJSJXWUZ;
FRAMEWORK_SEARCH_PATHS = (
"$(inherited)",
"$(PROJECT_DIR)/Carthage/Build/iOS",
Expand Down
12 changes: 10 additions & 2 deletions Audiobook Player/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<string>1.1.1</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>2</string>
<string>1</string>
<key>Fabric</key>
<dict>
<key>APIKey</key>
Expand All @@ -36,6 +36,14 @@
</dict>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>NSCameraUsageDescription</key>
<string>Snap &amp; share photos</string>
<key>NSContactsUsageDescription</key>
<string>To make it easier to connect with people you know and improve suggestions</string>
<key>NSMicrophoneUsageDescription</key>
<string>Enable microphone access so you can record audio</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>Add photos</string>
<key>UIBackgroundModes</key>
<array>
<string>audio</string>
Expand Down
21 changes: 14 additions & 7 deletions Audiobook Player/ListBooksViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -255,10 +255,10 @@ extension ListBooksViewController: UITableViewDataSource {

let item = self.itemArray[indexPath.row]

cell.titleLabel.text = AVMetadataItem.metadataItems(from: item.asset.metadata, withKey: AVMetadataCommonKeyTitle, keySpace: AVMetadataKeySpaceCommon).first?.value?.copy(with: nil) as? String
cell.titleLabel.text = AVMetadataItem.metadataItems(from: item.asset.metadata, withKey: AVMetadataCommonKeyTitle, keySpace: AVMetadataKeySpaceCommon).first?.value?.copy(with: nil) as? String ?? "Unknown Book"
cell.titleLabel.highlightedTextColor = UIColor.black

cell.authorLabel.text = AVMetadataItem.metadataItems(from: item.asset.metadata, withKey: AVMetadataCommonKeyArtist, keySpace: AVMetadataKeySpaceCommon).first?.value?.copy(with: nil) as? String
cell.authorLabel.text = AVMetadataItem.metadataItems(from: item.asset.metadata, withKey: AVMetadataCommonKeyArtist, keySpace: AVMetadataKeySpaceCommon).first?.value?.copy(with: nil) as? String ?? "Unknown Author"

var defaultImage:UIImage!
if let artwork = AVMetadataItem.metadataItems(from: item.asset.metadata, withKey: AVMetadataCommonKeyArtwork, keySpace: AVMetadataKeySpaceCommon).first?.value?.copy(with: nil) as? Data {
Expand Down Expand Up @@ -364,8 +364,11 @@ extension ListBooksViewController: UITableViewDelegate {

let url = self.urlArray[indexPath.row]
self.playerViewController!.fileURL = url

self.footerTitleLabel.text = cell.titleLabel.text! + " - " + cell.authorLabel.text!

let title = cell.titleLabel.text ?? "Unknown Book"
let author = cell.authorLabel.text ?? "Unknown Author"

self.footerTitleLabel.text = title + " - " + author
self.footerImageView.image = cell.artworkImageView.image

self.navigationController?.pushViewController(self.playerViewController!, animated: true)
Expand Down Expand Up @@ -407,7 +410,6 @@ extension ListBooksViewController:UIDocumentMenuDelegate {

extension ListBooksViewController:UIDocumentPickerDelegate {
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {
print("file picked: \(url)")

//Documentation states that the file might not be imported due to being accessed from somewhere else
do {
Expand All @@ -425,8 +427,13 @@ extension ListBooksViewController:UIDocumentPickerDelegate {
}

let fileURL = URL(fileURLWithPath: finalPath.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)!)

try! FileManager.default.moveItem(at: url, to: fileURL)

do {
try FileManager.default.moveItem(at: url, to: fileURL)
}catch{
self.showAlert("Error", message: "File import fail, try again later", style: .alert)
return
}

self.loadFiles()
}
Expand Down
19 changes: 14 additions & 5 deletions Audiobook Player/PlayerViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,22 @@ class PlayerViewController: UIViewController {
self.titleLabel.text = AVMetadataItem.metadataItems(from: self.playerItem.asset.metadata, withKey: AVMetadataCommonKeyTitle, keySpace: AVMetadataKeySpaceCommon).first?.value?.copy(with: nil) as? String

self.authorLabel.text = AVMetadataItem.metadataItems(from: self.playerItem.asset.metadata, withKey: AVMetadataCommonKeyArtist, keySpace: AVMetadataKeySpaceCommon).first?.value?.copy(with: nil) as? String

let artwork = AVMetadataItem.metadataItems(from: self.playerItem.asset.metadata, withKey: AVMetadataCommonKeyArtwork, keySpace: AVMetadataKeySpaceCommon).first?.value?.copy(with: nil) as! Data

var defaultImage:UIImage!
if let artwork = AVMetadataItem.metadataItems(from: self.playerItem.asset.metadata, withKey: AVMetadataCommonKeyArtwork, keySpace: AVMetadataKeySpaceCommon).first?.value?.copy(with: nil) as? Data {
defaultImage = UIImage(data: artwork)
}else{
defaultImage = UIImage()
}

let title = self.titleLabel.text?.replacingOccurrences(of: " ", with: "_") ?? "defaulttitle"
let author = self.authorLabel.text?.replacingOccurrences(of: " ", with: "_") ?? "defaultauthor"

self.identifier = title+author

if title == "defaulttitle" && author == "defaultauthor" {
self.showAlert(nil, message: "We can't correctly store your progress of books that don't have a title or author specified", style: .alert)
}

//set initial state for slider
self.sliderView.setThumbImage(UIImage(), for: UIControlState())
Expand All @@ -105,7 +114,7 @@ class PlayerViewController: UIViewController {
//load data on background thread
DispatchQueue.global().async {

let mediaArtwork = MPMediaItemArtwork(image: UIImage(data: artwork) ?? UIImage())
let mediaArtwork = MPMediaItemArtwork(image: defaultImage)

//try loading the data of the book
guard let data = FileManager.default.contents(atPath: self.fileURL.path) else {
Expand Down Expand Up @@ -179,8 +188,8 @@ class PlayerViewController: UIViewController {

//set book metadata for lockscreen and control center
MPNowPlayingInfoCenter.default().nowPlayingInfo = [
MPMediaItemPropertyTitle: self.titleLabel.text!,
MPMediaItemPropertyArtist: self.authorLabel.text!,
MPMediaItemPropertyTitle: self.titleLabel.text ?? "Unknown Book",
MPMediaItemPropertyArtist: self.authorLabel.text ?? "Unknown Author",
MPMediaItemPropertyPlaybackDuration: audioplayer.duration,
MPMediaItemPropertyArtwork: mediaArtwork
]
Expand Down

0 comments on commit 337306a

Please sign in to comment.