I'm trying to save a file in macOS using Swift. I have the following settings in my app:
In the info.plist I have this key set with a message I need access to the desktop:
Privacy - AppleEvents Sending Usage Description
Though I never see the app make this request.
In my sandbox I have read/write set for User Selected File
I use NSOpenPanel to let the user select a file. The app then programmatically makes changes to the file selected. I display the changes in a textview. I then provide the option for the user to save the changes. This is done with a NSSavePanel and this code:
if let thisFileURL = currentFileURL {
guard let window = view.window else { return }
let panel = NSSavePanel()
panel.directoryURL = FileManager.default.homeDirectoryForCurrentUser
panel.nameFieldStringValue = thisFileURL.lastPathComponent
panel.title = "Saving changes...";
panel.showsResizeIndicator = true;
panel.showsHiddenFiles = false;
panel.canCreateDirectories = true;
panel.allowedFileTypes = ["html"]
panel.beginSheetModal(for: window) { (result) in
if (panel.runModal() == NSApplication.ModalResponse.OK), let saveFileURL = panel.url, let strToSave = self.strCleanContents {
do {
try strToSave.write(to: saveFileURL, atomically: true, encoding: .utf8)
} catch {
self.myAppManager.displayInfoAlert("Unmable to save file:\n\(error.localizedDescription)", self, [], false, response: {(bResponse:Bool) in })
}
}
}
currentFileURL is the URL to the file the user initially selected.
strCleanContents is the edited string from the file the user selected.
When I test this no error is thrown. But nothing happens either. The SavePanel opens, I can navigate to a location, if I select the same location where the original file was located it ask if I want to overwrite the file. Clicking save though does nothing, the panel just sits there. I can't cancel the process, it doesn't close, the file is not written.
beginSheetModal and runModal both display the Save Panel. Call either beginSheetModal to display the panel as a sheet or runModal to display the panel as a window.
Related
I want to set title for my NSOpenPanel, but it does not work! Apple says:
Gets and sets the title for the panel shown at the top of the window.
But I can not make codes works, not sure what is wrong?
let nsOpenPanel = NSOpenPanel()
nsOpenPanel.canChooseFiles = true
nsOpenPanel.title = "Hello, Choose Files"
if (nsOpenPanel.runModal() == .OK) ... // rest of codes ...
The title is something from the past when windows in macOS had title bars. To show text or an instruction on top of the NSOpenPanel object, use the message property:
nsOpenPanel.message = "Hello, Choose Files"
Trying to create a UITest for entering an email address/password into text fields and then hitting a login button. Using Xcode.
I watch the UITest navigate to the proper page, but it does not detect the text fields. I made sure that both text fields have the "Accessibility" box checked, and I've given them identifiers.
I'm getting this error:
UI Testing Failure - No matches found for text field
func testExample() {
let app = XCUIApplication()
//Test login page
app.buttons["loginButton"].tap()
//Here it navigates to the desired page
let emailField = app.textFields["email"]
emailField.tap()
emailField.typeText("xxx.xx.edu")
let passwordField = app.textFields["password"]
passwordField.tap()
passwordField.typeText("xxx")
app.buttons["loginButton"].tap()
// let loginAlert = app.alerts["alertVC"]
// XCTAssertEqual(loginAlert.title, "Error")
}
The accessibility indicators are "email" and "password" respectively. Logging in works fine when I run the simulator myself.
EDIT: Apparently there are no text fields at all (count = 0) even though I see them and use them on the page...
Image of storyboard with text fields:
If UITextFiled set SecureTextEntry true, you should use:
let passwordField = app.secureTextFields["password"]
I was having the same problem and I solved by unchecking the "accessibility enabled" box for the "views" of ViewControllers
I'm trying to save an image to the photo library in Swift 3 (I'm working with Xcode 8).
ViewController Code:
func shareImage(image: UIImage) {
let items = [image]
var activityVC: UIActivityViewController = UIActivityViewController(activityItems: items, applicationActivities: nil)
let excludeActivities: [UIActivityType] = [UIActivityType.airDrop,
UIActivityType.assignToContact,
UIActivityType.addToReadingList,
UIActivityType.copyToPasteboard]
activityVC.excludedActivityTypes = excludeActivities
self.present(activityVC, animated: true, completion: nil)
}
When I run the application, and click on the button to take the screenshot (converting it to image, ..., that's all working perfectly), the app asks for permission to access the photo library, I tap the "OK" button, and then the app crashes. The image is not saved in the photo library.
The only clue I get from Xcode is the following:
2016-09-28 11:24:27.216043 Ajax Kids[4143:1545362] [error] error: -addPersistentStoreWithType:SQLite configuration:(null) URL:file:///var/mobile/Media/PhotoData/Photos.sqlite?readonly_shm=1 options:{
NSPersistentStoreFileProtectionKey = NSFileProtectionCompleteUntilFirstUserAuthentication;
NSReadOnlyPersistentStoreOption = 1;
NSSQLitePersistWALOption = 1;
NSSQLitePragmasOption = {
"journal_mode" = WAL;
};
} ... returned error Error Domain=NSCocoaErrorDomain Code=256 "The file couldn’t be opened." UserInfo={reason=Failed to access file: 1} with userInfo dictionary {
reason = "Failed to access file: 1";
}
2016-09-28 11:24:27.216433 Ajax Kids[4143:1545362] [Migration] Unexpected error opening persistent store <private>, cannot attempt migration <private>)
2016-09-28 11:24:27.216568 Ajax Kids[4143:1545362] [Migration] Failed to open store <private>. Requires update via assetsd (256: <private>)
Does anyone have any idea how to fix this?
Thanks in advance!
UPDATE
Sharing the image on Social Media works fine, so the problem is specified to saving the image in the photo library.
Add new records in your new InfoPlist.strings file.
<key>NSPhotoLibraryAddUsageDescription</key>
<string>$(PRODUCT_NAME)</string>
UPD: iOS 11 key
On iOS 11, there is a new property called NSPhotoLibraryAddUsageDescription, similar to NSPhotoLibraryUsageDescription. See https://developer.apple.com/library/content/documentation/General/Reference/InfoPlistKeyReference/Articles/CocoaKeys.html
Try to force request permissions like this:
PHPhotoLibrary.requestAuthorization { status in
if status == .authorized {
//do things
}
}
do not forget import Photos. Hope this helps.
I found the culprit in my particular case. We are using Leanplum for analytics and push notifications. The Leanplum.syncResourcesAsync method was causing the opening of the photo library to crash. It took a couple of days to find as I wasn't aware Leanplum was doing anything to hook into a user's photo library... which in itself is concerning.
We weren't using the functionality this particular method brings, so were able to just remove the method call and the photo library stopped crashing.
I am using the latest version of TinyMCE and I would like to integrate Google Drive Picker whenever an user clicks on Insert Image.
From the TinyMCE documentation I saw that I should use the file_browser_callback parameter but I am stuck with a couple of problems. First of all, I managed to attach the Google Picker but the Insert Image popup stays on top and there's no way for me to select a file. Even if I solve this problem, how can I set the textbox value from the Google Picker callback function? Below you can see my code, Google Picker code is pretty standard so I won't paste it.
var picker;
tinymce.init({
//other init parameters...
file_browser_callback: function(field_name, url, type, win) {
picker.setVisible(true);
win.document.getElementById(field_name).value = 'my browser value';
}
});
function createPicker() {
// Here I build the Picker...
// var picker = ...
}
function pickerCallback(data) {
//TODO: Find a way to set textbox value with the URL of the Image selected from Drive
}
I think you can hide the modal when you call createPicker():
function createPicker() {
document.getElementById('modal_id').style.display = 'none';
// your picker code here!
}
and then display it back once you get the callback i.e. pickerCallback(data):
function pickerCallback(data) {
document.getElementById('modal_id').style.display = 'block';
var url = 'nothing';
if (data[google.picker.Response.ACTION] == google.picker.Action.PICKED) {
var doc = data[google.picker.Response.DOCUMENTS][0];
url = doc[google.picker.Document.URL];
}
// set the input text value with the URL:
document.getElementById('source_input_id').value = url;
}
Change modal_id to your modal's div id and source_input_id with your source input field's id.
The purpose of the following function is to allow the user to save the edited page properties in the CQ-Dialog even though if they invalid by clicking on the button save anyway:
PageProperties.showMsg = function(dialog, config, errorMessage) {
CQ.Ext.MessageBox.buttonText.ok = "save anyway";
CQ.Ext.Msg.show({
title : "Completeness check failed",
msg : errorMessage,
buttons: CQ.Ext.Msg.OKCANCEL,
fn : function(buttons) {
if(buttons == "ok") {
dialog.form.items.each(function(field) {
// clear fields with emptyText so emptyText is not submitted
if (field.emptyText && field.el && field.el.dom && field.el.dom.value == field.emptyText) {
field.setRawValue("");
}
});
var action = new CQ.form.SlingSubmitAction(dialog.form, config);
dialog.form.isValid = function() {
return true;
};
dialog.form.doAction(action);
dialog[dialog.closeAction]();
CQ.Util.reload();
}
}
});
};
This functions works fine from the sidekick. When I click on save anyway all current values of the page properites are stored regardless if they are valid or not. This does not work from the site admin. when I call the page properties of the same page from the site admin and try to save the page properties with invalid values by clicking on save anyway, this does not works (old values are stored and nothing changes).
I hope somebody can help. thank you
I found the solution. the problem was the function CQ.Util.reload(). It prevent storing the values