how to check if file exist use swift, rewrite from objective c - ios

how to rewrite this objective-c language to swift?
NSString *filePath = #"/Applications/MySample.app";
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath])
{
// avoid open add friend
}
regards.

Equivalent Swift 3 Code:
let filePath = "/Applications/MySample.app"
if (FileManager.default.fileExists(atPath: filePath)) {
// avoid open add friend
}
Swift 2
let filePath = "/Applications/MySample.app"
if (NSFileManager.defaultManager().fileExistsAtPath(filePath))
{
// avoid open add friend
}

Some years after the question has been asked I recommend to take rewrite literally and use the URL related API
let fileURL = URL(fileURLWithPath:"/Applications/MySample.app")
if let _ = try? fileURL.checkResourceIsReachable() {
// file exists
}

let path = "/Applications/MySample.app"
let hasFile = FileManager().fileExists(atPath: path)
if hasFile {
// use file
}
else {
// possibly inform user the file does not exist
}

Related

How to read file as String in Flutter that has been written to filesystem using NSKeyedArchiver in iOS?

After updating iOS native app with an app written on Flutter I want to read a file from filesystem on iOS device using Dart. The file I want to read has been previously written to filesystem using this ObjectiveC code:
- (void)setAccount:(FTAccountModel *)account {
_account = account;
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
path = [path stringByAppendingPathComponent:AccountModelPath];
if (account) {
NSArray * array = #[account];
[array writeToFile:path atomically:YES];
[NSKeyedArchiver archiveRootObject:array toFile:path];
}
}
I've tried the following approach in Flutter using path_provider package:
final appDocDir = await getApplicationDocumentsDirectory();
final accountDataFile = File('${appDocDir.path}/$_iosAccountDataFile');
String contents = await accountDataFile.readAsString();
print("contents: $contents");
But I get an error when calling readAsString() method:
FileSystemException: Failed to decode data using encoding 'utf-8', path = '/var/mobile/Containers/Data/Application/FBCB4862-E5EA-4C93-8C2E-3DF1F00A8645/Documents/AccountModel.data'
How to read file on iOS device using Dart and Flutter, that has been written using NSKeyedArchiver?
As of writing this answer, there are no plugins to read the file, that has been previously written to the filesystem using NSKeyedArchiver in iOS. The way to read the file is to write custom platform-specific code.
So the iOS platform code on Swift will be something like the following:
private func callGetUserIdFromNativeApp(result: #escaping FlutterResult) {
var account: FTAccountModel?
let fm = FileManager.default
let urls = fm.urls(for: .documentDirectory, in: .userDomainMask)
if (!urls.isEmpty) {
let file = urls[0].appendingPathComponent("Accounts.data", isDirectory: false)
if (fm.fileExists(atPath: file.path)) {
if let accountArray: [FTAccountModel] = NSKeyedUnarchiver.unarchiveObject(withFile: file.path) as? [FTAccountModel] {
if (!accountArray.isEmpty) {
account = accountArray[0]
}
}
}
}
if let userId: Int = account?.userId {
result(String(userId))
} else {
result(nil)
}
}
And the flutter part will use MethodChannel to invoke the native code:
static const MethodChannel _channel = const MethodChannel("CHANNEL_NAME");
static Future<String> getUserIdFromNativeIos() async {
try {
return await _channel.invokeMethod("METHOD_NAME");
} catch (e){
return _failedString();
}
}

How to manually remove Cocoalumberjack log

