轉(zhuǎn)載自 http://www.cnblogs.com/mancong/p/5807924.html
一.微信原生的分享--準(zhǔn)備工作.
- 需要申請(qǐng)微信AppId.
導(dǎo)入系統(tǒng)架包.
SDK文件包括 libWeChatSDK.a,WXApi.h,WXApiObject.h,WechatAuthSDK.四個(gè).
3.導(dǎo)入必要的系統(tǒng)庫.
SystemConfiguration.framework,
libz.dylib,
libsqlite3.0.dylib,
libc++.dylib,
CoreTelephoy.framework (坑一: 這個(gè)庫是必要的,但是微信官方文檔中沒有說到要導(dǎo)入)該項(xiàng)目中的Bundle Identifier 應(yīng)該填向微信注冊(cè)的Bundle Identifier
注冊(cè)微信 (回調(diào)的時(shí)候用到,告訴微信,從微信返回到哪個(gè)APP)
Target --> info --> URL Types --> +按鈕 --> 填寫identifier 和 URL Schemes. 前一個(gè)是標(biāo)識(shí)符,一般填@"weixin".后一個(gè)是注冊(cè)的微信appId. 比如"wx19a984b788a8a0b1".(注釋: 假的appid)添加微信白名單
info.plist --> 右擊 --> open as --> source Code --> 添加白名單
我是在<key>CFBundleVersion</key>這一行上面添加的. 注意保持正確的鍵值對(duì).別插錯(cuò)了.
<key>LSApplicationQueriesSchemes</key>
<array>
<string>wechat</string>
<string>weixin</string>
</array>
二. 代碼部分.
AppDelegate.h中
(1) 導(dǎo)入
import "WXApi.h"
(2) 遵守協(xié)議
WXApiDelegate
- WXApiDelegate.m中
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// 注冊(cè)微信
[WXApi registerApp:@"wxbbf0646591e4a6d0" withDescription:@"測(cè)試"];
return YES;
}
//被廢棄的方法. 但是在低版本中會(huì)用到.建議寫上
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
return [WXApi handleOpenURL:url delegate:self];
}
//被廢棄的方法. 但是在低版本中會(huì)用到.建議寫上
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
return [WXApi handleOpenURL:url delegate:self];
}
//新的方法
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options
{
return [WXApi handleOpenURL:url delegate:self];
}
// 接收分享回調(diào)通知
//監(jiān)聽通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getOrderPayResult:) name:@"WXShare" object:nil];
[self basicSetting];
// AppID:wxbbf0646591e4a6d0
// 檢查是否裝了微信
if ([WXApi isWXAppInstalled])
{
}
- (void)buttonClciked
{
SendMessageToWXReq * req = [[SendMessageToWXReq alloc] init];
req.text = @"分享的內(nèi)容";
req.bText = YES;
req.scene = WXSceneSession;
[WXApi sendReq:req];
}
- (void)ButtonOneClciked
{
//
WXMediaMessage * message = [WXMediaMessage message];
[message setThumbImage:[UIImage imageNamed:@"seeall@1x"]];
WXImageObject * imageObject = [WXImageObject object];
NSString * filePath = [[NSBundle mainBundle] pathForResource:@"seeall@1x" ofType:@"png"];
imageObject.imageData = [NSData dataWithContentsOfFile:filePath];
message.mediaObject = imageObject;
SendMessageToWXReq * req = [[SendMessageToWXReq alloc] init];
req.bText = NO;
req.message = message;
req.scene = WXSceneTimeline;
[WXApi sendReq:req];
}
- (void)buttonTwoClciked
{
WXMediaMessage * message = [WXMediaMessage message];
message.title = @"標(biāo)題";
message.description = @"副標(biāo)題";
[message setThumbImage:[UIImage imageNamed:@"seeall@1x"]];
WXWebpageObject * webpageObject = [WXWebpageObject object];
webpageObject.webpageUrl = @"www.baidu.com";
message.mediaObject = webpageObject;
SendMessageToWXReq * req = [[SendMessageToWXReq alloc] init];
req.bText = NO;
req.message = message;
req.scene = WXSceneSession;
[WXApi sendReq:req];
}
#import <UIKit/UIKit.h>
#import "WXApi.h"
@interface AppDelegate : UIResponder <UIApplicationDelegate,WXApiDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
#import "AppDelegate.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// 注冊(cè)微信
[WXApi registerApp:@"wxbbf0646591e4a6d0" withDescription:@"測(cè)試"];
return YES;
}
#pragma mark 跳轉(zhuǎn)處理
//被廢棄的方法. 但是在低版本中會(huì)用到.建議寫上
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
return [WXApi handleOpenURL:url delegate:self];
}
//被廢棄的方法. 但是在低版本中會(huì)用到.建議寫上
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
return [WXApi handleOpenURL:url delegate:self];
}
//新的方法
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options
{
return [WXApi handleOpenURL:url delegate:self];
}
#pragma mark 微信回調(diào)方法
- (void)onResp:(BaseResp *)resp
{
/*
WXSuccess = 0, 成功
WXErrCodeCommon = -1, 普通錯(cuò)誤類型
WXErrCodeUserCancel = -2, 用戶點(diǎn)擊取消并返回
WXErrCodeSentFail = -3, 發(fā)送失敗
WXErrCodeAuthDeny = -4, 授權(quán)失敗
WXErrCodeUnsupport = -5, 微信不支持
*/
NSString * strMsg = [NSString stringWithFormat:@"errorCode: %d",resp.errCode];
NSLog(@"strMsg: %@",strMsg);
NSString * errStr = [NSString stringWithFormat:@"errStr: %@",resp.errStr];
NSLog(@"errStr: %@",errStr);
NSString * strTitle;
//判斷是微信消息的回調(diào) --> 是支付回調(diào)回來的還是消息回調(diào)回來的.
if ([resp isKindOfClass:[SendMessageToWXResp class]])
{
// 判斷errCode 進(jìn)行回調(diào)處理
if (resp.errCode == 0)
{
strTitle = [NSString stringWithFormat:@"分享成功"];
}
}
//發(fā)出通知 從微信回調(diào)回來之后,發(fā)一個(gè)通知,讓請(qǐng)求支付的頁面接收消息,并且展示出來,或者進(jìn)行一些自定義的展示或者跳轉(zhuǎn)
NSNotification * notification = [NSNotification notificationWithName:@"WXShare" object:resp.errStr];
[[NSNotificationCenter defaultCenter] postNotification:notification];
}
@end
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
#import "ViewController.h"
#import "WXApi.h"
#import "WechatAuthSDK.h"
#import "WXApiObject.h"
@interface ViewController ()
@end
@implementation ViewController
#pragma mark - 生命周期
#pragma mark viewDidLoad
- (void)viewDidLoad
{
[super viewDidLoad];
// 接收分享回調(diào)通知
//監(jiān)聽通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getOrderPayResult:) name:@"WXShare" object:nil];
[self basicSetting];
// AppID:wxbbf0646591e4a6d0
// 檢查是否裝了微信
if ([WXApi isWXAppInstalled])
{
}
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
button.backgroundColor = [UIColor redColor];
button.frame = CGRectMake(100, 100, 100, 100);
[button addTarget:self action:@selector(buttonClciked) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
UIButton * button1 = [UIButton buttonWithType:UIButtonTypeCustom];
button1.backgroundColor = [UIColor redColor];
button1.frame = CGRectMake(100, 210, 100, 100);
[button1 addTarget:self action:@selector(ButtonOneClciked) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button1];
UIButton * button2 = [UIButton buttonWithType:UIButtonTypeCustom];
button2.backgroundColor = [UIColor redColor];
button2.frame = CGRectMake(100, 320, 100, 100);
[button2 addTarget:self action:@selector(buttonTwoClciked) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button2];
}
- (void)getOrderPayResult:(NSNotification *)notification
{
// 注意通知內(nèi)容類型的匹配
if (notification.object == 0)
{
NSLog(@"分享成功");
}
}
/**
scene: 發(fā)送的目標(biāo)場(chǎng)景,可以選擇發(fā)送到會(huì)話(WXSceneSession)或者朋友圈(WXSceneTimeline),默認(rèn)發(fā)送到會(huì)話.
1.分享或收藏的目標(biāo)場(chǎng)景,通過修改scene場(chǎng)景值實(shí)現(xiàn)。
2.發(fā)送到聊天界面——WXSceneSession
3.發(fā)送到朋友圈——WXSceneTimeline
4.添加到微信收藏——WXSceneFavorite
*/
/** bText:
發(fā)送消息的類型.包括文本消息和多媒體消息兩種.兩者只能選擇其一.不能同時(shí)發(fā)送文本和多媒體消息.
*/
#pragma mark - 系統(tǒng)代理
#pragma mark - 點(diǎn)擊事件
#pragma mark 文字類型分享
- (void)buttonClciked
{
/** SendMessageToWXReq 文字分享內(nèi)容的類
1. text 文字分享的內(nèi)容
2. bText 發(fā)送消息的類型
3. scene 發(fā)送的目標(biāo)場(chǎng)景
*/
SendMessageToWXReq * req = [[SendMessageToWXReq alloc] init];
req.text = @"分享的內(nèi)容";
req.bText = YES;
req.scene = WXSceneSession;
[WXApi sendReq:req];
}
#pragma mark 圖片類型分享
- (void)ButtonOneClciked
{
/** WXMediaMessage 多媒體分享的類
1. setThumbImage 設(shè)置縮略圖
*/
WXMediaMessage * message = [WXMediaMessage message];
[message setThumbImage:[UIImage imageNamed:@"black"]];
WXImageObject * imageObject = [WXImageObject object];
NSString * filePath = [[NSBundle mainBundle] pathForResource:@"seeall@1x" ofType:@"png"];
imageObject.imageData = [NSData dataWithContentsOfFile:filePath];
message.mediaObject = imageObject;
SendMessageToWXReq * req = [[SendMessageToWXReq alloc] init];
req.bText = NO;
req.message = message;
req.scene = WXSceneSession;
[WXApi sendReq:req];
}
#pragma mark 網(wǎng)頁類型分享
- (void)buttonTwoClciked
{
WXMediaMessage * message = [WXMediaMessage message];
message.title = @"標(biāo)題";
message.description = @"副標(biāo)題";
[message setThumbImage:[UIImage imageNamed:@"seeall@1x"]];
WXWebpageObject * webpageObject = [WXWebpageObject object];
webpageObject.webpageUrl = @"www.baidu.com";
message.mediaObject = webpageObject;
SendMessageToWXReq * req = [[SendMessageToWXReq alloc] init];
req.bText = NO;
req.message = message;
req.scene = WXSceneSession;
[WXApi sendReq:req];
}
#pragma mark - 實(shí)現(xiàn)方法
#pragma mark 基本設(shè)置
- (void)basicSetting
{
self.title = @"";
}
#pragma mark - setter & getter
@end