NSAttributed String missing return value - ios

I'm trying to make this function inside a VC into a function within an extension (because I need to access it in multiple VC's so I'm trying to return the attributedStringWithRtf so i can use it elsewhere.
func populateTextViewWithCurrentScene() {
let fileURL = getFileURL()
do {
let attributedStringWithRtf:NSAttributedString = try NSAttributedString(
url: fileURL,
options: [.documentType: NSAttributedString.DocumentType.rtf],
documentAttributes: nil
)
self.textViewOutlet.attributedText = attributedStringWithRtf
}
catch {
print("failed to populate text view with current scene with error: \(error)")
}
}
So far, I've tried this, following the guide here How could I create a function with a completion handler in Swift? and I've also tried a version declaring a var before the function. The error I'm getting on the below is Cannot call value of non-function type 'NSAttributedString'.
I know there are quite a few questions about this sort of thing but a lot are for old versions of Swift
func populateTextViewWithCurrentScene(rtfString: NSAttributedString) -> Void {
let fileURL = getFileURL()
do {
let rtfString:NSAttributedString = try NSAttributedString(
url: fileURL,
options: [.documentType: NSAttributedString.DocumentType.rtf],
documentAttributes: nil
)
}
catch {
print("failed to populate text view with current scene with error: \(error)")
}
rtfString()
}

I went ahead and created an extension of UIViewController that should provide what you are looking for. Comments are included for each line to explain the decisions that I made.
Feel free to comment if some part is unclear, or does not work as expected.
import UIKit
// You mentioned wanting to extend your viewcontroller
// so I extend UIViewController to support that
extension UIViewController {
// Returns an optional NSAttributedString, based upon successfully loading the file contents
func loadString() -> NSAttributedString? {
do {
// Everything is cleaned up into a single return command,
// including the getFileURL, which can be used as a parameter instead
// of creating a variable for it
return try NSAttributedString(url: getFileURL(),
options: [.documentType: NSAttributedString.DocumentType.rtf],
documentAttributes: nil)
} catch {
// If this fails, use your existing print command
print("failed to populate text view with current scene with error: \(error)")
// and then return nil to indicate that nothing was loaded
return nil
}
}
}
This is building off of the comments below from you and rmaddy.
As referenced in my original comment, the solution was not consolidating the try & return, that was simply to streamline the code.
You can look at the function this way:
"I want to try and open a file that is located at getFileURL(), and
would like to use some options I specify with my options: parameter.
Since this action can fail, Xcode makes me use try.
Assuming that this file is successfully opened, then return the
contents back to the caller in the form of an NSAttributedString.
However, if this fails, print out a message telling me why it failed
and then return a nil to indicate that no data is returned."

Related

Export all images from Core Data SwiftUI

I'm trying to build out a simple way for my users to export their data outside of the app.. nothing that needs to be imported back in, just some way for them to back up the data for reference purposes. I have a Core Data Entity Project and the users are able to individually share a project in order to save the project data and images using the standard iOS Share Sheet. Works great.
However I'd like there to be a solution to export everything at once, not just individual projects one at a time.
I have part of it working, where I can export the data from Core Data (that isn't an image) into a CSV for users to reference. However I'm stuck on finding the best way to get all the Images exported in a similar singular button. Allowing the user to pick a location where a Folder would be created containing the images would be ideal.
Here's my code for the CSV export which works great:
func exportCSV() {
let fileName = "Metadata_Export_\(Date()).csv"
let path = NSURL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(fileName)
var csvText = "Name,Date,Project_Description\n"
for project in projects {
csvText += "\(project.person?.name ?? "-"),\(project.date ?? Date()),\(project.bodyText ?? "-"),\n"
}
do {
try csvText.write(to: path!, atomically: true, encoding: String.Encoding.utf8)
} catch {
print("Failed to create file")
print("\(error)")
}
print(path ?? "not found")
var filesToShare = [Any]()
filesToShare.append(path!)
let av = UIActivityViewController(activityItems: filesToShare, applicationActivities: nil)
UIApplication.shared.windows.first?.rootViewController?.present(av, animated: true, completion: nil)
isShareSheetShowing.toggle()
}
Now I just need to get the Images exported out. Images are saved in Core Data as Binary objects, and will be written to File if they're larger than 128kb (and therefore written to blob in CD if less than 128kb).
The images are stored in CD as Optionals, project.image1, project.image2, project.image3, and project.image4
I've looked at examples using fileManager and other solutions, but I'm not sure on the correct approach to pursue since many of those are actually alternatives to saving images in Core Data - not necessarily configuring user interaction for picking where to export images.
Can the above exportCSV function be adapted to a similar result for the project's images? My app supports iOS 14 and later, if that makes a difference. Thanks for any suggestions/direction!
=== UPDATE ===
I've discovered fileExporter() which seems like a promising solution. I've been able to implement a simple POC of this method by exporting an Image I have stored in my Assets folder. Has anyone used this method to achieve exporting all images out of Core Data?
I can add the modifier to my view:
.fileExporter(isPresented: $exportFile, documents: [
ImageDocument(image: UIImage(named: "testimage"))
],
contentType: .png, onCompletion: { (result) in
if case .success = result {
print("Success")
} else {
print("Failure")
}
})
}
Using an ImageDocument Struct as follows:
struct ImageDocument: FileDocument {
static var readableContentTypes: [UTType] { [.jpeg, .png, .tiff] }
var image: UIImage
init(image: UIImage?) {
self.image = image ?? UIImage()
}
init(configuration: ReadConfiguration) throws {
guard let data = configuration.file.regularFileContents,
let image = UIImage(data: data)
else {
throw CocoaError(.fileReadCorruptFile)
}
self.image = image
}
func fileWrapper(configuration: WriteConfiguration) throws -> FileWrapper {
// You can replace tiff representation with what you want to export
return FileWrapper(regularFileWithContents: image.jpegData(compressionQuality: 1)!)
}
}
So how can I have it include an Array of all images?

