[iOS Weibo project-3.4]-Get user information

Source: Internet
Author: User

Github:https://github.com/hellovoidworld/hvwweibo
 A. Getting user information1. Need to obtain user information and store the user nickname displayed in the title of the "Home" interface navigation bar 2. Ideas use the microblog API to encapsulate user information into the Hvwuser model and store the acquired user name in the account information hvwaccountinfo model stored in sandbox 3. Implement
1 //HVWHOMEVIEWCONTROLLER.M2 /** Get user information*/3- (void) Setupuserinfo {4     //Setting Parameters5Nsmutabledictionary *param =[Nsmutabledictionary dictionary];6     //Access Tokens7Hvwaccountinfo *accountinfo =[Hvwaccountinfotool AccountInfo];8param[@"Access_token"] =Accountinfo.access_token;9     //User uidTenparam[@"UID"] =Accountinfo.uid; One     A     //Send Request -[HvwnetworktoolGet:@"Https://api.weibo.com/2/users/show.json"Parameters:param success:^ (IDresponseobject) { -         //Get user Information theHvwuser *user =[Hvwuser Objectwithkeyvalues:responseobject]; -Hvwaccountinfo *accountinfo =[Hvwaccountinfotool AccountInfo]; -Accountinfo.screen_name =User.screen_name; - [Hvwaccountinfotool Saveaccountinfo:accountinfo]; +         -         //Set navigation bar title + [Self.titlebutton setTitle:accountInfo.screen_name forstate:uicontrolstatenormal]; A} failure:^ (Nserror *error) { atHvwlog (@"failed to get user information!error:%@", error); -     }]; -}
Because the user data model Hvwaccountinfo is used to read and write files using Nskeyedarchiver and Nskeyedunarchiver in Hvwaccountinfotool, the property names to read and write are set
1 //HVWACCOUNTINFOTOOL.M2 /** Get accountinfo from File*/3+ (Hvwaccountinfo *) AccountInfo {4Hvwaccountinfo *accountinfo =[Nskeyedunarchiver unarchiveobjectwithdata:[nsdata Datawithcontentsoffile:accountinfopath];5    6     //need to decide whether to expire7NSDate *now =[NSDate Date];8     if([now compare:accountInfo.expires_time]! = nsorderedascending) {//Now->expires_data not ascending, has expired9AccountInfo =Nil;Ten     } One     A     returnAccountInfo; - } -  the /** Store AccountInfo to file*/ -+ (void) Saveaccountinfo: (Hvwaccountinfo *) AccountInfo { - [Nskeyedarchiver archiverootobject:accountinfo Tofile:accountinfopath]; -}
1 //HVWACCOUNTINFO.M2 #pragmaMark-nscoding3 /** Called from File parsing object*/4- (ID) Initwithcoder: (Nscoder *) Adecoder {5     if(self =[Super Init]) {6Self.access_token = [Adecoder decodeobjectforkey:@"Access_token"];7self.expires_in = [Adecoder decodeobjectforkey:@"expires_in"];8Self.expires_time = [Adecoder decodeobjectforkey:@"Expires_time"];9Self.uid = [Adecoder decodeobjectforkey:@"UID"];TenSelf.screen_name = [Adecoder decodeobjectforkey:@"Screen_name"]; One     } A     -     returnSelf ; - } the  - /** Write object to file call*/ -- (void) Encodewithcoder: (Nscoder *) Acoder { -[Acoder encodeObject:self.access_token Forkey:@"Access_token"]; +[Acoder encodeObject:self.expires_in Forkey:@"expires_in"]; -[Acoder encodeObject:self.expires_time Forkey:@"Expires_time"]; +[Acoder encodeObject:self.uid Forkey:@"UID"]; A[Acoder encodeObject:self.screen_name Forkey:@"Screen_name"]; at}
Get the user nickname, and placed in the "Home" interface navigation bar Title button, there is the text length calculation button width, here directly rewrite the button Settitle method
1 //hvwnavigationbartitlebutton.m2 /** Rewrite settitle, change width according to text*/3- (void) Settitle: (NSString *) title forstate: (uicontrolstate) state {4 [Super Settitle:title forstate:state];5    6Nsdictionary *param =@{nsfontattributename:self.titlelabel.font};7CGFloat titlewidth =[title Boundingrectwithsize:cgsizemake (Maxfloat, Self.height) options:nsstringdrawinguseslinefragmentorigin Attributes:param Context:nil].size.width;8    9Self.width = titlewidth + self.height +Ten;Ten}
When the "home" controller is initialized, if you find a user nickname stored in the sandbox, set the navigation bar title to user nickname
1 //HVWHOMEVIEWCONTROLLER.M2 /** Set navigation bar*/3- (void) Setupnavigationbar {4     //Add navigation Controller button5     //left button6Self.navigationItem.leftBarButtonItem = [Uibarbuttonitem itemwithimage:@"Navigationbar_friendsearch"Hightlightedimage:@"navigationbar_friendsearch_highlighted"target:self selector: @selector (searchfriend)];7    8     //Right Button9Self.navigationItem.rightBarButtonItem = [Uibarbuttonitem itemwithimage:@"Navigationbar_pop"Hightlightedimage:@"navigationbar_pop_highlighted"target:self selector: @selector (pop)];Ten     One     //Set Caption Button AHvwnavigationbartitlebutton *titlebutton =[[Hvwnavigationbartitlebutton alloc] init]; -Titlebutton.height = *; -     the     //save to member properties -Self.titlebutton =Titlebutton; -     -     //Set navigation bar title +Hvwaccountinfo *accountinfo =[Hvwaccountinfotool AccountInfo]; -NSString *navtitle =@"Home Page"; +     if(accountinfo.screen_name) { ANavtitle =Accountinfo.screen_name; at     } - [Titlebutton settitle:navtitle forstate:uicontrolstatenormal]; -     -[Titlebutton setimage:[uiimage imagewithnamed:@"Navigationbar_arrow_down"] forstate:uicontrolstatenormal]; -     //set a background picture -[Titlebutton setbackgroundimage:[uiimage Resizedimage:@"navigationbar_filter_background_highlighted"] forstate:uicontrolstatehighlighted]; in     -     //Monitor button Click event, replace icon to [Titlebutton addtarget:self Action: @selector (TITLEBUTTONCLICKD:) forControlEvents: UIControlEventTouchUpInside]; +     -Self.navigationItem.titleView =Titlebutton; the}

[iOS Weibo project-3.4]-Get user information

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.