Posts

Showing posts from January, 2017

How to use the objective c files inside the swift project?

Image
Hai friends , if you confused for using the objective c files already you had but you had developed the app with swift.Then don't worry, apple had already the setup for the migration. Just you can drag and drop the objective c files inside your swift project it says an alert to create a Bridging header.After that you must select the yes then it will create the bridging header. Then you have to open the bridging header file and import the .h files of your objective c files.Then you can clean your project and import the file inside of your swift view controller file. Then you can easily access the variables and functions inside of your swift file.

TableView error Assertion Failiure when try to load the data

Image
When you tried to display the array of values in tableView but you got the below error. Error: Assertion failure in -[UITableView _configureCellForDisplay:forIndexPath:] The first thing you have to check with your tableView.Because if you not connected the delegate and data source to the taableView then you got the above error. Also if you did not add the table view data source and table view delegate then the above error will come.So add the delegate and data source things like below. class  ViewController: UIViewController , UITableViewDataSource , UITableViewDelegate { } Data source and delegate methods are connected from ViewController to tableView like below. You must have to implement the required delegate methods inside the View Controller.

CoreData Class used twise Error iOS Swift

Image
When you created coreData and tried to run then if it displays error as "the class name of coreData model className used twice in folder and CoreData".Then try the following steps to solve the issue.Select your coreData model object.Select third option in the right side menu. In above submenu Class->codegen->select as Manual/None. Then rebuild the code.The error is resolved Now.

Keyboard with done button and text field will scroll based on the keyboard height iOS swift

Hai friends, I know you had tired for implementing text field delegate and implement the delegate method codes in all the pages.when you follow the code it's easy to hadle the return button, Done button and more. If you want the keyboard with done button.Also when the keyboard appears when a text field is editing the text field wants to show above the keyboard.Keyboard with the next and previous button.The above three scenarios are fulfilled by using the IQKeyboardManager. First, we want to install the cocoa pods in the system and integrate the IQ keyboard manager. import the IQKeyboard in the app delegate.        import IQKeyboardManagerSwift Inside app delegate adds the below code inside the didfinishlaunchingoptions function. In every text field or textView the keyboard will have done, next, prev buttons and also the when keyboard up then the text field will go up and show the editing.

Location-based data validation when app is in terminated state iOS Swift

Hai friends, I know you had tried for location manager for getting the location updates in the background.But it was not possible from the location manager.In LocationManger we can get the location updates and location based data triggering in the foreground only.If we want to do it in terminated state also we can go with GEOFENCING. We will trigger the functions in terminated state with monitoring the locations using geofencing.

UILocalNotification alarm sound does not trigger when app is in Active state iOS Swift

