原生微信分享__iOS开发

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

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,008评论 25 708
  • Swift版本点击这里欢迎加入QQ群交流: 594119878最新更新日期:18-09-17 About A cu...
    ylgwhyh阅读 25,522评论 7 249
  • 生活在这个急剧变化的后工业时代,每个人都想摆脱简单、低效和平淡的生活,追求那些更加高效、更加丰富、更加刺激的东西。...
    二郎会阅读 246评论 0 1
  • 文/依风 静静的想着过去 很多事却总过不去 慢慢的走进心底 看见的全都是你 那一个夏季 我们不期而遇 从此情缘依依...
    新雅遇见读书阅读 362评论 0 0
  • 平安夜哄娃卧倒在被窝,等Santa来. 不出所料一会就梦乡去也。 大宝也睡了,呆滴也睡了,夜深人静了,我代Sant...
    对面的我阅读 272评论 1 0