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

 

0.先在微信開放平臺註冊建立應用地址https://open.weixin.qq.comhtml

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

審覈完成以後獲取微信的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_CNapp

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

2.下載完成之後打開,須要裏面的4個文件工具

libWeChatSDK.a  、WechatAuthSDK.h  、 WXApi.h  、WXApiObject.h開發工具

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

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

圖1url

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,        /**< 收藏       */

 

你們根據須要選擇要分享的地方

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

相關文章
相關標籤/搜索