iOS development: How do I contact the server in XMPP?

Source: Internet
Author: User
Tags call back vcard

How do I contact the server in XMPP?

The first step: Get data from the server through various modules. such as Electronic Business Card module, Avatar module, roster module, message module and so on.

The second step: the modified data needs to be synchronized to the server, its own defined method, when the click on the Save button to call the following method.

#pragma mark clicks the button to update the server. -(void) Editvcardviewcontroller: (Wceditvcardviewcontroller *) EDITVC Didfinishedsave: (ID) sender{    WCLog (@ "Finish saving" );    Get current vcard    xmppvcardtemp *myvcard = [Wcxmpptool sharedwcxmpptool].vcard.myvcardtemp;        Reset Avatar    Myvcard.photo = uiimagejpegrepresentation (Self.avatarImgView.image, 0.75);        Reset the properties in Myvcard    myvcard.nickname = self.nicknameLabel.text;    Myvcard.orgname = Self.orgNameLabel.text;        if (self.departmentLabel.text! = nil) {        myvcard.orgunits = @[self.departmentlabel.text];    }        Myvcard.title = Self.telLabel.text;    Myvcard.note = Self.telLabel.text;            Resolve Mailbox    //myvcard.mailer = Self.emailLabel.text;    if (Self.emailLabel.text.length > 0) {        //save only one mailbox        myvcard.emailaddresses = @[self.emaillabel.text];    }        //Save data to Server//    internal implementation data upload is the entire Electronic Business card data uploaded once, including the picture    [[Wcxmpptool Sharedwcxmpptool].vcard Updatemyvcardtemp:myvcard];    }

Step three: Notifies the client to refresh after the contents of the server are updated.

Method One: The modified content is displayed directly to the client without the server. such as the modification of electronic business cards.

Method Two: Through the server, when the contents of the server storage changes, the XMPP framework updates the local database content, and XMPP has a proxy method, when the server content changes will call back the method, in this method can be manipulated from the database to retrieve content, such as roster and chat message changes. As follows:

Get required database-(void) loadusers2{//Show Buddy data (save Xmpproster.sqlite file)//1. Context Association Xmpproster.sqlite file Nsmanagedobjectco        ntext *rostercontext = [Wcxmpptool sharedwcxmpptool].rosterstorage.mainthreadmanagedobjectcontext; 2.Request request query which table nsfetchrequest *request = [Nsfetchrequest fetchrequestwithentityname:@]        Xmppusercoredatastorageobject "];    Set sort Nssortdescriptor *sort = [Nssortdescriptor sortdescriptorwithkey:@ "DisplayName" ascending:yes];            Request.sortdescriptors = @[sort];    Filter Nspredicate *pre = [nspredicate predicatewithformat:@ "Subscription! =%@", @ "none"];        Request.predicate = pre; 3. Execute the request//3.1 Create the result controller//database query, if the data are many, will be placed in the sub-thread query//Mobile client database data will not be many, so many database query operations are the main thread _resultscontr = [[Nsfetche Dresultscontroller alloc] Initwithfetchrequest:request managedobjectcontext:rostercontext sectionNameKeyPath:nil    Cachename:nil];    _resultscontr.delegate = self;    Nserror *err = nil; 3.2 Execution [_resultscontr performfetch:&eRR]; Wclog (@ "%@", _resultscontr.fetchedobjects);}

Get the contents of the database and display-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath    {static NSString *id = @ "Contactcell";        UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:id];    Get the corresponding friend//xmppusercoredatastorageobject *user = Self.users[indexpath.row];    Xmppusercoredatastorageobject *user = _resultscontr.fetchedobjects[indexpath.row];    Identify whether the user is online//0: Online 1: Leave 2: Offline Wclog (@ "%@: Online Status%@", User.displayname,user.sectionnum);    Cell.textLabel.text = User.displayname; 1. Use KVO to monitor user status changes//[user addobserver:self forkeypath:@ "Sectionnum" Options:nskeyvalueobservingoptionnew Context:        NIL];            Switch ([User.sectionnum IntegerValue]) {Case 0:cell.detailtextlabel.text = @ "Online";        Break            Case 1:cell.detailtextlabel.text = @ "Away";        Break            Case 2:cell.detailtextlabel.text = @ "Offline";        Break Default:cell.detailTextLaBel.text = @ "Damn it";    Break    }//Show friend's avatar if (User.photo) {//default case, not program one starts with avatar cell.imageView.image = User.photo;        }else{//Get avatar from the server nsdata *imgdata = [[Wcxmpptool Sharedwcxmpptool].avatar PhotoDataForJID:user.jid];    Cell.imageView.image = [UIImage imagewithdata:imgdata];    } return cell; }

#pragma mark-the proxy for the result controller #pragma mark-the database content changes call this method-(void) Controllerdidchangecontent: (Nsfetchedresultscontroller *) controller{    Wclog (@ "%@", [Nsthread CurrentThread]);    Refresh Table    [Self.tableview reloaddata];}

iOS development: How do I contact the server in XMPP?

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.