微信接入詳細流程 分享給好友和朋友圈


 

0.先在微信開放平台注冊創建應用地址https://open.weixin.qq.com

在管理中心創建應用提交資料,獲取審核 注意Bundle ID 要填寫正確,不能隨便填

審核完成之后獲取微信的AppID  、AppSecret  審核大概一周時間

1.微信SDK下載地址 https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419319164&lang=zh_CN

選擇使用微信分享、登錄、收藏、支付等功能需要的庫以及文件。點擊下載iOS開發工具包64位

2.下載完成以后打開,需要里面的4個文件

libWeChatSDK.a  、WechatAuthSDK.h  、 WXApi.h  、WXApiObject.h

將這4個文件放到一個文件夾中,拖入你的項目中

3.點擊藍色的工程名字—>Build Phases—>第三行Link Binary 添加相應的庫

圖1

4.點擊藍色的工程名字—>Build Setting—>在右邊搜索Search Paths  

在Library Search Paths 中雙擊打開,點擊左下角+添加微信SDK的路徑  "$(SRCROOT)/Test/SDK1.6.2"

 Test  為項目的名稱  就是將SDK1.6.2這個文件夾直接拖到項目的目錄下 注意這個路徑一定不能錯

5.接下來 需要給你的項目添加 URL type

圖2

其中 添加的URL Types URL Schemes 一欄就要填寫我們再微信開放平台上申請的應用的AppID

 

6.在項目的AppDelegate.h文件中導入微信的頭文件 #import "WXApi.h"  和遵守微信的代理方法

則AppDelegate.h文件變為

#import <UIKit/UIKit.h>

#import <CoreData/CoreData.h>

#import "WXApi.h"

@interface AppDelegate : UIResponder <UIApplicationDelegate,WXApiDelegate>

 

@property (strong, nonatomic) UIWindow *window; 

@end

 

 

7.在AppDelegate.m文件中的這個方法中 注冊微信 WXAppID為微信開放平台獲取的AppID

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

 

    //微信  向微信注冊

    [WXApi registerApp:WXAppID];

 

}

 

 

然后在AppDelegate.m文件中重寫這兩個方法

/**

 *  微信接口重寫的方法

 *

 *  @param application <#application description#>

 *  @param url         <#url description#>

 *

 *  @return <#return value description#>

 */

-(BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url

{

    return [WXApi handleOpenURL:url delegate:self];

}

/**

 *  微信接口重寫的方法

 *

 *  @param application       <#application description#>

 *  @param url               <#url description#>

 *  @param sourceApplication <#sourceApplication description#>

 *  @param annotation        <#annotation description#>

 *

 *  @return <#return value description#>

 */

-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation

{

     return [WXApi handleOpenURL:url delegate:self];

}

 

- (BOOL)application:(UIApplication *)app

            openURL:(NSURL *)url

            options:(NSDictionary *)options {

    

        return [WXApi handleOpenURL:url delegate:self];

       

}

 

/*! @brief 發送一個sendReq后,收到微信的回應

 *

 * 收到一個來自微信的處理結果。調用一次sendReq后會收到onResp。

 * 可能收到的處理結果有SendMessageToWXResp、SendAuthResp等。

 * @param resp具體的回應內容,是自動釋放的

 */

-(void) onResp:(BaseResp*)resp{

    if([resp isKindOfClass:[SendMessageToWXResp class]])

    {

//        NSString *strMsg = [NSString stringWithFormat:@"發送消息結果:%d", resp.errCode];

//        NSLog(@"strmsg %@",strMsg);

        if (resp.errCode == 0) {

            YYCAccount *account = [YYCAccountTool account];

            int uid = account.uid;

            if (uid == 0) {

                [MBProgressHUD showSuccess:@"分享成功!"];

                return;

            }

           接受到微信處理的結果你要做的事

                

            } failure:^(NSError *error) {

                //隱藏蒙版

                HUDHide;

                

                RequestError;

                

                

            }];

            

        }

        

    }

}

 

 

 

8.然后在你需要用到分享的地方.m文件中導入微信的頭文件並遵守代理

#import "WXApi.h"  

<WXApiDelegate>

假如這里有一個按鈕,點擊按鈕進行分享

  1. - (void)viewDidLoad {  
  2.     [super viewDidLoad];  
  3.     // Do any additional setup after loading the view, typically from a nib.  
  4.       
  5.     UIButton *btnSendMessage=[[UIButton alloc]initWithFrame:CGRectMake(120, 120, 120, 36)];  
  6.     [btnSendMessage setTitle:@"testMessage" forState:UIControlStateNormal];  
  7.     [btnSendMessage setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];  
  8.     [self.view addSubview:btnSendMessage];  
  9.     [btnSendMessage addTarget:self action:@selector(testMessagesAct) forControlEvents:UIControlEventTouchDown];  
  10.       
  11.       
  12.       
  13. }  

 

實現點擊方法

 

view plaincopy

 

/**

 *  分享到微信好友

 */

-(void)shareWX

{

    //標題 內容 圖片 下載鏈接

    WXMediaMessage *message=[WXMediaMessage message];

    message.title=@"優益齒";

    message.description=@"優益齒-您身邊的口腔護理專家\n歡迎下載優益齒";

    [message setThumbImage:[UIImage imageNamed:@"logo80"]];

    

    WXWebpageObject *webPageObject=[WXWebpageObject object];

    webPageObject.webpageUrl=@"http://www.youyichi.com/wx/appDownload";

    message.mediaObject=webPageObject;

    

    SendMessageToWXReq *req=[[SendMessageToWXReq alloc]init];

    req.bText=NO;

    req.message=message;

    req.scene = WXSceneSession;

    

    [WXApi sendReq:req];

    

    

 

}

 

/**

 *  分享到朋友圈

 */

-(void)shareFriend

{

     //標題 內容 圖片 下載鏈接

    WXMediaMessage *message=[WXMediaMessage message];

    message.title=@"優益齒";

    message.description=@"優益齒-您身邊的口腔護理專家\n歡迎下載優益齒";

    [message setThumbImage:[UIImage imageNamed:@"logo80"]];

    

    WXWebpageObject *webPageObject=[WXWebpageObject object];

    webPageObject.webpageUrl=@"http://www.youyichi.com/wx/appDownload";

    message.mediaObject=webPageObject;

    

    SendMessageToWXReq *req=[[SendMessageToWXReq alloc]init];

    req.bText=NO;

    req.message=message;

    req.scene = WXSceneTimeline;

    

    [WXApi sendReq:req];

 

    

}

 

 

注意必須要在真機上才能分享

SendMessageToWXReq 這個類只能分享文字,大家需要別的可以找相應的類

 

其中req.scene這個是指分享到什么去  

 WXSceneSession  = 0,        /**< 聊天界面    */

    WXSceneTimeline = 1,        /**< 朋友圈      */

    WXSceneFavorite = 2,        /**< 收藏       */

 

大家根據需要選擇要分享的地方

到這里完畢  去真機試一下吧

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM