Saturday, 28 November 2015

How to Implement AFNetworking Library / without Cocoa Pods.

This is a guide to help developers get up to speed with AFNetworking. It is geared primarily towards anyone who is new to Mac or iOS development, or has not worked extensively with 3rd-party libraries before.
These step-by-step instructions are written for Xcode 5, using the iOS 7 SDK. If you are using a previous version of Xcode, you may want to update before starting.
Instead of boasting an killing your time over waste info, this tutorial will now direct you to implementation in simple easy steps. Enjoy saving your time to impress  your managers.
1. Download the AFNetworking Library from this Link:
https://github.com/AFNetworking/AFNetworking
2. Drag and Drop the entire folder "AFNetworking" in your project.

3. Then click on your project , then go to the build phases, selecting from the upper tabs, then press (CTRL+F) and then find the keyword "af" you will find a list of files from afnetworking. Some of the files do not support ARC so will need to add some flags .Don't worry i am still here to guide you even with this.
Double  click  on these files and then add then paste this string without quotes "-fno-objc-arc"
Files: 1. AFURLRequestSerialization.m
          2. AFNetworkReachabilityManager.m
          3. AFNetworkActivityIndicatorManager.m
          4. AFHTTPRequestOperation.m
          5. AFHTTPSessionManager.m
          6. AFHTTPRequestOperationManager.m
          7. AFSecurityPolicy.m
          8. AFURLResponseSerialization.m

4. Import this in your project's .pch file .
    #import "AFNetworking.h"
 Tag: http://icodesforcrazyclients.blogspot.in/2015/11/how-to-add-pch-file.html

5. Import Security Framework.

6.  This was all about background settings. But we have not yet covered how to use it as methods to operate on web services , i.e. to send the parameters to associated web services and response handling.
Below are the methods you just need to copy and use.

6.1 Create a class of NSObject and give it a name say "AFNetworkMethods"

6.2 Add these method declaration in its .h file:

6.3.1 POST METHOD
      +(void)callPostWSWithMethod:(NSString*)methodName withUrl:(NSString*)urlString withParameter:(NSDictionary*)parameter success:(void(^)(id response))successBlock errorBlock:(void(^)(NSError *error))errorBlock;

6.3.2 GET METHOD
+(void)callGetWSWithMethod:(NSString*)methodName withUrl:(NSString*)urlString withParameter:(NSDictionary*)parameter Block:(downloadBlock)block;

6.3.2 PUT METHOD
+(void)callPutWSWithMethod:(NSString*)methodName withUrl:(NSString*)urlString withParameter:(NSDictionary*)parameter success:(void(^)(id response))successBlock errorBlock:(void(^)(NSError *error))errorBlock;
and add this statement after the header file of .h file:
typedef void(^downloadBlock)(id success,NSError *error);

6.3 Now we will define the methods in its .m file:
First import app delegate in .m file: 
#import "AppDelegate.h"

6.4 
6.4.1 POST METHOD
     +(void)callPostWSWithMethod:(NSString*)methodName withUrl:(NSString*)urlString withParameter:(NSDictionary*)parameter success:(void(^)(id response))successBlock errorBlock:(void(^)(NSError *error))errorBlock
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:urlString parameters:parameter success:^(AFHTTPRequestOperation *operation, id responseObject)
     {
         NSError *error;
         id result = [NSJSONSerialization JSONObjectWithData: responseObject options: NSJSONReadingMutableContainers error:&error];
         successBlock(result);
     }
          failure:^(AFHTTPRequestOperation *operation, NSError *error)
     {
         //NSLog(@"%@",[error description]);
         errorBlock(error);
     }];
}

