iOS 原生分享

UIActivityViewController是iOS原生彈窗式分享

UIActivityViewController的使用

// 1、設(shè)置分享的內(nèi)容,并將內(nèi)容添加到數(shù)組中
    UIImage *shareImage = [UIImage imageNamed:@"shareImage.png"];
    NSURL *shareUrl = [NSURL URLWithString:@"http://www.itdecent.cn/u/e12f189a320d"];
    NSArray *activityItemsArray = @[shareImage,shareUrl];

    // 自定義的CustomActivity,繼承自UIActivity
    CustomActivity *customActivity = [[CustomActivity alloc]initWithTitle:@"" ActivityImage:[UIImage imageNamed:@"custom.png"] URL:shareUrl ActivityType:@"Custom"];
    NSArray *activityArray = @[customActivity];

    // 2、初始化控制器,添加分享內(nèi)容至控制器
    UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems:activityItemsArray applicationActivities:activityArray];
    activityVC.modalInPopover = YES;
     NSMutableArray *excludeArray = [@[UIActivityTypeAirDrop,UIActivityTypeMessage,UIActivityTypePrint,UIActivityTypePostToTwitter,UIActivityTypePostToFacebook,UIActivityTypeOpenInIBooks,UIActivityTypePostToVimeo,UIActivityTypePostToFlickr,UIActivityTypeAddToReadingList,UIActivityTypeSaveToCameraRoll,UIActivityTypeAssignToContact,UIActivityTypeCopyToPasteboard,UIActivityTypeMail] mutableCopy];
    activityVC.excludedActivityTypes = excludeArray;
    // 3、設(shè)置回調(diào)
    if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0) {
        // ios8.0 之后用此方法回調(diào)
        UIActivityViewControllerCompletionWithItemsHandler itemsBlock = ^(UIActivityType __nullable activityType, BOOL completed, NSArray * __nullable returnedItems, NSError * __nullable activityError){
            NSLog(@"activityType == %@",activityType);
            if (completed == YES) {
                NSLog(@"completed");
                
            }else{
                NSLog(@"cancel");
            }
        };
        activityVC.completionWithItemsHandler = itemsBlock;
    }else{
        // ios8.0 之前用此方法回調(diào)
        UIActivityViewControllerCompletionHandler handlerBlock = ^(UIActivityType __nullable activityType, BOOL completed){
            NSLog(@"activityType == %@",activityType);
            if (completed == YES) {
                NSLog(@"completed");
            }else{
                NSLog(@"cancel");
            }
        };
        activityVC.completionHandler = handlerBlock;
    }
    // 4、調(diào)用控制器
    [self presentViewController:activityVC animated:YES completion:nil];

CustomActivity.h

- (instancetype)initWithTitle:(NSString *)title ActivityImage:(UIImage *)activityImage URL:(NSURL *)url ActivityType:(NSString *)activityType;

CustomActivity.m

@interface CustomActivity ()
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSURL *url;
@property (nonatomic, copy) UIImage *image;
@property (nonatomic, copy) NSString *type;
@end

- (instancetype)initWithTitle:(NSString *)title ActivityImage:(UIImage *)activityImage URL:(NSURL *)url ActivityType:(NSString *)activityType{
    self = [super init];
    if (self) {
        self.title = title;
        self.url = url;
        self.image = activityImage;
        self.type = activityType;
    }
    return self;
}
/**
 決定自定義CustomActivity在UIActivityViewController中顯示的位置。
 最上層:AirDrop
 中層:Share,即UIActivityCategoryShare
 中層:Action,即UIActivityCategoryAction
 */
+ (UIActivityCategory)activityCategory{
    return UIActivityCategoryAction;
}

- (NSString *)activityType{
    return _type;
}

- (NSString *)activityTitle {
    return _title;
}
/**
 這個(gè)得注意,當(dāng)self.activityCategory = UIActivityCategoryAction時(shí),系統(tǒng)默認(rèn)會渲染圖片,所以不能重寫為 - (UIImage *)activityImage {return _image;}
 */
- (UIImage *)_activityImage {
    return _image;
}

- (NSURL *)activityUrl{
    return _url;
}

- (BOOL)canPerformWithActivityItems:(NSArray *)activityItems {
    return YES;
}
/**
 準(zhǔn)備分享所進(jìn)行的方法,通常在這個(gè)方法里面,把item中的東西保存下來,items就是要傳輸?shù)臄?shù)據(jù)。
 */
- (void)prepareWithActivityItems:(NSArray *)activityItems {

}
/**
  1、這里就可以關(guān)聯(lián)外面的app進(jìn)行分享操作了
  2、也可以進(jìn)行一些數(shù)據(jù)的保存等操作
  3、操作的最后必須使用下面方法告訴系統(tǒng)分享結(jié)束了
 */
- (void)performActivity {
    [self activityDidFinish:YES];
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容