When the app is in foreground app will not trigger the sound of the notification.It will directly come to the app updates.So you can use the AVPlayer to trigger the sound manually when app receives the notification. Code:     func application( _ application: UIApplication , didReceive notification: UILocalNotification ) {          if application. applicationState == . active { //here play the sound using AVPlayer } }

Navigate Particular ViewController from appdelegate in NavigationViewController stack

When you are in AppDelegate you want to navigate to the particular view in Navigation stack.You will use the window as rootViewController then navigate to the particular ViewController using the navigation stack. Code:                 var rootViewController = self .window!.rootViewController as ! UINavigationController                 rootViewController.pushViewController(ViewController, animated: true )

Create round button in iOS swift

Image
You can create a normal button with width and height as equal.After that in storyboard "show the identity inspector" menu create the runtime attributes. Add the below attributes when clicking the plus button.layer.CornerRadius set the value as number 25.layer.MasksToBounds set the boolean value as true.if the button width and height is 50 then corner radius will be 25 because if you want the circle icon you must set corner half of its width.

GMS map view myLocation button action iOS Swift

In GMS map view when you try with the following.          self . maps . isMyLocationEnabled = true Then it will display myLocation button.When you tap that it will zoom the camera to your current location.But you cannot manually implement the action method.So it is better to manually add the button and set the actions.When you want to display the marker to current location if the user taps on the myLocation button then follow the below code.      var currentmarker = GMSMarker () Above class declare the marker as common.      @IBAction func didTapOnMyLocation( _ sender: Any ) { //This code is used for move the marker to the user based location          if CLLocationCoordinate2DIsValid ( currentmarker . position ) {             currentmarker . position = ( self . maps . myLocation ?. coordinate )!             currentmarker . snippet = self . getAddressForLatLng (latitude: String ( currentmarker . position . latitude ), longitude:

GMSMarker manually select the marker iOS swift

I know you guys had worked with Google Maps then you had an idea about marker.You can manually call the select marker method to display the selected marker with marker contentWindow.              self . maps . selectedMarker = currentmarker The marker is selected and the info window is opened.

Google Maps get address from latitude and Longitude iOS Swift

If you are implemented the google maps in your application then you can use your core location framework to get the current latitude and Longitude.Then pass the lat and Long to the below function and get the address to the passed lat, long.It will be got from google Places API. Code:     func getAddressForLatLng(latitude: String , longitude: String ) -> String {          let url = NSURL (string: "https://maps.googleapis.com/maps/api/geocode/json?latlng= \ ( latitude ), \ ( longitude )&key= \ ( googlePlacesapiKey )" )         let data = NSData (contentsOf: url! as URL )         if data != nil {         let json = try ! JSONSerialization . jsonObject (with: data! as Data , options: JSONSerialization . ReadingOptions . allowFragments ) as ! NSDictionary             if let result = json[ "results" ] as ? NSArray   {                 if result. count > 0 {             if let addresss: NSDictionary = result[ 0 ] as ! N

Common Navigation Bar iOS Swift 3

Image
In android, they had Hamburger menu for the common menu.They will use fragment for the menu.But on iOS, we don't have the option.So we will try the below method to achieve that.First, we will create an XIB file for the menu.Then add the menu options in that file.In ViewController add the XIB file as a subview in the top of the view. The above two screenshots had the xib file design and the class file in the second screenshot. The class file as follows Navigation.swift: import UIKit class Navigation: UIView {     @IBOutlet weak var mainTitleLabel: UILabel !     @IBOutlet weak var subTitleLabel: UILabel !     @IBOutlet weak var backBtn: UIButton !     @IBOutlet weak var rightBtn: UIButton !     @IBOutlet weak var addBtn: UIButton !          var callingViewController: AnyObject !     var callingNavigationController: AnyObject !          override internal func prepareForInterfaceBuilder() {         super . prepa

UiPickerView Implementation in iOS Swift 3

Initially, you have to add the PickerView delegate and textField to the class. Code: class  ViewController: UIViewController , UIPickerViewDelegate , UIPickerViewDataSource , UITextFieldDelegate { } Declare the picker in globally. var  Picker: UIPickerView ! var array:NSMutableArray! In ViewDidLoad add the following code          Picker = UIPickerView ()         Picker . delegate = self Set the text field inputView as the picker.         textfield . inputView = Picker Then Implement the PickerView delegate methods.      // MARK: - Picker view data source     func numberOfComponentsInPickerView(pickerView: UIPickerView ) -> Int {         return 1     }          func numberOfRows(inComponent component: Int ) -> Int {         return array . count     }          func pickerView( _ pickerView: UIPickerView ,                     titleForRow row: Int ,                     forComponent compone

Convert NsNumber, NSDate to String in iOS Swift

Code: String (describing: nsnumbervalue )          (or) String (nsnumbervalue ) The above code is used to convert the NsNumber to String.  NSDate To String String (describing: Date ) Combine NsNumber and string to String Code: String ( " \ ( nsnumbervalue ) km" ) Convert Float to String  Code: String (describing: floatvalue )

Convert String to NSNumber iOS Swift

We cannot convert string directly to NSNumber. So we will convert string to Int then convert Int to NSNumber. Code:           NSNumber (value: Int ( "String" )!)

After completed the Full audio handle the Audio player iOS Swift

After audio finished playing we want to set all the player settings to default.So we will call the audioPlayerDidFinishPlaying method in the class. Code:      func audioPlayerDidFinishPlaying( _ player: AVAudioPlayer !, successfully flag: Bool ) {         let view = progress . superview !         audioPlayBtn . isSelected = false         progress . value = 0.0         player. pause ()          updater . invalidate () // updater for the slider play     }

Update the slider based on the audio Player time duration iOS Swift

First, you have to declare the updater. Code:      var updater : CADisplayLink ! = nil      var progress: UISlider ! Then add the target to the updater. Code:              updater = CADisplayLink (target: self , selector: Selector ( "updateProgressView" ))             updater . frameInterval = 1             updater . add (to: RunLoop . current , forMode: RunLoopMode . defaultRunLoopMode ) Then implement the selector method. Code:      func updateAudioProgressView()     {         if player . isPlaying         {             // Update progress                          let total = Float ( player . duration / 60 )             let current_time = Float ( player . currentTime / 60 )             progress . minimumValue = 0.0             progress . maximumValue = Float ( player . duration )             progress . setValue ( Float ( player . currentTime ), animated: true )                      }  

Share the text with newline iOS Swift

Normally in iOS, if you share the text via installed share-based apps we can use \n for new Line or next line.But in WhatsApp, it will not accept the \n.It will only accept the <br></br>.It will navigate the user to the next Line.

Scrollview with horizontal image display with more button iOS swift

We had one scrollView.In that scrollView, we will show the dynamic images.If the user clicks on that image we will execute one action.If the images are more not to show in the scrollView within the view not the scroll then we have to put one more button on the view.We will create one global value as xPos.We will save the next x value on that variable.Every image is added then we will increase the x value. Code:                 for (index, element) in array . enumerated () {                      var newImage = UIButton (type: . custom )                     newImage = UIButton (frame: CGRect (x: xPos , y: 0 , width: 100 , height: 100 ))                     newImage. addTarget ( self , action: #selector ( self . openView (sender:)), for: . touchUpInside )                     if ( xPos - 100 )<( scroll . frame . size . width ) {                         newImage. setImage ( name:"imagename.png" !), for: . normal )                     } else {