flutter 插件开发:分享插件只针对ios平台

在开发flutter应用的时候总会遇到其他插件解决不了的问题,我遇到的问题就是在ios平台分享时候调起的是系统的分享但是,分享栏里面没有我想要的facebook和twitter平台,所以不得已写了一个插件但是只针对ios原生平台,下面介绍一下怎么实现插件的流程。

插件的原理是dart建立一个二进制流来于原生平台进行通信,flutter相当于客户端,ios平台和android平台相当于主机。

一:创建flutter插件工程

1:android平台:

利用android studio 创建一个插件工程,在开发安卓插件的时候一定要打开example项目的android目录右键-> flutter -> open android module in android studio,在国内环境编译此项目的时候可能会出现编译错误的问题,建议在开启VPN的环境下下载对应的依赖,坑会比较少。在对应的地方添加dart代码和java代码

2:Ios平台:

ios平台在写OC代码得时候需要先build一下example工程:flutter build ios这样才能找到插件的原生代码,这一点谨记

SharePlugin.h 

#import <Flutter/Flutter.h>

@interface SharePlugin : NSObject<FlutterPlugin>
@end

SharePlugin.m

#import "SharePlugin.h"
#import "ShareHelper.h"
#import <Social/Social.h>

@implementation SharePlugin
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
  FlutterMethodChannel* channel = [FlutterMethodChannel
      methodChannelWithName:@"share_plugin"
            binaryMessenger:[registrar messenger]];
  SharePlugin* instance = [[SharePlugin alloc] init];
  [registrar addMethodCallDelegate:instance channel:channel];
}

- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
  if ([@"getPlatformVersion" isEqualToString:call.method]) {
    result([@"iOS " stringByAppendingString:[[UIDevice currentDevice] systemVersion]]);
  }
    
    // 分享到twitter平台
    if([@"share" isEqualToString:call.method]) {
        NSDictionary *arguments = [call arguments];
        NSString* shareContent = arguments[@"content"];
        NSString* shareUrl = arguments[@"url"];
        
        [ShareHelper shareToPlatformType:SLServiceTypeTwitter withContent:shareContent withShareUrl:shareUrl];
//        [ShareHelper shareToPlatformType:SLServiceTypeFacebook withContent:shareContent withShareUrl:shareUrl];
        result(nil);
    }
    
    // 分享到facebook 平台
    if([@"shareToFacebook" isEqualToString:call.method]) {
        NSDictionary* arguments = [call arguments];
        NSString* shareContent = arguments[@"content"];
        NSString* shareUrl = arguments[@"url"];
        
        [ShareHelper shareToPlatformType:SLServiceTypeFacebook withContent:shareContent withShareUrl:shareUrl];
        result(nil);
    }
    
    if([@"shareToSnapchat" isEqualToString:call.method]) {
        NSDictionary* arguments = [call arguments];
        NSString* shareContent = arguments[@"content"];
        NSString* shareUrl = arguments[@"url"];
        
        [ShareHelper shareToPlatformType:@"com.toyopagroup.picaboo.share" withContent:shareContent withShareUrl:shareUrl];
        result(nil);
    }
    
    if([@"shareToIns" isEqualToString:call.method]) {
        NSDictionary* arguments = [call arguments];
        NSString* shareContent = arguments[@"content"];
        NSString* shareUrl = arguments[@"url"];
//        [ShareHelper shareToPlatformType:@"com.burbn.instagram.shareextension" withContent:shareContent withShareUrl:shareUrl];
        [ShareHelper shareToPlatformType:SLServiceTypeLinkedIn withContent:shareContent withShareUrl:shareUrl];
        result(nil);
    }
    
    if([@"shareToWeixin" isEqualToString:call.method]) {
        NSDictionary* arguments = [call arguments];
        NSString* shareContent = arguments[@"content"];
        NSString* shareUrl = arguments[@"url"];
        [ShareHelper shareToPlatformType:@"com.tencent.xin.sharetimeline" withContent:shareContent withShareUrl:shareUrl];
        result(nil);
        
    }
    
}

@end

 ShareHelper.h

//
//  ShareHelper.h
//  Pods
//
//  Created by 明日虫洞 on 2022/10/8.
//

#ifndef ShareHelper_h
#define ShareHelper_h

#import <Foundation/Foundation.h>

