Object-C Development tutorial -- How to Use AFNetworking in a project
AFNetworking is a convenient network development framework for iOS. Today, we will briefly introduce how to use it in our project.
1. download the latest AFNetworking code from the official website.
2. Import the AFNetWorking and UIKit + AFNetworking folders to the project.
3. Add the class library Security. framework, MobileCoreServices. framework, and SystemConfiguration. framework.
4. import data in use
# Import AFNetworking. h
# Import UIKit + AFNetworking. h
5. Test network url access.
NSString * URLTmp = @ http://www.coneboy.com;
NSString * URLTmp1 = [URLTmp stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
// Transcoding to UTF-8 otherwise errors may occur
URLTmp = URLTmp1;
NSURLRequest * request = [NSURLRequest requestWithURL: [NSURL URLWithString: URLTmp];
AFHTTPRequestOperation * operation = [[AFHTTPRequestOperation alloc] initWithRequest: request];
[Operation setCompletionBlockWithSuccess: ^ (AFHTTPRequestOperation * operation, id responseObject ){
NSLog (@ Success: % @, operation. responseString );
NSString * requestTmp = [NSString stringWithString: operation. responseString];
NSData * resData = [[NSData alloc] initWithData: [requestTmp dataUsingEncoding: NSUTF8StringEncoding];
// JSON Parsing is provided in the system
NSDictionary * resultDic = [NSJSONSerialization JSONObjectWithData: resData options: NSJSONReadingMutableLeaves error: nil];
NSLog (@ success: % @, resultDic );
} Failure: ^ (AFHTTPRequestOperation * operation, NSError * error ){
NSLog (@ Failure: % @, error );
// [SVProgressHUD dismissWithError: @ submission failed. Please try again];
}];
[Operation start];
6. How to obtain a network image
NSString * url = @ http://www.uimaker.com/uploads/allianz 111101/410111104265050_3.jpg;
UIImageView * imageView = [[UIImageView alloc] initWithFrame: CGRectMake (10.0f, 1500000f, 2000000f, 2000000f)];
[ImageView setImageWithURL: [NSURL URLWithString: url];
[Self. view addSubview: imageView];