Uiactivityviewcontroller Usage (iii)--custom sharing (lighter than third-party shared libraries)

Source: Internet
Author: User

Use Uiactivityviewcontroller to customize the way you share,

The following code to create a Uiactivityviewcontroller, can be used airdrop, mail, QQ, sharing, airdrop sharing is data transmission, the other three items are text sharing, Can be controlled in the item class that implements the Uiactivityitemsource protocol,

//creates a uiactivityviewcontroller and renders-(void) createandshowactivityviewcontroller{//construct custom activities, select only the system's mail and AirDrop typesNsarray *activities = @[[qqactivityNew], [wechatactivityNew]]; Nsarray*excludedactivitytypes =@[uiactivitytypeposttofacebook, Uiactivitytypeposttotwitter,                                    Uiactivitytypeposttoweibo, Uiactivitytypemessage, //Uiactivitytypemail,Uiactivitytypeprint, Uiactivitytypecopytopasteb Oard, Uiactivitytypeassigntocontact, uiactivitytypes Avetocameraroll, Uiactivitytypeaddtoreadinglist, UIA                                    Ctivitytypeposttoflickr, Uiactivitytypeposttovimeo,        Uiactivitytypeposttotencentweibo]; //construct the data item to be sentShareditem *item = [[Shareditem alloc] InitWithData:self.data andlink:@"http://www.baidu.com?pid=12j12kjdlsaj&d=jsdlkjfch9802"]; //Build UiactivityviewcontrollerUiactivityviewcontroller *activityviewcontroller =[[Uiactivityviewcontroller alloc] initwithactivityitems:@[item] applicationactivities:activities]; Activityviewcontroller.excludedactivitytypes=excludedactivitytypes; Activityviewcontroller.completionhandler= ^ (NSString *Activitytype, BOOL completed) {        //Uiactivityviewcontroller Call this block when exiting//Whether successful or not, exit directly[Self.DelegateBacktomainview];    }; [Self Presentviewcontroller:activityviewcontroller animated:yes completion:nil];}

Activity classes that are only posted

#import<UIKit/UIKit.h>@interfacewechatactivity:uiactivity@end#import "WeChatActivity.h"@implementationwechatactivity-(nsstring*) activitytype{//equivalent to Uiactivitytypeairdrop, which can be used to determine what activity type    return @"Com.hsun.ShareViaWeChat";}-(nsstring*) activitytitle{//the title displayed    return @"";}-(uiimage*) activityimage{//The displayed icons, iOS8 and later are color, size can be viewed in the document of this method    return[UIImage imagenamed:@"Weixin"];}-(void) Preparewithactivityitems: (Nsarray *) activityitems{//in doing so, items are the data to be transferred and the APIs that can be called directly hereNSLog (@"%@", Activityitems);}+(uiactivitycategory) activitycategory{//determine the position shown in Uiactivityviewcontroller, the top is airdrop, the middle is share, the following is the action    returnUiactivitycategoryshare;}-(void) performactivity{//This method is not called by a custom Uiviewcontroller. Here are some things you can do to get rid of Uiactivityviewcontroller at the end[self activitydidfinish:yes];}-(BOOL) Canperformwithactivityitems: (Nsarray *) activityitems{//depending on the type of item, you can determine whether the behavior service operation    returnYES;}@end

The corresponding item class (where you can control different activity to make different data transfers)

#import<Foundation/Foundation.h>#import<UIKit/UIKit.h>@interfaceshareditem:nsobject<uiactivityitemsource>-(Instancetype) Initwithdata: (nsdata*) Data andLink: (NSString*) Link;@end#import "SharedItem.h"@interfaceShareditem () @property (nonatomic, strong) NSData*data; @property (nonatomic, strong) NSString*link;@end@implementationShareditem-(Instancetype) Initwithdata: (NSData *) data andlink: (NSString *) link{ Self=[Super Init]; if(self) {_data=data; _link=link; }    returnSelf ;}-(instancetype) init{@throw[NSException exceptionwithname:@" do not use"Reason@"Initwithdata:andlink to initiate."Userinfo:nil]; returnNil;}#pragmamark-uiactivityitemsource-(ID) Activityviewcontrollerplaceholderitem: (Uiactivityviewcontroller *) activityviewcontroller{return[NSData data];}-(ID) Activityviewcontroller: (Uiactivityviewcontroller *) Activityviewcontroller itemforactivitytype: (NSString *) activitytype{//if it is through AirDrop, return directly to NSData    if([Activitytype Isequaltostring:uiactivitytypeairdrop]) {returnSelf.data; }    //all the others are back links    returnSelf.link;}-(nsstring*) Activityviewcontroller: (Uiactivityviewcontroller *) Activityviewcontroller SubjectForActivityType: ( NSString *) activitytype{return @"Share Data";}-(nsstring*) Activityviewcontroller: (Uiactivityviewcontroller *) Activityviewcontroller Datatypeidentifierforactivitytype: (NSString *) activitytype{//if it is through AirDrop, return directly to NSData    if([Activitytype Isequaltostring:uiactivitytypeairdrop]) {return @"Com.hsun.customUTI.hsundata"; }    //all the others are back links    returnNil;}

Uiactivityviewcontroller Usage (iii)--custom sharing (lighter than third-party shared libraries)

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.