原生微信分享__iOS开发

2017-06-06  本文已影响0人  Stone_熊小叔

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

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

上一篇 下一篇

猜你喜欢

热点阅读