6.4.2  GET METHOD
+(void)callGetWSWithMethod:(NSString*)methodName withUrl:(NSString*)urlString withParameter:(NSDictionary*)parameter Block:(downloadBlock)block;
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    manager.responseSerializer.acceptableContentTypes=[NSSet setWithObject:@"text/html"];
    NSString *token=[[NSUserDefaults standardUserDefaults] objectForKey:@"TOKEN"];
    NSString *str=[NSString stringWithFormat:@"Token %@",token];
    [manager.requestSerializer setValue:str forHTTPHeaderField:@"Authorization"];
    AFJSONResponseSerializer *jsonReponseSerializer = [AFJSONResponseSerializer serializer];
    jsonReponseSerializer.acceptableContentTypes = nil;
    manager.responseSerializer = jsonReponseSerializer;
    [manager GET:urlString parameters:parameter success:^(AFHTTPRequestOperation *operation, id responseObject)
     {
         block(responseObject,nil);
     }failure:^(AFHTTPRequestOperation *operation, NSError *error)
     {
         block(nil,error);
     }];
}

6.4.3 PUT METHOD
+(void)callPutWSWithMethod:(NSString*)methodName withUrl:(NSString*)urlString withParameter:(NSDictionary*)parameter success:(void(^)(id response))successBlock errorBlock:(void(^)(NSError *error))errorBlock
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer=[AFHTTPResponseSerializer serializer];
    NSString *token=[[NSUserDefaults standardUserDefaults] objectForKey:@"TOKEN"];
    NSString *str=[NSString stringWithFormat:@"Token %@",token];
    
    [manager.requestSerializer setValue:str forHTTPHeaderField:@"Authorization"];
    
    [manager PUT:urlString parameters:parameter success:^(AFHTTPRequestOperation *operation, id responseObject)
     {
         NSError *error;
         id result = [NSJSONSerialization JSONObjectWithData: responseObject options: NSJSONReadingMutableContainers error:&error];
         successBlock(result);
     }
         failure:^(AFHTTPRequestOperation *operation, NSError *error)
     {
         errorBlock(error);
     }];
}

7. Now we are all set to use these methods. lets see how to implement these methods. One thing that we need to know before implementing the last stage is that AFNetworking runs on a different thread,So you may find difficulty in handling them. But i know Visitors are smart enough to handle them. But why to waste time ,lets fasten the process of development and just copy and paste even this where you want to call the web service.
7.1 METHOD ILLUSTRATION
-(void) YourMethodName
{

    NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init];
// add parameters to the dictionary.

    [AFNetworkHelper callPostWSWithMethod:@"" withUrl:
@"your web service url " withParameter:parameters success:^(id response)
     {
         if((response == nil) || (response == NULL))
         {
              return;
         }
         if(response)
         {
            // Handle the response.
         }
         else
         {
             return;
         }
     }
      errorBlock:^(NSError *error)
     {
       NSLog(@"%@",error);
     }];

}

Hope you will not find any difficulty implementing AFNetworking. 
Feel free to post your suggestions and your contribution to improving this tutorial will be highly appreciated . 
Thanks for visiting and do help your friends to save their time.

How to add a .pch file ?

How to add a .pch file??

This tutorial makes this task veru easy .
Lets have a brief look on what is it and what is it used for??

Prefix.pch is a precompiled header. Precompiled headers were invented to make compiling faster. Rather than parsing the same header files over and over, these files get parsed once, ahead of time.
In Xcode, you add imports of the header files you want in a “prefix header,” and enabling Precompile Prefix Header so they get precompiled. But the idea behind a prefix header is different from precompiling.
We need to follow these simple steps :
1. Right Click on Supporting files and select New File from pop up menu.


2. Then select Other option from IOS

3. We find pch file from there , select and then click Next.

4. Make sure the location of the .pch file is the project location. Click Create.


5. You will find the .pch file in your project. Start importing frameworks and files you want to be precompiled.


5.  If you find any error while building and running the app comment the lines as shown in below image and then add files as shown below.


Hope you will not find any difficulty implementing AFNetworking. 
Feel free to post your suggestions and your contribution to improving this tutorial will be highly appreciated . 
Thanks for visiting and do help your friends to save their time.