I am using CocoaLumberjack V2.4 to save log into file.
This is the default code to set saving log into file:
DDFileLogger *fileLogger = [[DDFileLogger alloc] init]; // File Logger
fileLogger.rollingFrequency = 60 * 60 * 24; // 24 hour rolling
fileLogger.logFileManager.maximumNumberOfLogFiles = 7;
[DDLog addLogger:fileLogger];
Log is saved in default place: /AppData/Library/Caches/Logs/
I want implement an API to remove the saved log manually. I check they have Manually clear logs as the open issue there. Anyone has suggestion?
This should work for you:
[fileLogger rollLogFileWithCompletionBlock: ^{
for (NSString *filename in fileLogger.logFileManager.sortedLogFilePaths) {
[[NSFileManager defaultManager] removeItemAtPath:filename error:nil];
}
}];
Swift Answer:
fileLogger.rollLogFile(withCompletion: {
for filename: String in self.fileLogger.logFileManager.sortedLogFilePaths {
do {
try FileManager.default.removeItem(atPath: filename)
} catch {
print(error.localizedDescription)
}
}
})
Try this one in Swift.
func clearLogs(){
let logHelper = LogHelper(subsystem: "Logs", category: "Clear")
logHelper.i("Clearing logs...")
for logger in DDLog.allLoggers {
if let fileLogger = logger as? DDFileLogger {
fileLogger.rollLogFile{
for path in fileLogger.logFileManager.sortedLogFilePaths {
let fileURL = URL(fileURLWithPath: path)
logHelper.i("\(fileURL)")
do {
try FileManager.default.removeItem(at: fileURL)
logHelper.i("Done!")
}
catch {
logHelper.e(error.localizedDescription)
}
}
}
}
}
}

Changing advice language doesn't work

