How to create a wrapper for Alamofire and SwiftyJSON? Swift – iOS

This blogpost updated with Swift 3 – Xcode 8 – iOS 10 – Alamofire 4.0

AFWrapper
AFWrapper

I have posted a basic tutorial about Alamofire and SwiftyJSON How to use Alamofire and SwiftyJSON with Swift?
And some day before we have learned about Use of Blocks(Closures) or Completion Handlers with Function in Swift – iOS
Let’s combine both the topics to make a WRAPPER of Alamofire and SwiftyJSON.

Read more

How to use Alamofire and SwiftyJSON with Swift? – Swift 3 – iOS 10 – Xcode 8

Swift JSON
Swift JSON

Alamofire is an HTTP networking library written in Swift.
SwiftyJSON makes it easy to deal with JSON data in Swift.

Read more

iOS 8 Map Kit Obj-C : Get Users Location

Map View | User Location
Map View | User Location

iOS 8 Map Kit Obj-C : Get Users Location
In your .plist Add a new row with the key name:
[code lang=”obj-c”]
NSLocationWhenInUseUsageDescription
[/code]
Or
[code lang=”obj-c”]
NSLocationAlwaysUsageDescription
[/code]
Define the header:
[code lang=”obj-c”]
#define IS_OS_8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
[/code]
Update your files with following code:
ViewController.h
[code lang=”obj-c”]
#import <MapKit/MapKit.h>
#import <MapKit/MKAnnotation.h>
@interface YourViewController : UIViewController <MKMapViewDelegate, CLLocationManagerDelegate> {
}
@property(nonatomic, retain) IBOutlet MKMapView *mapView;
@property(nonatomic, retain) CLLocationManager *locationManager;
[/code]
ViewController.m
[code lang=”obj-c”]
– (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
mapView.delegate = self;
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
#ifdef __IPHONE_8_0
if(IS_OS_8_OR_LATER) {
// Use one or the other, not both. Depending on what you put in info.plist
[self.locationManager requestWhenInUseAuthorization];
[self.locationManager requestAlwaysAuthorization];
}
#endif
[self.locationManager startUpdatingLocation];
mapView.showsUserLocation = YES;
[mapView setMapType:MKMapTypeStandard];
[mapView setZoomEnabled:YES];
[mapView setScrollEnabled:YES];
}
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:YES];
self.locationManager.distanceFilter = kCLDistanceFilterNone;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[self.locationManager startUpdatingLocation];
NSLog(@"%@", [self deviceLocation]);
//View Area
MKCoordinateRegion region = { { 0.0, 0.0 }, { 0.0, 0.0 } };
region.center.latitude = self.locationManager.location.coordinate.latitude;
region.center.longitude = self.locationManager.location.coordinate.longitude;
region.span.longitudeDelta = 0.005f;
region.span.longitudeDelta = 0.005f;
[mapView setRegion:region animated:YES];
}
– (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 800, 800);
[self.mapView setRegion:[self.mapView regionThatFits:region] animated:YES];
}
– (NSString *)deviceLocation {
return [NSString stringWithFormat:@"latitude: %f longitude: %f", self.locationManager.location.coordinate.latitude, self.locationManager.location.coordinate.longitude];
}
[/code]
Helping, Learning, Coding 🙂

Create Simple Table (UITableView) in Swift Language iOS8

Here is simple tutorial to create table (UITableView) in Swift Language – iOS 8 – Xcode 6

Swift Table
Swift Table

Attach your UITableView IBOutlet to .swift file
[code language=”obj-c”]
@IBOutlet var tblSwift : UITableView = nil
[/code]
Delegate UITableViewDelegate and UITableViewDataSource to your Controller
[code language=”obj-c”]
class ViewController: UIViewController,UITableViewDelegate, UITableViewDataSource
{

}
[/code]
Don’t forgot to add the delegate methods otherwise you got error
[code language=”obj-c”]
class ViewController: UIViewController,UITableViewDelegate, UITableViewDataSource
{
func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {

}
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {

}
func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {

}
}
[/code]
Register your cell
[code language=”obj-c”]
override func viewDidLoad() {
super.viewDidLoad()
self.tblSwift.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
// Do any additional setup after loading the view, typically from a nib.
}
[/code]
Following is sample of complete code
[code language=”obj-c”]
import UIKit
class ViewController: UIViewController,UITableViewDelegate, UITableViewDataSource
{
@IBOutlet var tblSwift : UITableView = nil
var items: String[] = ["This", "is" , "swift" , "language" , ":)"]
override func viewDidLoad() {
super.viewDidLoad()
self.tblSwift.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
// 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 tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
return self.items.count;
}
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
var cell:UITableViewCell = self.tblSwift.dequeueReusableCellWithIdentifier("cell") as UITableViewCell
cell.textLabel.text = self.items[indexPath.row]
return cell
}
func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
println("You selected item "+items[indexPath.row]) //or
println("You selected item \(items[indexPath.row])") //or
println("You selected cell #\(indexPath.row)!")
}
}
[/code]
Helping, Learning, Coding 🙂