Saving a PDF file to application documents folder, swift iOS [duplicate] - ios

I'm trying to show my data in my app in the Files app on my iPhone
I searched a lot and did everything right, I don't know where the error is
func fileManger(nameFolder: String) {
let manager = FileManager.default
let DecomentFolder = manager.urls(for: .documentDirectory, in: .userDomainMask).last
let Folder = DecomentFolder?.appendingPathComponent("\(nameFolder)")
do {
try manager.createDirectory(at: Folder!, withIntermediateDirectories: true, attributes: [:])
} catch let error {
print(error.localizedDescription)
}
}
Also here I am sending the value to be a folder
#objc func alertNewFolder () {
let alert = UIAlertController(title: "Create an album", message: "", preferredStyle: .alert)
alert.addTextField(configurationHandler: nil)
alert.textFields![0].placeholder = "name album"
alert.textFields![0].textAlignment = .right
alert.addAction(UIAlertAction(title: "cancel", style: .cancel, handler: nil))
alert.addAction(UIAlertAction(title: "save", style: .default, handler: { ـ in
if let textFileds = alert.textFields {
let links = textFileds[0].text
self.arrS1.append(addCatogrey(nameCatog: links!, imageSection: UIImage(named: "folder")))
// Here Send FileManager
helperCoding().fileManger(nameFolder: links!)
self.collection.reloadData()
}
}))
self.present(alert, animated: true, completion: nil)
}
In the Simulators it saved in the Documents folder here correctly
/Users/badrshammry/Library/Developer/CoreSimulator/Devices/7986A27F-7026-45E1-9073-78CCD6A9B90A/data/Containers/Data/Application/3173C4DC-BCDE-41B9-89E1-6E8D9B52EF25/Documents

If you would like to expose your App document files inside Apple's Files App you need to include the "Supports Document Browser" key in your info plist file and set it to YES:

Add these two keys to 'info.plist' -
...
<key>UIFileSharingEnabled</key>
<true/>
<key>LSSupportsOpeningDocumentsInPlace</key>
<true/>
...
After this, files saved in '.documentDirectory' will appear in 'Files App' inside a folder with your app name.
If editing 'info.plist' in Xcode then add below keys -
Application supports iTunes file sharing = YES
Supports opening documents in place = YES
info.plist

To set Supports Document Browser has to be set in Custom iOS Target Propertiesin the info tab
info.plist is no more from Xcode 13
Anyhow, it does not work. The files App will not find files in the your app's bundle. They need to be "shared" (In SwiftUI use UIViewControllerRepresentable)
But your App will not have a directory in the file structure visible to the Files App. Adding that folder is still a mystery
See: How can folders visible to FIles app be created by App programatically

Related

Create directory for App available to Files app [duplicate]

I'm trying to show my data in my app in the Files app on my iPhone
I searched a lot and did everything right, I don't know where the error is
func fileManger(nameFolder: String) {
let manager = FileManager.default
let DecomentFolder = manager.urls(for: .documentDirectory, in: .userDomainMask).last
let Folder = DecomentFolder?.appendingPathComponent("\(nameFolder)")
do {
try manager.createDirectory(at: Folder!, withIntermediateDirectories: true, attributes: [:])
} catch let error {
print(error.localizedDescription)
}
}
Also here I am sending the value to be a folder
#objc func alertNewFolder () {
let alert = UIAlertController(title: "Create an album", message: "", preferredStyle: .alert)
alert.addTextField(configurationHandler: nil)
alert.textFields![0].placeholder = "name album"
alert.textFields![0].textAlignment = .right
alert.addAction(UIAlertAction(title: "cancel", style: .cancel, handler: nil))
alert.addAction(UIAlertAction(title: "save", style: .default, handler: { ـ in
if let textFileds = alert.textFields {
let links = textFileds[0].text
self.arrS1.append(addCatogrey(nameCatog: links!, imageSection: UIImage(named: "folder")))
// Here Send FileManager
helperCoding().fileManger(nameFolder: links!)
self.collection.reloadData()
}
}))
self.present(alert, animated: true, completion: nil)
}
In the Simulators it saved in the Documents folder here correctly
/Users/badrshammry/Library/Developer/CoreSimulator/Devices/7986A27F-7026-45E1-9073-78CCD6A9B90A/data/Containers/Data/Application/3173C4DC-BCDE-41B9-89E1-6E8D9B52EF25/Documents
If you would like to expose your App document files inside Apple's Files App you need to include the "Supports Document Browser" key in your info plist file and set it to YES:
Add these two keys to 'info.plist' -
...
<key>UIFileSharingEnabled</key>
<true/>
<key>LSSupportsOpeningDocumentsInPlace</key>
<true/>
...
After this, files saved in '.documentDirectory' will appear in 'Files App' inside a folder with your app name.
If editing 'info.plist' in Xcode then add below keys -
Application supports iTunes file sharing = YES
Supports opening documents in place = YES
info.plist
To set Supports Document Browser has to be set in Custom iOS Target Propertiesin the info tab
info.plist is no more from Xcode 13
Anyhow, it does not work. The files App will not find files in the your app's bundle. They need to be "shared" (In SwiftUI use UIViewControllerRepresentable)
But your App will not have a directory in the file structure visible to the Files App. Adding that folder is still a mystery
See: How can folders visible to FIles app be created by App programatically