phResourceManager.writeData(for... wont run on iOS 14

Before I updated to iOS 14 on my iPhone, this code was working perfectly. After, iOS 14 this is weirdly not running... it is very odd and I have not seen any solution online, additionally from my investigation, I have not been able to see any change.
This code is used in order to retrieve a videoURL for this video from the imported Camera Roll (I use import Photos...).
phResourceManager.writeData(for: resource.last!, toFile: newURL!, options: resourceRequestOptions) { (error) in
if error != nil {
print(error, "not c67omplted error?")
} else {
print("woah completedd 345?")
newUserTakenVideo.videoURL = newURL
print(newUserTakenVideo.videoURL, "<--?")
}
}
EDIT:
To be clear, it "does not run" means the compleition block never runs... as in it never even runs and gives an error, the compleition block simply never is called (nothing prints at least..)
And here is a print statement printing out all the values I pass in to the parameters:
phResourceManager:
<PHAssetResourceManager: 0x282d352c0>
resource.last:
Optional(<PHAssetResource: 0x28128bc00> {
type: video
uti: public.mpeg-4
filename: v07044090000bu6n1nhlp4leque7r720.mp4
asset: C97B45D3-7039-4626-BA3E-BCA67912A2A9/L0/001
locallyAvailable: YES
fileURL: file:///var/mobile/Media/DCIM/113APPLE/IMG_3404.MP4
width: 576
height: 1024
fileSize: 4664955
analysisType: unavailable
cplResourceType: Original
isCurrent: YES
})
newURL:
Optional(file:///var/mobile/Containers/Data/Application/E2792F47-142E-4601-8D5B-F549D03C9AFE/Documents/Untitled%2027228354.MP4)
resourceRequestOptions:
<PHAssetResourceRequestOptions: 0x28230d480>
Note: this is the decleration for the resource variable:
let resource = PHAssetResource.assetResources(for: (cell?.assetPH)!)
I have a solution to this! Swift 4+, tested on iOS 14!
I looked through using a PHAssetResourceRequest, but the file names were messed with in the process, and it generally didn't work with my sandbox. Then I also tried requesting a AVPlayerItem from the PHAsset but this too, did not work with sandboxing...
But then, I tried simply using PHAssetResourceManager.default().writeData(... and seemingly started working!
I tested a bit more and seemed to work, here is the full code:
let resource = PHAssetResource.assetResources(for: (cell?.assetPH)!)
let resourceRequestOptions = PHAssetResourceRequestOptions()
let newURL = ExistingMediaVC.newFileUrl
PHAssetResourceManager.default().writeData(for: resource.last!, toFile: newURL!, options: resourceRequestOptions) { (error) in
if error != nil {
print(error, "error")
} else {
print("good")
newUserTakenVideo.videoURL = newURL
}
}
It is quite simple!! Tell me if anything is not working, and note I still use the ExisitingMedia.fileURL variable you used in your original code as well :)

Swift: how to display bold words in textview without attributed string

We're adding the finishing touches to an app we're working on, and that apparently means putting an entire "Terms and Conditions" and "FAQs" section, formatting, bullets, breaks and all.
So I tried copy-pasting it into a textView with "editable" set to off, which kept the bullets, but not the bolded text.
Now, I've done attributed string before, and I have to say, I'm not sure it will be easy to do that on some 12-pages worth of paragraphs, bulleted lists and breaks that are likely to change in a few years or so.
So my question is, is there a way to do this without using attributed string?
Barring that, perhaps there's a way to loop through the text, and look for a written tag that will apply the attributes?
EDIT:
Update. It's been suggested I use HTML tags, and web view. That's what was done for the FAQs (which uses a label), I neglected to mention I tried that too.
For some reason, it just shows a blank textview, albeit a large-sized one, as if there's text in it (there isn't any). Strange that copy-pasting works but this doesn't.
Here's my code for it:
override func awakeFromNib() {
super.awakeFromNib()
termsTitle.text = "Terms and Conditions"
htmlContent = "<p style=\"font-family:Helvetica Neue\"><br/><strong><br/> BLA BLA BLA BLA BLA BLA x 12 Pages"
do {
let str = try NSAttributedString(data: htmlContent.dataUsingEncoding(NSUnicodeStringEncoding, allowLossyConversion: true)!, options: [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil)
termsTextView.attributedText = str
} catch {
print("Dim background error")
}
}
I'm pretty sure you can't do this in a Textview without using AttributedString. A possible solution would be using a WebView. Converting your "Terms and Conditions" and "FAQ" to HTML would probably be much easier than using an AttributedString.
If you still want to use your HTML in a UITextView you can try this function:
func getAttributedString(fileName: String) -> NSAttributedString? {
if let htmlLocation = NSBundle.mainBundle().URLForResource(fileName, withExtension: "html"), data = NSData(contentsOfURL: htmlLocation) {
do {
let attrString = try NSAttributedString(data: data, options: [NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType], documentAttributes: nil)
return attrString
} catch let err as NSError {
print("Attributed String Creation Error")
print(err.localizedDescription)
return nil
}
} else {
return nil
}
}
This function assumes you have a .html file in your main bundle. You pass it the name (minus extension) of the file (that should be in your project) and then use it like so:
textView.attributedText = getAttributedString("TermsAndConditions")
Just to clarify, the textView is a #IBOutlet on a View Controller in this example.
This function returns nil if either the .html file does not exist or the NSAttributedString conversion failed.

How to use ORKESerializer in my app?

I am working on developing my first ResearchKit App. I have been watching this video. One of the techniques used that is going to be helpful for me is serializing the results of a survey to JSON. The method used in the video is ORKESerializer.JSONDataForObject(taskResult). He explains that this is not a standard part of researchKit, but it was included in a test app, called ORKTest that is on GitHub.
I set up my taskViewController delegate just like he had it set on the video, like this:
extension ViewController : ORKTaskViewControllerDelegate {
func taskViewController(taskViewController: ORKTaskViewController, didFinishWithReason reason: ORKTaskViewControllerFinishReason, error: NSError?) {
switch reason {
case .Completed:
let taskResult = taskViewController.result
let jsonData = try! ORKESerializer.JSONDataForObject(taskResult)
if let jsonString = NSString(data: jsonData, encoding: NSUTF8StringEncoding) {
print(jsonString)
}
break
case .Failed, .Discarded, .Saved:
break
}
//Handle results with taskViewController.result
// let taskResult = taskViewController.result
taskViewController.dismissViewControllerAnimated(true, completion: nil)
}
}
I am getting this error upon compiling : use of unresolved identifier: ORKESerializer
So in the ORKTest app, in the GitHub files, I found 2 files. One called ORKESerialization.h, and one called ORKESerialization.m. I tried dragging those into my project, as I saw those files in the man's project in the video. And then that also prompted me to create a bridging header file as well, which I also saw in his project.
After doing that I am still getting the same error. The truth is I don't know exactly how to include these serialization packages with my app. Does anyone know how to included the right files so that I can implement this ORKEserialization method?
Thanks!
You need to import ORKESerialization.h in your bridging header:
#import "ORKESerialization.h"

Swift 2.0 Call Can Throw but is not marked with try

I'm trying to update the code in my app after the update to XCode 7 and it looks like I'm going to have to go through a serious learning curve again just to catch up. What am I doing wrong in the code below?
Is if let still being used?
I am so not familiar with try/catch outside of C#. I don't know how to use it in the context of swift and it'd be great to find an easy to understand guide that doesn't assume that I ever knew Objective C or have ever come across this before.
Use this instead:
do {
let json = try NSJSONSerialization.JSONObjectWithData(...)
return json
} catch let error as NSError {
print("JSON Error: \(error.localizedDescription)")
}
You are calling a method that throws a Swift error and as such, it needs to be marked with try.
do
{
let json = try NSJSONSerializer.JSONObjectWithData(...)
return json
}
catch
{
// By default the catch clause defines the variable error as whatever ws thrown
print("Error is \(error)")
return nil
}
Is pretty much what you want.
In case of my understanding is
let result: AnyObject? = try! NSJSONSerialization.JSONObjectWithData(jsonData!, options: NSJSONReadingOptions.MutableContainers)

Resources