美文网首页
原生微信分享__iOS开发

原生微信分享__iOS开发

作者: Stone_熊小叔 | 来源:发表于2017-06-06 16:11 被阅读0次

part 1 微信开发者平台注册AppKey

微信开发者平台直达车


part 2 配置环境

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions在此方法中注册微信Appkey

    //微信分享
    [WXApi registerApp:WeixinAppID];

以下方法处理回调

-(BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options{
    return [WXApi handleOpenURL:url delegate:self];
}

//onReq是微信终端向第三方程序发起请求,要求第三方程序响应。第三方程序响应完后必须调用sendRsp返回。在调用sendRsp返回时,会切回到微信终端程序界面。
-(void) onReq:(BaseReq*)req{
    
}
//如果第三方程序向微信发送了sendReq的请求,那么onResp会被回调。sendReq请求调用后,会切到微信终端程序界面。
-(void) onResp:(BaseResp*)resp{
    if([resp isKindOfClass:[SendMessageToWXResp class]]) {
        
        switch (resp.errCode) {
            case WXSuccess:
            {
                UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"成功" message:@"分享成功" preferredStyle:UIAlertControllerStyleAlert];
                [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil]];
                [self.window.rootViewController presentViewController:alert animated:YES completion:nil];
            }
                break;
            case WXErrCodeUserCancel:
                break;
            default:
            {
                UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"失败" message:@"分享失败" preferredStyle:UIAlertControllerStyleAlert];
                [alert addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil]];
                [self.window.rootViewController presentViewController:alert animated:YES completion:nil];
            }
                
                break;
        }
    }
}

最后在需要的时候调用微信接口...

-(void)shareClick{
    NSArray *contentArray = @[@{@"name":@"微信",@"icon":@"sns_icon_7"},
                              @{@"name":@"朋友圈",@"icon":@"sns_icon_8"},
                              @{@"name":@"微信收藏",@"icon":@"sns_icon_9"}
                              ];
    QLShareView *shareView = [[QLShareView alloc] init];
    [shareView addShareItems:self.view shareItems:contentArray selectShareItem:^(NSInteger tag, NSString *title) {
        NSLog(@"%ld --- %@", tag, title);
        if (tag < 3) {
            //调用微信分享
            [self SendTextImageLinkWithTag:(int)tag];
        }else{
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"别点了,好难受~" message:@"晚上吃啥????" preferredStyle:UIAlertControllerStyleAlert];
            [alert addAction:[UIAlertAction actionWithTitle:@"你说的都对" style:UIAlertActionStyleDefault handler:nil]];
            [self presentViewController:alert animated:YES completion:nil];
        }
    }];

}

/** 发送图片文字链接*/
- (void)SendTextImageLinkWithTag: (int )tag{
    if (![WXApi isWXAppInstalled]) {
        NSLog(@"请移步App Store去下载微信客户端");
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"哎呦喂,失败🐦~" message:@"请移步App Store去下载微信客户端" preferredStyle:UIAlertControllerStyleAlert];
        [alert addAction:[UIAlertAction actionWithTitle:@"你说的都对" style:UIAlertActionStyleDefault handler:nil]];
        [self presentViewController:alert animated:YES completion:nil];
    }else {
        SendMessageToWXReq *sendReq = [[SendMessageToWXReq alloc] init];
        sendReq.bText = NO;
        sendReq.scene = tag;
        
        // 创建分享内容
        WXMediaMessage *message = [WXMediaMessage message];
        //分享标题
        message.title = @"晚上想吃啥???恩,想想就好了!!";
        // 描述
        message.description = @"微信微信微信微信微信微信微信微信微信微信测试";
        //分享图片,使用SDK的setThumbImage方法可压缩图片大小
        [message setThumbImage:[UIImage imageNamed:@"1"]];
        
        //创建多媒体对象
        WXWebpageObject *webObj = [WXWebpageObject object];
        // 点击后的跳转链接
        webObj.webpageUrl = @"www.baidu.com";
        message.mediaObject = webObj;
        sendReq.message = message;
        [WXApi sendReq:sendReq];
    }
}

part 3 效果图

哎呀,要被闪瞎了...手速快怪我咯...

Untitled.gif

part 4 注意的坑...

  • 总共有三个点
1. 记得设置Schemes,否则应用无法回跳,只能停留在微信界面
2. 添加微信白名单,否则会报```-canOpenURL: failed for URL: "weixin://app/你的微信Appkey/" - error: "This app is not allowed to query for scheme weixin"```错误
3. 新特性要求 App 内访问的网络必须使用HTTPS协议,否则报```App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist file.```错误
  • 不多说,上解决方案图
WechatIMG242.jpeg

** 刚刚开始写简书,欢迎各位大佬指导...**

相关文章

  • 原生微信分享__iOS开发

    part 1 微信开发者平台注册AppKey 微信开发者平台直达车 part 2 配置环境 - (BOOL)ap...

  • 从一个NSString中获取指定字节数的子串(注:不是字符数)

    NSString 懂的人一看就知道,这是古老的iOS OC开发了..... 例如,在微信分享时,微信对分享内容有...

  • iOS微信原生分享

    首先去微信开放平台注册添加应用 在微信开发平台获取已审核通过的AppId微信.开放平台 用CocoaPods集成S...

  • IOS 微信原生登录、分享

    一、微信登录 1.向微信注册你的应用程序id 请到 开发者应用登记页面 进行登记,登记并选择移动应用进行设置后,将...

  • 简单实用的iOS分享方式iOS_Share

    搞iOS开发的人,都会碰到一个问题,就是iOS分享,主要是分享到微信、QQ、新浪微博等社交媒体。分享实现的方式有以...

  • iOS开发-微信分享

    导入SKD 封装 前言:公司活动需要加入微信分享,有多处调用,就地封装了。微信开放平台 1. 导入SDK 1.在微...

  • iOS 微信编译速度优化分享

    iOS 微信编译速度优化分享iOS 微信编译速度优化分享

  • 天信红包开发 原生APP

    天信红包开发 原生APP找周经理:182微1918电1929;APP何为原生开发?何为混合开发? 1. 原生开发(...

  • iOS-微信分享

    一、微信原生的分享--准备工作.1、在微信开发平台获取已审核通过的AppId和AppSecrect2、导入微信SD...

  • iOS原生分享

    前段时间看到一个帖子,分享时无法显示微信和QQ,最后发现是个笑话.哪位小伙子就没有安装微信和QQ. iOS原生分享...

网友评论

      本文标题:原生微信分享__iOS开发

      本文链接:https://www.haomeiwen.com/subject/nsngfxtx.html