Swift Custom URL is not opening the app from extension

I have a Share-Extension from which I would like to redirect the user to the main app. This is what I tried:
let signInAction = UIAlertAction(title: "Anmelden", style: .default) { (alert) in
let myAppUrl = NSURL(string: "open://")!
self.extensionContext?.open(myAppUrl as URL, completionHandler: { (success) in
if (!success) {
// let the user know it failed
print("fail!")
}
})
}
And my info-plist :
Right now it is printing the "fail!". What am I doing wrong here?
Have a look at your project settings in xcode.
Did you set a custom url scheme in the "URL-Types" setting in your apps target?
Xcode - Target - General Settings - URL Types

Possible to programmatically tap textfields and paste username/passwords? Login not saved in Keychain

I am trying to help my boss and co-workers out with an iOS 12 Swift App to help us all better manage inventory when out on the shop floor.
The all works by taking advantage of the barcode/QR-code reading technology that comes bundled with iOS 12.
The webpage used to log into the inventory service uses plain-old http technology and so the iOS (appears) not to be offering me a means of letting me save/retrieve the password to/from the Keychain. I CAN however save the login information when I access the website via my Mac. I have verified that the login information been saved on the Mac's keychain (which is shared with the iPhone via iCloud).
The webpage warns that the password (on either platform) will be sent unencrypted.
I have adapted some of the code already at https://www.appcoda.com/barcode-reader-swift/.
I don't have a full Apple developer account, so at present this is not a production app.
The main difference between the code supplied by www.appcoda.com/barcode-reader-swift and mine nearly all lies in the 'QRSwiftController.swift' file's func launchApp function. Slight modifications to help improve readability and usability. A Safari View Controller is used make everything more cohesive inside of one app etc.
func launchApp(decoded_barcode: String) {
if presentedViewController != nil {
return
}
AudioServicesPlayAlertSound(SystemSoundID(kSystemSoundID_Vibrate))
let result = decoded_barcode.replacingOccurrences( of:"[^0-9]", with: "", options: .regularExpression)
let startOfURL: String = "http://xxx.webname.com/deploy/artikel.php?nummer="
let fullURL = startOfURL + result
let alertPrompt = UIAlertController(title: "Open App", message: "You're going to open \(fullURL)", preferredStyle: .actionSheet)
let confirmAction = UIAlertAction(title: "Confirm", style: UIAlertActionStyle.default, handler: { (action) -> Void in
if let url = URL(string: fullURL){
let safariVC = SFSafariViewController(url: url)
self.present(safariVC, animated: true, completion: nil)
}
})
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler: nil)
alertPrompt.addAction(confirmAction)
alertPrompt.addAction(cancelAction)
present(alertPrompt, animated: true, completion: nil)
}
Even when I visit the inventory management website via normal Safari on my iPhone I am prompted for a username/password every time.
Alt URL for login screen https://imgur.com/a/S2EMYcE
Is there any way I possibly circumvent this problem by automating the tapping of the text fields for the username/password (following by pasting the info) and then tapping 'Log in'??
I've looked already for solutions on Stackoverflow but they seem overwhelmingly complicated and/or no longer valid for Swift 4.
Thanks!
Assume generic username and passwords such as 'user' and 'password'.
In the end (as a workaround) I just got Google Chrome (on iOS) to do all the donkey-work (it appears willing to save the password for this website for future retrieval).
if URL(string: fullURL) != nil {
//let safariVC = SFSafariViewController(url: url)
//self.present(safariVC, animated: true, completion: nil)
UIApplication.shared.openURL(NSURL(string: fullURL)! as URL)
}

