iOS development--xmppframework--friends list (v)

Source: Internet
Author: User
<span id="Label3"></p><p><p>In the previous article, we discussed debugging and Buddy module, This one, after the introduction of the friend module, we talk about the display of the friend List.</p></p><p><p>Remember in the previous article, we turned off the Auto-buddy list, so we chose to manually pull the buddy list in the Controller's-(void) viewdidload, and add the Agent.</p></p><pre><pre><span style="color: #000000;">[[xmppmanager sharedinstance].xmpproster fetchroster];</span> <span style="color: #000000;">[[xmppmanager sharedinstance].xmpproster adddelegate:self delegatequeue:dispatch_get_main_queue ()]; [[xmppmanager sharedinstance].xmppstream adddelegate:self delegatequeue:dispatch_get_main_queue ()];</span></pre></pre><pre><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">close friend Sync</span></span>- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>) xmpprosterdidendpopulating: (xmpproster *<span style="color: #000000;"><span style="color: #000000;">) Sender {NSLog (</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">close friend sync, query database</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">); Dispatch_async (dispatch_get_main_queue (),</span></span>^<span style="color: #000000;"><span style="color: #000000;">{[self queryfriendlist]; });}</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Find a friend</span></span>- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>) xmpproster: (xmpproster *) Sender didreceiverosterpush: (xmppiq *<span style="color: #000000;"><span style="color: #000000;">) IQ {dispatch_after (dispatch_time (dispatch_time_now, (int64_t) (</span></span><span style="color: #800080;"><span style="color: #800080;">0.5f</span></span>* nsec_per_sec)), Dispatch_get_main_queue (), ^<span style="color: #000000;"><span style="color: #000000;">{[self queryfriendlist]; });}</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">#pragma</span></span>Mark xmppstreamdelegate-(<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>) xmppstream: (xmppstream *) Sender didreceivepresence: (xmpppresence *<span style="color: #000000;"><span style="color: #000000;">) Presence {</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">If this is true, the other person refuses or deletes you.</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>([presence.type isequaltostring:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">unsubscribed</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">]) {[[xmppmanager sharedinstance].xmpproster removeuser:presence.</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> from</span><span style="color: #000000;"><span style="color: #000000;">]; }}</span></span></pre><p><p>After manually pulling the friend list, the first proxy method is picked up, and we do a friend lookup locally in this Method. When the server side receives a friend list, the second proxy method is called, and the list is saved to the local coredata, so we'll find it again locally.</p></p><p><p>The third proxy method is the call after the presence message is Received. We are in the process of receiving a friend request is rejected or deleted (that is, unsubscribe), This side of the action is to remove the friend Removed.</p></p><p><p>Next is the most important local CoreData lookup function, the-(void) queryfriendlist Function.</p></p><p><p>Finding data from CoreData is divided into three steps, creating a Find request, locating the entity, setting the data sort or filtering Mode. The code doesn't need to be written, and Apple has a code block for us to Use. Here are the Following:</p></p><p><p></p></p><p><p>Just drag this piece of code where we need it.</p></p><pre>- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">) queryfriendlist {nsfetchrequest</span></span>*fetchrequest =<span style="color: #000000;"><span style="color: #000000;">[[nsfetchrequest alloc] init]; Xmpprostercoredatastorage</span></span>*storage =<span style="color: #000000;"><span style="color: #000000;">[xmpprostercoredatastorage sharedinstance]; Nsentitydescription</span></span>*entity = [nsentitydescription entityforname:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">Xmppusercoredatastorageobject</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">inManagedObjectContext:storage.mainThreadManagedObjectContext]; [fetchrequest setentity:entity]; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">specify criteria for filtering which objects to fetch</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">nspredicate *predicate = [nspredicate predicatewithformat:@ "subscription = ' Both '"];</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">[fetchrequest setpredicate:predicate]; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">specify how the fetched objects should be sorted</span></span>Nssortdescriptor *sortdescriptor = [[nssortdescriptor alloc] initwithkey:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">Jidstr</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">ascending:yes]; [fetchrequest Setsortdescriptors:[nsarray arraywithobjects:sortdescriptor, nil]]; Nserror</span></span>*error =<span style="color: #000000;"><span style="color: #000000;">nil; Nsarray</span></span>*fetchedobjects = [storage.mainthreadmanagedobjectcontext Executefetchrequest:fetchrequest error:&<span style="color: #000000;"><span style="color: #000000;">error]; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(fetchedobjects = =<span style="color: #000000;"><span style="color: #000000;">Nil) { </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #000000;"><span style="color: #000000;">} self.friendlist</span></span>=<span style="color: #000000;"><span style="color: #000000;">fetchedobjects; [self.tableview reloaddata];}</span></span></pre><p><p>After the drag, modify the class name, sorting and filtering methods, and then do some personalized operation, it is Possible.</p></p><p><p>It is important to note that after we have fetchedobjects these results, we can immediately switch to the model we need, which is convenient to use, and each object in the array is</p></p><p class="p1"><p class="p1"><span class="s1">Xmppusercoredatastorageobject object, go into the header file and see more Clearly.</span></p></p><p class="p1"><p class="p1"><span class="s1">next, Let's Debug.</span></p></p><p class="p1"><p class="p1"><span class="s1">Run the Program.</span></p></p><p class="p1"><p class="p1"><span class="s1">Add friends to the Messages app First.</span></p></p><p class="p1"><p class="p1"><span class="s1"></span></p></p><p class="p1"><p class="p1">Program Proxy methods</p></p><pre><pre>-(<span style="color: #0000ff;">void</span>) xmppstream: (xmppstream *) sender didreceivepresence: (xmpppresence *) presence;</pre></pre><p class="p1"><p class="p1"><span class="s1"><span class="s2"><span class="s1"><span class="s3"><span class="s1"><span class="s3"><span class="s1">Receive a subscription Request.</span></span></span></span></span></span></span></p></p><pre><pre><presence xmlns=<span style="color: #800000;">"</span><span style="color: #800000;">jabber:client</span><span style="color: #800000;">"</span> type=<span style="color: #800000;">"</span><span style="color: #800000;">subscribe</span><span style="color: #800000;">"</span> to= <span style="color: #800000;"> "</span><span style="color: #800000;">[email protected]</span><span style="color: #800000;">"</span><span style="color: #0000ff;"></span>from =<span style="color: #800000;">"</span><span style="color: #800000;">[email protected]</span><span style="color: #800000;">"</span>> </presence></pre></pre><p><p>A subscription request from [email protected], type Subscribe.</p></p><p><p>As we said in the previous article, we automatically agree upon Receipt. We switch to the Buddy list interface</p></p><p><p></p></p><p><p>Success! Zhangsan already exists in our friends List ~</p></p><p class="p1"><p class="p1"><span class="s1"><span class="s2"><span class="s1"><span class="s3"><span class="s1"><span class="s3"><span class="s1">In this article we discussed the list of friends, the next topic we started chatting, see you next Time.</span></span></span></span></span></span></span></p></p><p class="p1"><p class="p1"></p></p><p class="p1"><p class="p1"><span class="s1"><span class="s2"><span class="s1"><span class="s3"><span class="s1"><span class="s3"><span class="s1">XMPP Instant Messenger Communication Group 140147825, Welcome to Exchange ~ We are writing code together Brother ~ Happy Weekend ~</span></span></span></span></span></span></span></p></p><p><p>iOS development--xmppframework--friends list (v)</p></p></span>

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.