iOS微信之簡單文本分享(集成官方SDK)

  • 2019 年 11 月 7 日
  • 筆記

前言

開發工具:XCode7.3.1 SDK版本:V1.7.1

一.APP提交審核

前期準備工作:可以參考這篇博文http://www.jianshu.com/p/839dc30f2250 iOS版本只需要提供Bundle Id即可

500CD4E3-4EE2-449F-8BF6-32D3102D1605.png

註:應用下載地址非必填

審核通過後就可以獲取AppID,AppSecret

appid.png

二.環境搭建

1.下載最新的SDK

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

iOS資源下載.png

2.導入SDK

導入SDK.png

3.項目配置

a.設置支援HTTP請求

支援HTTP請求.png

參考博文:http://www.jianshu.com/p/5935dff47e4f

b.設置sheme白名單

設置Scheme白名單.png

參考博文:http://www.jianshu.com/p/f974f4cbba18

c.關閉bitcode

禁用bitcode1.png

禁用bitcode2.png

d.設置URL

設置URL.png

e.設置Build phases

設置Build phases.png

三.程式碼整合

1.註冊ID,處理回調
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    // 從微信開放平台獲取  NSString *APPID = @"從微信開放平台獲取AppID";  // 向微信註冊  [WXApi registerApp:APPID];    return YES;  }    -(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 WXApiDelegate 微信分享的相關回調    // onReq是微信終端向第三方程式發起請求,要求第三方程式響應。第三方程式響應完後必須調用sendRsp返回。在調用sendRsp返回時,會切回到微信終端程式介面  - (void)onReq:(BaseReq *)req  {  NSLog(@"onReq是微信終端向第三方程式發起請求,要求第三方程式響應。第三方程式響應完後必須調用sendRsp返回。在調用sendRsp返回時,會切回到微信終端程式介面");  }    // 如果第三方程式向微信發送了sendReq的請求,那麼onResp會被回調。sendReq請求調用後,會切到微信終端程式介面  - (void)onResp:(BaseResp *)resp  {  NSLog(@"回調處理");    // 處理 分享請求 回調  if ([resp isKindOfClass:[SendMessageToWXResp class]]) {      switch (resp.errCode) {          case WXSuccess:          {              UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"                                                              message:@"分享成功!"                                                             delegate:self                                                    cancelButtonTitle:@"OK"                                                    otherButtonTitles:nil, nil];              [alert show];          }              break;            default:          {              UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"                                                              message:@"分享失敗!"                                                             delegate:self                                                    cancelButtonTitle:@"OK"                                                    otherButtonTitles:nil, nil];              [alert show];          }              break;      }    }  }
2.分享核心程式碼
// 簡單文本分享  -(IBAction)clickShareButton:(id)sender{  SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];  req.text                = @"簡單文本分享測試";  req.bText               = YES;  // 目標場景  // 發送到聊天介面  WXSceneSession  // 發送到朋友圈    WXSceneTimeline  // 發送到微信收藏  WXSceneFavorite  req.scene               = WXSceneSession;  [WXApi sendReq:req];  }

四.常見問題匯總(遇到的那些坑…)

1.官方Demo編譯報錯

下載地址: https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419319164&token=fdf6c5d38e9c82f85ebf72374441beff257c4849&lang=zh_CN 解決辦法:

解決辦法.png

官方Demo:(直接運行官方的demo會報各種錯誤。。。,提供一個無錯版本的)

https://github.com/andli0626/wx_demo_for_iOS-V1.7.1.git

五.參考資料

官方文檔

https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=1417694084&token=fdf6c5d38e9c82f85ebf72374441beff257c4849&lang=zh_CN

官方API更新說明:
# SDK1.7.1  1.支援兼容ipv6(提升穩定性)  2. xCode Version 7.3.1 (7D1014) 編譯  # SDK1.7  1.支援兼容ipv6  2.修復若干問題增強穩定性  # SDK1.6.3  1.xCode7.2 構建的sdk包。  2.請使用xCode7.2進行編譯。  3. 需要在Build  Phases中Link  Security.framework  4.修復若干小問題。  # SDK1.6.2  1、xCode7.1構建的sdk包  2、請使用xCode7.1進行編譯  # SDK1.6.1  1、修復armv7s下,bitcode可能編譯不過  2、解決warning  # SDK1.6  1、iOS 9系統策略更新,限制了http協議的訪問,此外應用需要在「Info.plist」中將要使用的URL  Schemes列為白名單,才可正常檢查其他應用是否安裝。  受此影響,當你的應用在iOS9中需要使用微信SDK的相關能力(分享、收藏、支付、登錄等)時,需要在「Info.plist」里增加如下程式碼:  <key>LSApplicationQueriesSchemes</key>  <array>  <string>weixin</string>  </array>  <key>NSAppTransportSecurity</key>  <dict>  <key>NSAllowsArbitraryLoads</key>  <true/>  </dict>  2、開發者需要在工程中鏈接上CoreTelephony.framework  3、解決bitcode編譯不過問題  # SDK1.5  1、廢棄safeSendReq:介面,使用sendReq:即可。  2、新增+(BOOL)  sendAuthReq:(SendAuthReq*) req viewController : (UIViewController*)  viewController delegate:(id<WXApiDelegate>) delegate;  支援未安裝微信情況下Auth,具體見WXApi.h介面描述  3、微信開放平台新增了微信模組用戶統計功能,便於開發者統計微信功能模組的用戶使用和活躍情況。開發者需要在工程中鏈接上:SystemConfiguration.framework,libz.dylib,libsqlite3.0.dylib。

實現效果圖

未命名圖片.png

源碼: https://github.com/andli0626/wx_sharedemo_for_iOS-V1.7.1.git

萬事開頭難,完成一個簡單文本分享,後面的其他API的使用就可以參考官方文檔,一步步實踐了。