@interface ShareHelper : NSObject
+(void)shareToPlatformType:(NSString *)platformType withContent:(NSString *)content withShareUrl:(NSString *)url;

@end


#endif /* ShareHelper_h */

ShareHelper.m

//
//  ShareHelper.m
//  Pods-Runner
//
//  Created by 明日虫洞 on 2022/10/8.
//

#import <Foundation/Foundation.h>
#import <Social/Social.h>
#import "ShareHelper.h"
#import "PackageUtil.h"

@implementation ShareHelper
+(void)shareToPlatformType:(NSString *)platformType withContent:(NSString *)content withShareUrl:(NSString *)url {
    if(platformType == nil || platformType.length == 0) {
        NSLog(@"分享平台字符串不正确");
        return;
    }
    
    // 检查facebook 平台
    if([platformType isEqualToString:SLServiceTypeFacebook] && ![[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"fb://"]]) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"itms-apps://itunes.apple.com/app/id284882215"]];
        NSLog(@"UnInstall facebook");
        return;
    }
    
    // 检查twitter 平台
    if([platformType isEqualToString:SLServiceTypeTwitter] && ![[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"twitter://"]]) {
        NSLog(@"不可以打开twitter平台");
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"itms-apps://itunes.apple.com/app/id333903271"]];
        NSLog(@"Uninstall twitter");
        return;
    }

    // 检查instagram 平台 com.burbn.instagram.shareextension
    if([platformType isEqualToString:@"com.burbn.instagram.shareextension"] && ![[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"instagram://app"]]) {
        NSLog(@"不能打开Ins平台");
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"itms-apps://itunes.apple.com/app/id389801252"]];
        return;
    } else {
        NSLog(@"可以打开ins");
    }
    
    if([platformType isEqualToString:@"com.toyopagroup.picaboo.share"] && ![[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"snapchat://"]]) {
        NSLog(@"未安装snapchat");
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"itms-apps://itunes.apple.com/app/id447188370"]];
        return;
    } else {
        NSLog(@"可以打开snapchat");
    }
    
    if([platformType isEqualToString:@"com.tencent.xin.sharetimeline"] && ![[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"wechat://"]]) {
        NSLog(@"打不开微信");
    }
    
//    if(![SLComposeViewController isAvailableForServiceType:platformType]) {
//        NSLog(@"软件未配置登录信息");
//        return;
//    }
    
//    SLComposeViewController* cvc = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeSinaWeibo];
//    NSLog(@"%@",cvc);
    
//    [SLComposeViewController]
//    SLComposeViewController
    
    // 创建服务类型控制器 调起对应app的控制器
    SLComposeViewController *composeVC = [SLComposeViewController composeViewControllerForServiceType:platformType];
    
    NSLog(@"---->>>%@",composeVC);
    
    // 分享连接
    [composeVC addURL:[NSURL URLWithString:url]];
    // 分享内容
    [composeVC setInitialText:content];
    
    
    if([[[UIApplication sharedApplication] keyWindow].rootViewController isKindOfClass:[UINavigationController class]]) {
        UINavigationController *vc = (UINavigationController *)[[UIApplication sharedApplication] keyWindow].rootViewController;
        
        [vc pushViewController:composeVC animated:YES];
    } else {
        UINavigationController *vc = (UINavigationController *)[[UIApplication sharedApplication] keyWindow].rootViewController;
        [vc presentViewController:composeVC animated:YES completion:^{
            NSLog(@"分享完毕");
        }];
        
        
    }
    
    composeVC.completionHandler = ^(SLComposeViewControllerResult result) {
        if(result == SLComposeViewControllerResultDone) {
            NSLog(@"分享完成");
        } else if(result == SLComposeViewControllerResultCancelled) {
            NSLog(@"取消分享");
        }
    };
    
}

@end

二: 集成到自己的项目里面

在自己的项目里面根目录里面添加一个目录,名字暂定为plugins,将插件的项目整个复制到里面去

然后再pubspec.yaml文件中添加自己写的插件路径:

flutter_ffmpeg: ^0.4.2
  flutter_linkify: ^5.0.2
#  ffmpeg_kit_flutter: ^4.5.1

  share_plugin:
    path: plugins/share_plugin

这里注意一下格式哦,至此插件就可以使用了 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值