Object-C Development tutorial -- How to Use AFNetworking in a project

Source: Internet
Author: User

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];



 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.