Get Reactions from Timeline Post via Facebook Graph API – Swift – iOS

Facebook Reactions
Facebook Reactions

New version of SDK is 4.16.x(Swift) and Graph API Version is 2.8.
With the Graph API 2.6 Facebook has given support to fetch (read only) the Reactions on Timeline Posts.

Post is updated for iOS 10 and Swift 3

API documentation is available here.

Let’s learn how to get reactions from the post

Install pods

Login with Facebook

I have already written a tutorial on Facebook Login.

Get Facebook Posts via Graph API

We can get the Facebook post via graph API : /me/posts for that we have to add user_posts permission with login.

Get Reactions from one of the Post

We can get the Facebook post reactions via graph API : /{post-id}/reactions. We have to pass parameters like fields and summary as described in getReactions function.
Note : Here I am writing sample for only first post.

Response should be like

Type of reactions : NONE, LIKE, LOVE, WOW, HAHA, SAD, ANGRY

#Like #Share #React 🙂 😛 😀
Happy coding 🙂

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

Use of Blocks(Closures) or Completion Handlers with Function in Swift – iOS

Blocks in Objective-C

In Objective-C we are using the blocks(completion handlers) with functions as follows :

[code language=”obj-c”]
– (void)yourFunctionName:(NSString *)yourString withCompletionHandler:(void (^)(NSString *yourResult))block;
[/code]

Closures in Swift

Closures are self-contained blocks of functionality that can be passed around and used in your code. Closures in Swift are similar to blocks in C and Objective-C and to lambdas in other programming languages.

Syntax with Function

[code language=”obj-c”]
func yourFunctionName(parameter:Type, … , withCompletionHandler:(result:Type) -> Void)
[/code]
For more closure syntax : goshdarnclosuresyntax.com

Example

Function Definition:
[code language=”obj-c”]
func closureReturn(isTest:Bool, withCompletionHandler:(result:String) -> Void) {
if(isTest){
withCompletionHandler(result: "Yes")
}
else{
withCompletionHandler(result: "No")
}
}
[/code]
Calling Function:
[code language=”obj-c”]
closureReturn(true) { (result) -> Void in
print(result)
}
[/code]
Output should be respective to value true/false.


In next post I will write a tutorial on a wrapper class for Alamofire with use of SwiftyJSON by using closures.

It will be related to this post How to use Alamofire and SwiftyJSON with Swift? – Swift 2 – iOS 9 – Xcode 7

Happy Coding 🙂

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

How to work with IBDesignable and IBInspectable in Swift Language?

IBDesignable with Storyboard
IBDesignable with Storyboard

How to use the @IBDesignable and @IBInspectable?

By giving you a simple example that we can not change the corner radius from the design view. We have to change it run time. If you want to make possible it with design time then you can use the @IBDesignable and @IBInspectable.

So, We are taking an example as IBButtonExtender for this the functionality

  • Border Color
  • Border Width
  • Corner Radius

Create an Custom Class for UIButton with @IBDesignable

Create an @IBInspectable for Inspect the element. the Border Color property will be added to the list in the right panel when you create button with this class.

Set Initialization functions and other @IBInspectable as above.
You can check the code for the ButtonExtender.swift

IBButtonExtender

Presenting the ButtonExtender for Border Color, Border Width and Corner Radius function with the @IBDesignable and @IBInspectable in the Swift Language.

By making some corner radius and colors, You will get neat and clean output.

Output
Output

Github

Find IBButtonExtender on github 🙂
Check other articles on Swift Language.
All suggestions are acceptable. Put it in the comments!
Happy Coding 🙂

Facebook Login – Swift Language – iOS 10 – Swift 3

Facebook Login using custom button in Swift Language
Xcode Version 8
Article is Updated with the Facebook 4.16 SDK [27-September-2016].

facebook-logo
Install Pods

Appdelegate.swift

ViewController.swift
Create touch up inside event for custom button like as follows

In iOS 10 don’t forget to set capabilities (Keychain Sharing) :
Go to your project targets -> Capabilities -> Keychain Sharing -> Toggle Switch ON

Capabilities - KeyChain Sharing
Capabilities – KeyChain Sharing

Detailed Output Log :

Happy Coding 😀

Requesting Access to the Address Book – Swift Language – iOS 8

Request to Access Address Book
Request to Access Address Book

Here is simple steps to requesting access to the Address Book in Swift Language iOS 8
Import the framework of Address Book:
[code language=”objc”]
import AddressBook
[/code]
Create an object of Address Book:
[code language=”objc”]
var addressBook: ABAddressBookRef?
[/code]
Create a method for assigning the value to addressBook:
[code language=”objc”]
func createAddressBook(){
var error: Unmanaged<CFError>?
addressBook = ABAddressBookCreateWithOptions(nil, &error).takeRetainedValue()
}
[/code]
Post following code to requesting access:
[code language=”objc”]
switch ABAddressBookGetAuthorizationStatus(){
case .Authorized:
println("Already authorized")
createAddressBook()
/* Access the address book */
case .Denied:
println("Denied access to address book")
case .NotDetermined:
createAddressBook()
if let theBook: ABAddressBookRef = addressBook{
ABAddressBookRequestAccessWithCompletion(theBook,
{(granted: Bool, error: CFError!) in
if granted{
println("Access granted")
} else {
println("Access not granted")
}
})
}
case .Restricted:
println("Access restricted")
default:
println("Other Problem")
}
[/code]
Happy Coding 🙂

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 🙂

How to add an Objective-C file in your Swift Project? or How to set Objective-C bridging header?

Bridging header
Bridging header

To import a set of Objective-C files in the same app target as your Swift code, you rely on an Objective-C bridging header to expose those files to Swift. Xcode offers to create this header file when you add an Objective-C file to an existing Swift app.

If you accept, Xcode creates the header file along with the file you were creating, and names it by your product module name followed by adding “-Bridging-Header.h”.

Alternatively, you can create a bridging header yourself by choosing File > New > File > (iOS or OS X) > Source > Header File.

You’ll need to edit the bridging header file to expose your Objective-C code to your Swift code.

To import Objective-C code into Swift from the same target

  1. In your Objective-C bridging header file, import every Objective-C header you want to expose to Swift.
    For example:
    [code language=”obj-c”]
    #import "XYZCustomCell.h"
    #import "XYZCustomView.h"
    #import "XYZCustomViewController.h"
    [/code]
  2. Under Build Settings, make sure the Objective-C Bridging Header build setting under Swift Compiler – Code Generation has a path to the header.

    The path should be relative to your project, similar to the way your Info.plist path is specified in Build Settings. In most cases, you should not need to modify this setting.

Any public Objective-C headers listed in this bridging header file will be visible to Swift. The Objective-C functionality will be available in any Swift file within that target automatically, without any import statements. Use your custom Objective-C code with the same Swift syntax you use with system classes.

For Example:
[code language=”obj-c”]
let myCell = XYZCustomCell()
myCell.subtitle = "A custom cell"
[/code]
Helping, Learning, Coding 🙂
Source : Apple Documents

Update : JSON Array Parsing in Swift Language – Swift 3 – iOS 10 – Xcode 8

Swift JSON
Swift JSON

So, how to parse following type of JSON?

Create JSON Array Object :

Parse JSON Array Object :

Complete code snippet with UITableView:

Posted a gist on github.
Helping, Learning, Coding 🙂