Tuesday, 27 March 2018

Apple Map -- Swift4

Plist:

Privacy - Location When In Use Usage Description
Privacy - Location Usage Description

Code Implementation:


import UIKit
import CoreLocation
import MapKit

class ViewController: UIViewController , CLLocationManagerDelegate ,MKMapViewDelegate {
    @IBOutlet weak var loca_map: MKMapView!
    let locationMgr = CLLocationManager()

    override func viewDidLoad() {
        super.viewDidLoad()
        loca_map.delegate=self
        loca_map.showsUserLocation=true
        locationMgr.delegate = self
        
        locationMgr.delegate = self;
        locationMgr.desiredAccuracy = kCLLocationAccuracyThreeKilometers;
       
        let authorizationStatus = CLLocationManager.authorizationStatus()
        
        if (authorizationStatus == CLAuthorizationStatus.notDetermined) || (authorizationStatus == nil) {
            locationMgr.requestWhenInUseAuthorization()
        } else {
            locationMgr.startUpdatingLocation()
        }
        
        let coord = CLLocation (latitude: 12.961945, longitude: 80.258784)
        
        let CLLCoordType = CLLocationCoordinate2D(latitude: coord.coordinate.latitude,
                                                  longitude: coord.coordinate.longitude);
        let anno = MKPointAnnotation();
        anno.coordinate = CLLCoordType;
        loca_map.addAnnotation(anno);
        
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        let currentLocation = locations.last!
        print("Current location: \(currentLocation)")
        let viewRegion = MKCoordinateRegionMakeWithDistance(currentLocation.coordinate, 500, 500)
        loca_map.setRegion(viewRegion, animated: false)
    }

}

Universal Linking

Setup

AASA File



- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray *))restorationHandler {
    if ([userActivity.activityType isEqualToString:NSUserActivityTypeBrowsingWeb]) {
        NSString *myUrl = [userActivity.webpageURL absoluteString];
        NSLog(@"myUrl==%@",myUrl);

    }
    return YES;
}

Auto Layout Vs Adaptive Layout


Auto Layout: With Auto Layout, you can define constraints to control how your user interface adapts immediately to any size changes
Adaptive Layout: Design once in common storyboard and use for both iPad and iPhone.

Tutorial Adaptive Layout







Thursday, 15 March 2018

Swift4--Tableview


import UIKit

class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate {
    
    private var sampleTbl:UITableView!
    
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:UITableViewCell=sampleTbl.dequeueReusableCell(withIdentifier: "identifier")!
        
        
        cell.textLabel?.text="sample done"
        return cell
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let alert:UIAlertController=UIAlertController(title:"Test",message:"success",preferredStyle:UIAlertControllerStyle.actionSheet)
        let cancel:UIAlertAction=UIAlertAction(title:"Cancel",style:UIAlertActionStyle.default,handler:nil)
        alert.addAction(cancel)
        self.present(alert, animated: true, completion: nil)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        
        sampleTbl=UITableView(frame:CGRect(x:20,y:50,width:300,height:300))
        sampleTbl.delegate=self
        sampleTbl.dataSource=self
        sampleTbl.register(UITableViewCell.self, forCellReuseIdentifier: "identifier")
        self.view.addSubview(sampleTbl)
        
        let inptTxt:UITextField=UITextField(frame:CGRect(x:30,y:sampleTbl.frame.maxY+20,width:150,height:50))
        inptTxt.backgroundColor=UIColor.purple
        inptTxt.delegate=self
        self.view.addSubview(inptTxt)
        
        let butAdd:UIButton=UIButton(frame:CGRect(x:inptTxt.frame.origin.x,y:inptTxt.frame.maxY+10,width:200,height:100))
        butAdd.setTitle("Add", for:.normal)
        butAdd.setTitleColor(UIColor.red, for: .normal)
        butAdd.backgroundColor=UIColor.yellow
        butAdd.addTarget(self, action: #selector(ClickActio), for: .touchUpInside)
        self.view.addSubview(butAdd)
        
//        let activityView=UIActivityIndicatorView(activityIndicatorStyle:UIActivityIndicatorViewStyle.gray)
//        activityView.center=self.view.center
//        activityView.hidesWhenStopped=false
//        activityView.startAnimating()
////        activityView.backgroundColor=UIColor(red:(100/255),green:(98/255),blue:(8/255),alpha:1.0)
//        self.view .addSubview(activityView)
       
    }
    @objc func ClickActio(sender:UIButton!) {
        print("add")
    }

    func textFieldDidEndEditing(_ textField: UITextField) {
        textField.resignFirstResponder()
    }
    

Wednesday, 14 March 2018

Swift 4.0 -- CoreData

https://medium.com/xcblog/core-data-with-swift-4-for-beginners-1fc067cca707

Save:



let appDelegate1=UIApplication.shared.delegate as! AppDelegate
let context=appDelegate1.persistentContainer.viewContext
        let entity=NSEntityDescription.entity(forEntityName: "UserDetails", in: context)
        let objMode=NSManagedObject(entity: entity!, insertInto: context)
        objMode.setValue("asfff",forKey:"userId")
        do {
            try context.save()
        } catch {
            print("Failed saving")
        }

        

Fetch:


        let request = NSFetchRequest<NSFetchRequestResult>(entityName:"UserDetails")
        request.returnsObjectsAsFaults=false
        do {
            let result=try context.fetch(request)
            for data in result as! [NSManagedObject] {
              print(data.value(forKey: "userId") as! String)
            }
            
        } catch  {
             print("Failed")
        }

Monday, 8 May 2017

Study

1) http://abhijeetbargeios.blogspot.in/

2) https://www.codementor.io/mattgoldspink/ios-interview-tips-questions-answers-objective-c-du1088nfb

Wednesday, 4 January 2017