IOS Sina Weibo client demo Practices (4) Weibo posts page

Source: Internet
Author: User

The demo program has been uploaded and requires five resource points. If there is no resource score, leave an email in the comments. I will send it to you. Please download and modify it and exchange it!Demo download link

This blog post describes the implementation of the Weibo page.


First, let's take a look at what needs to be done on this Weibo page!

(1) Weibo posts include text content and Weibo images, so we can use textview to load Weibo text content and imageview to load image content.

① In the text section, a textview is used to check the number of sent texts. If the number exceeds 140, a prompt is displayed. In the image section, use an imageview. If an image is added, add a cross button in the upper-right corner of the image to remove the image. Of course, when you do not select to add an image or cancel the selected image, the button is automatically canceled. The effect is as follows:

The cancelbutton processing for this cross-cutting is relatively simple. The Code is as follows:

- (IBAction)cancelImage:(id)sender {    _cancelButton.hidden = YES;    self.theImageView.image = [UIImage imageNamed:@"noImage50x118.png"];}

Add a send button to the right of the navigation bar to process the sent information. The Code is as follows:

-(Ibaction) sendweibo o :( ID) sender {[self. thetextview resignfirstresponder]; nsstring * content = [[nsstring alloc] initwithstring: _ thetextview. text]; // calculate the number of words in the content sent to Weibo, and process nsinteger contentlength = content accordingly. length; If (contentlength> 140) {mbprogresshud * overlengthhud = [[mbprogresshud alloc] initwithview: Self. view]; [self. view addsubview: overlengthhud]; overlengthhud. mode = mbprogresshudmodetext; Overlengthhud. labeltext = @ "" "; overlengthhud. detailslabeltext = [nsstring stringwithformat: @": % d exceeds the upper limit of 140! ", Contentlength]; [overlengthhud show: Yes]; [overlengthhud hide: Yes afterdelay: 2];} else {uiimage * image = _ theimageview. image; // no image if (! Haspostimage) {[self postwithtext: content] ;}// else {[self postwithtext: Content image: Image];} HUD = [[mbprogresshud alloc] init]; HUD. dimbackground = yes; HUD. labeltext = @ "sending... "; [HUD show: Yes]; [self. view addsubview: HUD] ;}}

Note that the method-(void) postwithtext :( nsstring *) text is sent to Weibo, and the method-(void) postwithtext :( nsstring *) text image :( uiimage *) image is a text + image microblog. The specific implementation process is described below.


Modify: It suddenly finds that there is still a small problem in the above sending code. If no text is entered, it can also be sent. This is obviously incorrect. Then, add an if statement to determine whether the word length is 0. If the word length is 0, an alert window prompt is displayed.

If (contentlength = 0) {uialertview * Alert = [[uialertview alloc] initwithtitle: Nil message: @ "Enter Weibo content! "Delegate: Nil cancelbuttontitle: @" "otherbuttontitles: nil, nil]; [alert show];} else if (contentlength> 140) {} else {}

② Here we will focus on how to add images.

Use a button to insert an image.



The source of the inserted image includes the system album and shooting.

Here we need to use the uiimagepickercontroller class to create an image selector. Uiimagepickercontroller the image selector is a navigation controller class that allows you to add simple image selection functions or camera interfaces to applications. The user will see an image selection screen where a photo is selected. The source of the photo is his own photo library, saved album or camera. After you select a photo, the image is selected using the method in the uiimagepickerdelegate protocol and the delegate method of the notification selector.

The role and appearance of an image picker controller depend on the source type you assign to it before you present it.

A sourcetypeUiimagepickercontrollersourcetypecameraProvides a user interface for taking a new picture or movie (on devices that support media capture ).

A sourcetypeUiimagepickercontrollersourcetypephotolibraryOrUiimagepickercontrollersourcetypesavedphotosalbumProvides a user interface for choosing among saved pictures and movies.

It is illustrated here thatSourcetypeSet the source of the image. Including

Enum {

Uiimagepickercontrollersourcetypephotolibrary, // photo library

Uiimagepickercontrollersourcetypecamera, // camera

Uiimagepickercontrollersourcetypesavedphotosalbum // Save the photo

};

Typedef nsuinteger uiimagepickercontrollersourcetype;

Verify that the device is capable of picking content from the desired source. Do this calling the issourcetypeavailable: class method, providing a constant from the "uiimagepickercontrollersourcetype" enumeration.

When using this method, check whether the current device supports using uiimagepickercontroller. In this case, we need to call issourcetypeavailable: method to determine whether sourcetype must be provided as a parameter.

After you select an image, the selector delegate will receive a notification through the didfinishpickingimage method. The proxy will get a uiimage object containing the image. If the editing function is enabled, it will also get an nsdictionary containing the editing attribute.

When using uiimagepickercontroller, add the two delegate, <uiimagepickercontrollerdelegate, uinavigationcontrollerdelegate>, and implement the two methods.

-(Void) imagepickercontroller :( uiimagepickercontroller *) picker didfinishpickingmediawithinfo :( nsdictionary *) info

-(Void) imagepickercontrollerdidcancel :( uiimagepickercontroller *) picker

Set the delegate of the selector to assign a delegate to the selector: picker. Delegate = self;

Here we need to implement the first method below, so that when an image is selected, the delegate class will be notified, add the image processing code to this method to process the selected image.

The code for adding images is as follows:

-(Ibaction) addphoto :( ID) sender {uialertview * Alert = [[uialertview alloc] initwithtitle: Nil message: @ "insert image" delegate: Self cancelbuttontitle: @ "cancel" failed: @ "system album", @ "shooting", nil]; [alert show] ;}# Pragma mark-uialertviewdelegate-(void) alertview :( uialertview *) alertview clickedbuttonatindex :( nsinteger) buttonindex {If (buttonindex = 1) {[self addphoto];} else if (buttonindex = 2) {[se Lf takephoto] ;}}- (void) addphoto {uiimagepickercontroller * imagepickercontroller = [[uiimagepickercontroller alloc] init]; imagepickercontroller. sourcetype = uiimagepickercontrollersourcetypephotolibrary; imagepickercontroller. delegate = self; imagepickercontroller. allowsediting = no; [self presentmodalviewcontroller: imagepickercontroller animated: Yes];}-(void) takephoto {If (! [Uiimagepickercontroller failed: Failed]) {uialertview * Alert = [[uialertview alloc] initwithtitle: Nil message: @ "this device does not support the camera function" delegate: Nil cancelbuttontitle: Nil failed: @ "good", nil]; [alert show];} else {uiimagepickercontroller * imagepickercontroller = [[uiimagepickercontroller alloc] init]; imagepickercontroller. sourcetype = uiimagepickercontrollersourcetypecamera; imagepickercontroller. delegate = self; imagepickercontroller. allowsediting = no; [self presentmodalviewcontroller: imagepickercontroller animated: Yes] ;}# Pragma mark-Example-(void) imagepickercontroller :( uiimagepickercontroller *) picker syntax :( nsdictionary *) info {[picker dismissmodalviewcontrolleranimated: Yes]; uiimage * image = [info objectforkey: @ "uiimagepickercontrolleroriginalimage"]; self. theimageview. image = image; haspostimage = yes; _ cancelbutton. hidden = no ;}

(1) API call required for sending Weibo posts; this API is used if only Weibo posts are sent: publish. It is noted that both HTTP requests are post requests, and the request parameters also need to be updated. The text must be subject to urlencode with no more than 140 Chinese characters. For images, they must be of the binary type, only JPEG, GIF, and PNG formats are supported. The image size is smaller than 5 MB.

I used the third-party class library asihttprequest to process these data requests.

The Code is as follows:

// Publish text Weibo-(void) postwithtext :( nsstring *) text {nsurl * url = [nsurl urlwithstring: weibo_update]; asiformdatarequest * Item = [[asiformdatarequest alloc] initwithurl: url]; [item setpostvalue: [infoforsina returnaccesstokenstring] forkey: @ "access_token"]; [item setpostvalue: Text forkey: @ "status"]; [item setcompletionblock: ^ {self. thetextview. TEXT = nil; [HUD removefromsuperview]; mbprogresshud * custuon HUD = [[mbprogresshud alloc] initwithview: Self. view]; custuonhud. customview = [[uiimageview alloc] initwithimage: [uiimage imagenamed: @ "37x-checkmark.png"]; custuonhud. labeltext = @ "Weibo posts successful! "; Custuonhud. mode = mbprogresshudmodecustomview; [self. view addsubview: custuonhud]; [custuonhud show: Yes]; [custuonhud hide: Yes afterdelay: 1] ;}]; [item startasynchronous];} // publish text image Weibo-(void) postwithtext :( nsstring *) text image :( uiimage *) image {nsurl * url = [nsurl urlwithstring: weibo_upload]; asiformdatarequest * Item = [[asiformdatarequest alloc] initwithurl: url]; [item setpostvalue: [infoforsina Returnaccesstokenstring] forkey: @ "access_token"]; [item setpostvalue: Text forkey: @ "status"]; [item adddata: uiimagepngrepresentation (image) forkey: @ "pic"]; [item setcompletionblock: ^ {self. theimageview. image = [uiimage imagenamed: @ "noimage50x118.png"]; self. thetextview. TEXT = nil; [HUD removefromsuperview]; _ cancelbutton. hidden = no; mbprogresshud * custuonhud = [[mbprogresshud alloc] initwithview: S Elf. View]; custuonhud. customview = [[uiimageview alloc] initwithimage: [uiimage imagenamed: @ "37x-checkmark.png"]; custuonhud. labeltext = @ "Weibo success! "; Custuonhud. mode = mbprogresshudmodecustomview; [self. view addsubview: custuonhud]; [custuonhud show: Yes]; [custuonhud hide: Yes afterdelay: 1] ;}]; [item startasynchronous];}

# Import "asiformdatarequest. H"

There are two points to note:

1. urlencode is required for text content. I have not done anything here, because the third-party class library we use has already been implemented for us. For Weibo image content, it must be converted to binary reality. This is how we handle: uiimagepngrepresentation (image)

Nsdata * uiimagepngrepresentation (
Uiimage * Image
);

Returns the data for the specified image in PNG format

2. In the block after the message is sent, we processed both textview and imagview to facilitate sending the next Weibo message. At the same time, I added a prompt box to indicate that the message was successfully sent, in this prompt box, I used customview, which is the image to be hooked. As follows.

Okay, that's probably the end!



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.