Your app uses the “prefs:root=” non public URL scheme. Best plan to update old code? [duplicate]

This question already has answers here:
Is it considered a private API to use App-prefs:root?
(4 answers)
Closed 4 years ago.
so I posted previously about this issue here.
As you can read I was rejected.
My question is how do I update this code to use openSettingsURLString instead of UIApplication.shared.openURL
After doing some asking and some research I found out I have to change this:
static func openSettingsAlert(_ title: String, message: String, settingsURL: String) -> UIAlertController {
let alertController = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
let settingsAction = UIAlertAction(title: "Settings", style: .default) { (_) -> Void in
let settingsURL = URL(string: settingsURL)
if let url = settingsURL {
DispatchQueue.main.async(execute: {
UIApplication.shared.openURL(url)
})
}
}
let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: nil)
alertController.addAction(cancelAction)
alertController.addAction(settingsAction)
return alertController
}
into code that uses this
But I'm so confused because I can't find that class anywhere, and if it's a class why is isn't the class capitalized?
How can I use it in the code snippet above.
I think it is a subclass of String, but I have no idea where to find this, or how to incorporate the change into the existing code safely.
Very confused on this and thank you for any insights.
I'm getting a Type 'UIApplication' has no member 'openSettingsURLString'
Well, you can see from the documentation that the type UIApplication does have this member. Here it is:
https://developer.apple.com/documentation/uikit/uiapplication/1623042-opensettingsurlstring
It is a static read-only property of UIApplication. (That is why it is not capitalized; it is a property.) And it is used like this:
let url = URL(string: UIApplication.openSettingsURLString)!
UIApplication.shared.open(url)
If you are not able to use it that way, it must be because you are using an outdated version of Swift. In that case, you would write:
let url = URL(string:UIApplicationOpenSettingsURLString)!
UIApplication.shared.open(url)
However, you should not be using such an outdated version of Swift. It is no crime, to be sure, but it certainly makes communication with the rest of us here on Stack Overflow difficult. By now, Xcode 10 and Swift 4.2 have been available for a long time, and you should upgrade to them so as to be on the same page with everyone else. Otherwise you're just trying to talk a different language from the rest of us — an older language that most of us have already forgotten. Not to mention the fact that you are out of sync with the online documentation, which does not show linguistic terms for older versions of the Swift language.

Swift: NSLocalizedString(key, string) not working with AlertController - how to use the right way?

I am trying to localize my app and I have set up under the Info tab a localization for two languages. Xcode has created under Main.storyboard two new files called Main.strings (English) and Main.strings (German).
Now when I change the different elements within those files (the ones Xcode has created for buttons, labels, titles etc.) everything works fine.
But I need to localize an alert controller as well. Here is what I did, but this doesn't work and I don't know why, because as far search engines help, I did it the right way:
In the Main.strings files for both languages I added this under the Xcode created elements at the bottom of the file:
"alert" = "Nothing to Share!";
"message" = "You must enter a value and hit SPLIT to share with your friends.";
"cancel" = "Cancel";
(and obviously the same for the German Main.Strings file)
In my viewController I added the alert controller like this:
let alertTitle = NSLocalizedString("alert", comment: "")
let alertMessage = NSLocalizedString("message", comment: "")
let cancelButtonText = NSLocalizedString("cancel", comment: "")
let alert = UIAlertController(title: alertTitle, message: alertMessage, preferredStyle: .alert)
let cancelAction = UIAlertAction(title: cancelButtonText, style: UIAlertActionStyle.cancel, handler: nil)
alert.addAction(cancelAction)
present(alert, animated: true, completion: nil)
The alert controller works fine, but it displays the keys I added for NSLocalizedString. So in this case the controller displays:
alert
alertmessage
cancel
But Xcode doesn't pull the corresponding strings from the keys I added in both Main.Strings files. If I would change the key "alert" to "test" the controller would display the word test instead.
So what am I doing wrong here and how to solve this?
Do I have to specify in the Main.strings files something I did not or do I have to link to these files? Since changing all the Xcode generated elements in Main.strings works, I though this process is completely automatic?
Thanks.

Resources