part 1 微信開(kāi)發(fā)者平臺(tái)注冊(cè)AppKey
part 2 配置環(huán)境
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
在此方法中注冊(cè)微信Appkey
//微信分享
[WXApi registerApp:WeixinAppID];
以下方法處理回調(diào)
-(BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options{
return [WXApi handleOpenURL:url delegate:self];
}
//onReq是微信終端向第三方程序發(fā)起請(qǐng)求,要求第三方程序響應(yīng)。第三方程序響應(yīng)完后必須調(diào)用sendRsp返回。在調(diào)用sendRsp返回時(shí),會(huì)切回到微信終端程序界面。
-(void) onReq:(BaseReq*)req{
}
//如果第三方程序向微信發(fā)送了sendReq的請(qǐng)求,那么onResp會(huì)被回調(diào)。sendReq請(qǐng)求調(diào)用后,會(huì)切到微信終端程序界面。
-(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;
}
}
}
最后在需要的時(shí)候調(diào)用微信接口...
-(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) {
//調(diào)用微信分享
[self SendTextImageLinkWithTag:(int)tag];
}else{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"別點(diǎn)了,好難受~" message:@"晚上吃啥????" preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"你說(shuō)的都對(duì)" style:UIAlertActionStyleDefault handler:nil]];
[self presentViewController:alert animated:YES completion:nil];
}
}];
}
/** 發(fā)送圖片文字鏈接*/
- (void)SendTextImageLinkWithTag: (int )tag{
if (![WXApi isWXAppInstalled]) {
NSLog(@"請(qǐng)移步App Store去下載微信客戶端");
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"哎呦喂,失敗??~" message:@"請(qǐng)移步App Store去下載微信客戶端" preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"你說(shuō)的都對(duì)" style:UIAlertActionStyleDefault handler:nil]];
[self presentViewController:alert animated:YES completion:nil];
}else {
SendMessageToWXReq *sendReq = [[SendMessageToWXReq alloc] init];
sendReq.bText = NO;
sendReq.scene = tag;
// 創(chuàng)建分享內(nèi)容
WXMediaMessage *message = [WXMediaMessage message];
//分享標(biāo)題
message.title = @"晚上想吃啥???恩,想想就好了!!";
// 描述
message.description = @"微信微信微信微信微信微信微信微信微信微信測(cè)試";
//分享圖片,使用SDK的setThumbImage方法可壓縮圖片大小
[message setThumbImage:[UIImage imageNamed:@"1"]];
//創(chuàng)建多媒體對(duì)象
WXWebpageObject *webObj = [WXWebpageObject object];
// 點(diǎn)擊后的跳轉(zhuǎn)鏈接
webObj.webpageUrl = @"www.baidu.com";
message.mediaObject = webObj;
sendReq.message = message;
[WXApi sendReq:sendReq];
}
}
part 3 效果圖
哎呀,要被閃瞎了...手速快怪我咯...
Untitled.gif
part 4 注意的坑...
- 總共有三個(gè)點(diǎn)
1. 記得設(shè)置Schemes,否則應(yīng)用無(wú)法回跳,只能停留在微信界面
2. 添加微信白名單,否則會(huì)報(bào)```-canOpenURL: failed for URL: "weixin://app/你的微信Appkey/" - error: "This app is not allowed to query for scheme weixin"```錯(cuò)誤
3. 新特性要求 App 內(nèi)訪問(wèn)的網(wǎng)絡(luò)必須使用HTTPS協(xié)議,否則報(bào)```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.```錯(cuò)誤
- 不多說(shuō),上解決方案圖
WechatIMG242.jpeg
** 剛剛開(kāi)始寫(xiě)簡(jiǎn)書(shū),歡迎各位大佬指導(dǎo)...**