Posts

Showing posts from April, 2018

How to copy the file from appBundle to document directory iOS swift?

Solution: If you want copy the file that you had already added to your bundle to the document directory then follow the code.Below i had copied the first.wav file from bundle directory to document directory. Code:         let destPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!         let fileManager = FileManager.default         //beep sound copy         let bundlePath = Bundle.main.path(forResource: "first", ofType: ".wav")         let fullDestPath = NSURL(fileURLWithPath: destPath).appendingPathComponent("first.wav")         do{             try fileManager.copyItem(atPath: bundlePath!, toPath: (fullDestPath?.path)!)         }catch{             print("\n")             print(error)         }

Audio recording sometime shows error at first time iOS swift

Solution: First you have to get the userinput for recording audio from userpermissions.If you put the below code when you try recording sometimes it will crash.So must have to add the code inside the appdelegate didfinishlaunching and it will works good. let session:AVAudioSession = AVAudioSession.sharedInstance()         // ios 8 and later         if (session.responds(to: #selector(AVAudioSession.requestRecordPermission(_:)))) {             AVAudioSession.sharedInstance().requestRecordPermission({(granted: Bool)-> Void in             })}

How to upload a build version without change the certificates in my app for bot development and testing iOS swift?

Solution: Xcode has the powerful tool for developers.It had an option to set the debug and deployment mode for developers. So you can set the development certificate to the debug mode and distribution certificate to the release mode. It will works in both the sides if you run the app to the device and also archieve the build and upload to the appstore no headaches to change the certificates in the xcode.Blast the door and enjoyyy...

How to create a folder or directory in the documentdirectory iOS swift?

Solution: Here the below code for creating a folder inside the file directory.The below code is used for creating sounds folder inside filedirectory. let fileManager = FileManager.default                         let documentDirectoryURL = FileManager.default.urls(for: .libraryDirectory, in: .userDomainMask).first! as NSURL             var newUrl:NSURL!             let newDir = documentDirectoryURL.appendingPathComponent("Sounds")?.path                         do{                 try fileManager.createDirectory(atPath: newDir!,withIntermediateDirectories: true, attributes: nil)                 newUrl = documentDirectoryURL.appendingPathComponent("Sounds") as! NSURL             } catch {                 print("Error: \(error.localizedDescription)")             } If you want to create the same folder in document directory then below the steps let fileManager = FileManager.default                         let documentDirectoryURL = FileManager.default

Unnotification with timedelay sometimes not working iOS swift

Solution: I had faced the problem that i had give time for unnotification at a particular time like now or after few seconds.but sometimes it was not triggered.So i changed the code from let trigger = UNCalendarNotificationTrigger(dateMatching: triggerDate,                                                repeats: true) To         let trigger = UNTimeIntervalNotificationTrigger(timeInterval: TimeInterval(delay),                                                         repeats: false) It was working good. If you want to trigger a notification to current time use the above code or if you want to trigger a notification at next hour or some date then use the dae based trigger.

How to get if the file with same name already exists on document directory iOS swift?

Solution: The below code used for check if the file with the same name already exists in the document directory.And it will remove the existing file. code : if fileManager.fileExists(atPath: mergeAudioFiles.path!) {             do{                 try fileManager.removeItem(atPath: path!)             } catch {                 //Do nothing             }         }

Show custom tone as notification tone that saved from document directory iOS swift

Solution: If you want to show the notification with custom tone that you had saved in document directory then follow the steps to play the tone at notification time.But it had difficult so you just save the audio in libraryDirectory and show. The below code is used for write the file in the librarydirectory with the folder named sound.You must have to create a directory to the library and save the file     let fileManager = FileManager.default          let documentDirectoryURL = FileManager.default.urls(for: .libraryDirectory, in: .userDomainMask).first! as NSURL         var newUrl:NSURL!         let newDir = documentDirectoryURL.appendingPathComponent("Sounds")?.path                 do{             try fileManager.createDirectory(atPath: newDir!,withIntermediateDirectories: true, attributes: nil)             newUrl = documentDirectoryURL.appendingPathComponent("Sounds") as! NSURL         } catch {             print("Error: \(error.localizedDescription)&q

Saved Image in document directory and save path in coredata not able to fetch the image file iOS swift

Solution: If you saved the entire path like file://....png then it is not possible to get the image because document directory path will be dynamic.So better you can save the imagename in your coredata after that below code to get the image. Code: let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]       let urls = documentsDirectory.appendingPathComponent(filename) it will return the url of the image.You can get the image and enjoyyy....

Combine audioFiles iOS

Solution:  You can combine audio files in swift also.You can pass the audiofile paths to one function it can combine those files and save it as single file. Code:      func combineAudioFiles(audioFileUrls: NSArray,place:MyLocations,isOnEntry:Bool) {         let composition = AVMutableComposition()         for i in 0 ..< audioFileUrls.count {             let compositionAudioTrack :AVMutableCompositionTrack = composition.addMutableTrack(withMediaType: AVMediaTypeAudio, preferredTrackID: CMPersistentTrackID())             let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]             let urls = documentsDirectory.appendingPathComponent(audioFileUrls[i] as! String)             let asset = AVURLAsset(url: urls)             let track = asset.tracks(withMediaType: AVMediaTypeAudio).first             let timeRange = CMTimeRange(start: CMTimeMake(0, 600), duration: (track?.timeRange.duration)!)                    try! compositionAudi

Custom Navigation bar with bar attributes || Change navigationBar color swift || iOS NavigationBar color not changed || Backbutton position change in UINavigationBar iOS

Solution: Here we can change the entire app navigation bar titlecolor,font and fontSize in swift.The code as follows.You can add the code in the appdelegate and get the custom navigation Bar.Also you can change the backButton position in the navigationBar let style = NSMutableParagraphStyle()         style.lineBreakMode = .byTruncatingHead         let attrs = [NSParagraphStyleAttributeName: style,NSForegroundColorAttributeName:UIColor.white,NSFontAttributeName:UIFont(name: "Trebuchet MS", size: 20)]         UINavigationBar.appearance().titleTextAttributes = attrs  UIBarButtonItem.appearance().setBackButtonTitlePositionAdjustment(UIOffsetMake(-200, -5), for: .default)         UINavigationBar.appearance().tintColor = UIColor.white