I'm new with Skobbler SDK and learn with the Swift Demo + the well documented tuto (http://developer.skobbler.com/getting-started/ios#sec01)
However, I still can't configure the advice language settings using their instructions ...
Here is my code :
let settings = SKAdvisorSettings()
settings.advisorVoice = "fr"
settings.language = SKAdvisorLanguage.FR
settings.advisorType = SKAdvisorType.AudioFiles
settings.resourcesPath = NSBundle.mainBundle().resourcePath! + "/SKMaps.bundle/AdvisorConfigs/Languages"
The event is define by :
func routingService(routingService: SKRoutingService!, didChangeCurrentAdvice currentAdvice: SKRouteAdvice!, isLastAdvice: Bool) {
NSLog("New advice "+currentAdvice.adviceInstruction)
}
Si I get "in 90 meters turn right " for instance.
By the way, no audio files are played neither
Could you please give me a hand :) ? Thank you in advance
There is a bug in the code that is supposed to "play the audio advice" (in AudioService.m) - the name of the .mp3 file was not correctly built.
I've fixed this by making the following change:
func playAudioFile(audioFileName: String) {
var soundFilePath: String = audioFilesFolderPath + "/" + audioFileName
soundFilePath = soundFilePath + ".mp3"
if !NSFileManager.defaultManager().fileExistsAtPath(soundFilePath)
{
return
}
else
{
audioPlayer = try? AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: soundFilePath), fileTypeHint: nil)
audioPlayer.delegate = self
audioPlayer.play()
}
}
This affected only the swift demo and will be fixed in the next update
Ok I found my mistake by replacing :
settings.advisorType = SKAdvisorType.AudioFiles
with
settings.advisorType = SKAdvisorType.TextToSpeech
However I still don't know how to use prerecorded files ... Even with the section "Using prerecorded files in tuto ...
Did you set your settings as the advisorConfigurationSettings of your SKRoutingService?
[SKRoutingService sharedInstance].advisorConfigurationSettings = advisorSettings;
You will also have to set the path for the audio files like this:
NSBundle* advisorResourcesBundle = [NSBundle bundleWithPath:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"SKAdvisorResources.bundle"]];
NSString* soundFilesFolder = [advisorResourcesBundle pathForResource:#"Languages" ofType:#""];
NSString* audioFilesFolderPath = [NSString stringWithFormat:#"%#/%#/sound_files",soundFilesFolder,userLanguageCode];
[AudioService sharedInstance].audioFilesFolderPath = audioFilesFolderPath;
userLanguageCode would be "fr" in your case

PHAsset get original file name

I wonder if there any way to get the original file name using PHAsset?
I use the following code to extract the file info.
[[PHImageManager defaultManager] requestImageDataForAsset:asset options:requestOption resultHandler:^(NSData *imageData, NSString *dataUTI, UIImageOrientation orientation, NSDictionary *info) {
entity.fileUrl = [info objectForKey:#"PHImageFileURLKey"];
entity.filename = [[NSFileManager defaultManager] displayNameAtPath:[ entity.fileUrl path]];
}];
However, It doesn't return original name but the name in the format "img_123"
I've just checked official apple docs . there has been introduced a new class PHAssetResource and the property originalFilename which is available in the iOS 9+. The problem is that I use the image picker library CTAssetsPickerController which 's based on the Photos framework; it returns picked image as the PHAsset object . PS. I'm looking for the solution which is compatible with iOS 8 :).
Thank you!
On iOS 8 your solution is the right (and only approach) to get a filename at all.
On iOS 9 this works:
NSArray *resources = [PHAssetResource assetResourcesForAsset:asset];
NSString *orgFilename = ((PHAssetResource*)resources[0]).originalFilename;
Short way to get file name with one line of code. Asset have a property for accessing file name.
NSString*FileName=[asset valueForKey:#"filename"];
NSLog(#"File name %#",FileName);
Its done.
Note: Accepted answer takes lots of time to load a phasset but it works.
I had to modify my code because it started returning nonsense names. My solution was to pick the resource based on asset's mediaType and resource's type, but maybe there is something easier:
extension PHAsset {
var primaryResource: PHAssetResource? {
let types: Set<PHAssetResourceType>
switch mediaType {
case .video:
types = [.video, .fullSizeVideo]
case .image:
types = [.photo, .fullSizePhoto]
case .audio:
types = [.audio]
case .unknown:
types = []
#unknown default:
types = []
}
let resources = PHAssetResource.assetResources(for: self)
let resource = resources.first { types.contains($0.type)}
return resource ?? resources.first
}
var originalFilename: String {
guard let result = primaryResource else {
return "file"
}
return result.originalFilename
}
}
Maybe you can use the method, it works above iOS8:
[asset requestContentEditingInputWithOptions:options completionHandler:^(PHContentEditingInput *contentEditingInput, NSDictionary *info) {
CIImage *fullImage = [CIImage imageWithContentsOfURL:contentEditingInput.fullSizeImageURL];
NSLog(#"%#",contentEditingInput.fullSizeImageURL);//get url
NSLog(#"%#", fullImage.properties.description);//get {TIFF}, {Exif}
}];
#holtmann solution written in Swift
let resource = PHAssetResource.assetResources(for: asset)
let filename = resource.first?.originalFilename ?? "unknown"

Load file in Today extension when device is locked

In my today extension with my device unlocked, this line of code works as expected, returning the data from the image path:
let imageData = NSData(contentsOfFile: path)
However when my device is locked with a passcode, it returns nil. Is there any way to access images in the file system when the device is locked? I can access UserDefaults just fine, but not files in the directory for my shared group. Here is how I am creating the path, calling imagePath, which is correctly populated with the path I expect in both cases:
func rootFilePath() -> String? {
let manager = NSFileManager()
let containerURL = manager.containerURLForSecurityApplicationGroupIdentifier(GROUP_ID)
if let unwrappedURL = containerURL {
return unwrappedURL.path
}
else {
return nil
}
}
func imagePath() -> String? {
let rootPath = rootFilePath()
if let uPath = rootPath {
return "\(uPath)/\(imageId).png"
}
else {
return nil
}
}
I just figured it out! You need to set the file permissions accordingly:
NSFileManager *fm = [[NSFileManager alloc] init];
NSDictionary *attribs = #{NSFileProtectionKey : NSFileProtectionNone};
NSError *unprotectError = nil;
BOOL unprotectSuccess = [fm setAttributes:attribs
ofItemAtPath:[containerURL path]
error:&unprotectError];
if (!unprotectSuccess) {
NSLog(#"Unable to remove protection from file! %#", unprotectError);
}
In many cases you wouldn't normally want to do this, but because the information is intended to be viewed from the lock screen, I'm OK with removing file protection.

Resources