微信原生的分享(轉(zhuǎn)載)

轉(zhuǎn)載自 http://www.cnblogs.com/mancong/p/5807924.html

一.微信原生的分享--準(zhǔn)備工作.

  1. 需要申請(qǐng)微信AppId.
  1. 導(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)入)

  2. 該項(xiàng)目中的Bundle Identifier 應(yīng)該填向微信注冊(cè)的Bundle Identifier

  3. 注冊(cè)微信 (回調(diào)的時(shí)候用到,告訴微信,從微信返回到哪個(gè)APP)
    Target --> info --> URL Types --> +按鈕 --> 填寫identifier 和 URL Schemes. 前一個(gè)是標(biāo)識(shí)符,一般填@"weixin".后一個(gè)是注冊(cè)的微信appId. 比如"wx19a984b788a8a0b1".(注釋: 假的appid)

  4. 添加微信白名單
    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

  1. 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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 實(shí)現(xiàn)支付寶支付的準(zhǔn)備工作: 1.向支付寶簽約,成為支付寶的商戶 簽約完成后,支付寶會(huì)提供一些必要的數(shù)據(jù)給我們 商戶...
    Anson楊春安閱讀 8,631評(píng)論 0 6
  • 簡介: 一直看大牛的文章了,學(xué)到很多,也不敢寫,怕自己水平有限,寫的東西讓大家笑話. 最近公司要做簡單的分享功能,...
    愿世界和平閱讀 2,313評(píng)論 0 0
  • 最后一分鐘,換到一張票,不用等到年三十,今天下午就可以回家過年去了。 今天早上出門,覺得更冷清了,小區(qū)里靜悄悄的,...
    文蟲閱讀 592評(píng)論 12 10
  • 客人對(duì)于旅游源于想象和向往,而旅途中給客人帶來游玩的樂趣和還未游玩前客人的期待,而構(gòu)成了旅游這個(gè)讓人魂?duì)繅?mèng)縈的一件...
    翟金曼閱讀 493評(píng)論 1 0
  • 翻譯對(duì)比 1. The former bookseller accounts for more than half...
    潘慧_06b3閱讀 1,599評(píng)論 0 0

友情鏈接更多精彩內(nèi)容