使用步驟
官方開發(fā)平臺注冊應用侠草,一般3天左右通過
pod安裝SDK
pod WechatOpenSDK
-
在Xcode中健蕊,選擇你的工程設置項沦补,選中“TARGETS”一欄,在“info”標簽欄的“URL type“添加“URL scheme”為你所注冊的應用程序id
URL type添加在微信開發(fā)平臺注冊的AppKey 實現相關方法和遵守協議
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
添加 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<UIApplicationOpenURLOptionsKey,id> *)options {
return [WXApi handleOpenURL:url delegate:self];
}
-(void)onReq:(BaseReq *)req
{
}
// 從微信分享過后點擊返回應用的時候調用
- (void)onResp:(BaseResp *)resp
{
}
這是我簡單封裝了一下微信分享的SDK SFWeChatShareManager使用簡單方便
分享網頁鏈接的使用
/**
分享網頁
@param title 標題
@param description 描述
@param thumbImage 縮略圖
@param webpageUrl 鏈接
@param type 分享類型 0:聊天界面 1:朋友圈 2:收藏
*/
+ (void)shareToWechatWithWebTitle:(NSString *)title
description:(NSString *)description
thumbImage:(UIImage *)thumbImage
webpageUrl:(NSString *)webpageUrl
type:(NSUInteger)type
{
WXMediaMessage *message = [WXMediaMessage message];
message.title = title;
message.description = description;
[message setThumbImage:thumbImage];
WXWebpageObject *webpageObject = [WXWebpageObject object];
webpageObject.webpageUrl = webpageUrl;
message.mediaObject = webpageObject;
[self sendToWechatWithBText:NO message:message scene:type];
}
/**
* 發(fā)送請求給微信
* bText: 發(fā)送的消息類型
* message: 多媒體消息結構體
* scene: 分享的類型場景
**/
+ (void)sendToWechatWithBText:(BOOL)bText message:(WXMediaMessage *)message scene:(NSUInteger)scene
{
SendMessageToWXReq *req = [[SendMessageToWXReq alloc]init];
req.bText = bText;
req.message = message;
req.scene = (int)scene;
[WXApi sendReq:req];
}
文件中還封裝了分享圖片他爸、視頻聂宾、文字、音樂讲逛,就不一一介紹了亏吝×氩海可下載下來直接拖入工程中使用
demo鏈接:https://github.com/gzc453645620/CodeData/tree/master/WeChatShare
注意:
- 縮略圖大小不能超過32K
- 我們的項目中有兩個部門的業(yè)務都用到了微信盏混,但是appid不一樣所以每次調用微信功能的時候都registerApp一次,因為如果在appDelegate注冊兩次的話惜论,后者會把前者覆蓋许赃。
- 在注冊registerApp之后再調用isWXAppInstalled否則會一直返回NO
isWXAppInstalled 返回NO
還有一種情況也會返回NO
需要在在info.plist中添加LSApplicationQueriesSchemes
WXApi isWXAppInstalled