微信分享的简单应用

微信分享的简单应用

在微信开放平台上申请应用之后并下载sdk进行导入,
1.先导入需要的框架包:SystemConfiguration.framework, libz.dylib, libsqlite3.0.dylib, libc++.dylib, Security.framework, CoreTelephony.framework, CFNetwork.framework

2.在你的工程文件中选择Build Setting,在"Other Linker Flags"中加入"-Objc -all_load"

在这里插入图片描述3…添加URL types
在这里插入图片描述4.添加白名单,使手机能调用微信app
在这里插入图片描述5.开始使用,
在Apppdelegate.m中导入所需要的头文件
在这里插入图片描述注册微信

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    //注册本地消息推送
    [Publicmethods registerLocalNotification:application];
    //[Publicmethods addLocalNotification:@"haha, 您有一个新的消息。" alertAction:@"打开应用" timeInterval:1.0 isSound:[ShareData sharedSingleton].globalIsSound soundID:[ShareData sharedSingleton].globalSystemSoundID isVibration:[ShareData sharedSingleton].globalIsVibration];
    
    //检查手机设备类型
    [self checkDeviceType];
    
    //启动第三方平台蒲公英SDK,利用其进行软件检测更新
    //[self PgyManager];
    
    //向微信注册:必须真机调试,并且装有微信
    [WXApi registerApp:kWeixinAppID];

重写handleurl方法

#pragma mark - 微信weixin,QQ
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
    if ([url.scheme isEqualToString:kWeixinAppID]) {
        return  [WXApi handleOpenURL:url delegate:[WXApiManager sharedManager]];
        
    }else if ([url.scheme isEqualToString:[NSString stringWithFormat:@"tencent%@",QQOpenId]])    //QQ
    {
        return [QQApiInterface handleOpenURL:url delegate:self];
        
    }else{
        return YES;
    }
}
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
    if ([url.scheme isEqualToString:kWeixinAppID]) {
        return  [WXApi handleOpenURL:url delegate:[WXApiManager sharedManager]];
        
    }else if ([url.scheme isEqualToString:[NSString stringWithFormat:@"tencent%@",QQOpenId]])    //QQ
    {
        return [QQApiInterface handleOpenURL:url delegate:self];
    }else{
        return YES;
    }
}

因为还有其他的分享app,所以根据url.scheme判断id进行区分

在所需要点击分享的ViewController.m中,在点击事件中添加

//分享app链接
-(void)sendLinkContent
{
    /*检查是否安装了微信
     1、把wexin加入白名单:
     info:LSApplicationQueriesSchemes添加weixin
     2、设置url type:wx175ce621e64638e3
     */
    
    //if ([Publicmethods checkAppIsInstalled:kWeixinAppID])//检查是否安装了微信
    if ([WXApi isWXAppInstalled])//检查是否安装了微信
    {
//        UIImage* image = [Publicmethods imageWithFileName:kQRCodeFileName];
//        if (!image)
//            return;
        UIImage *image=[UIImage imageNamed:@"80_4.png"];
       
        //NSData *imageData = UIImagePNGRepresentation(image);
        
        
        UIImage *thumbImage = image;
        
        //压缩图片,避免因图片过大,无法跳转到微信app
       // UIImage *thumbImage = [Publicmethods zoomToScale:image :0.2];
        
        NSString *urlString=[[NSUserDefaults standardUserDefaults]objectForKey:@"versionURL"];  //下载地址
        
        /*
        //分享二维码图片
        [WXApiRequestHandler sendImageData:imageData
                                   TagName:kQRCodeFileName
                                MessageExt:@"下载壹家医宝(医生端APP)"
                                    Action:urlString
                                ThumbImage:thumbImage
                                   InScene:_currentScene];
        */
        
        
         //分享app下载地址
         [WXApiResponseHandler respLinkURL:urlString
                                     Title:@"软件分享:壹家医宝医生端"
                               Description:@"整合式、流程化家庭医生服务管理,助力医改新模式"
                                ThumbImage:thumbImage];
        
    }
    else
    {
        //初始化提示框
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"没有检测手机里安装了微信应用程序,请先安装" preferredStyle: UIAlertControllerStyleAlert];
        [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            //点击按钮的响应事件
            NSLog(@"ok");
        }]];
        //弹出提示框;
        [self presentViewController:alert animated:true completion:nil];
    }
}

用户取消分享回调

#pragma mark - WXApiManagerDelegate
- (void)managerDidRecvMessageResponse:(SendMessageToWXResp *)response
{
    //response.errCode:取消:-2;分享成功:0
    NSString *message;
    if (response.errCode == 0)//分享成功
        message = @"分享成功";
    else
        message = @"用户已取消";
    //初始化提示框
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle: UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //点击按钮的响应事件
        NSLog(@"ok");
    }]];
    //弹出提示框;
    [self presentViewController:alert animated:true completion:nil];
}

如此,可以进行验证微信分享是否可行了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值