1. TARGETS - Info - URL Types
identifier -> weixin
URL Schemes -> 應用id
2.在AppDelegate.h 引入頭文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
<pre name=
"code"
>#
import
"WXApi.h"
{
/**
* WXSceneSession 分享到會話
* WXSceneTimeline 分享到朋友圈
* WXSceneFavorite 分享到我的收藏
*/
enum
WXScene _scene;
}
- (id)init{
if
(self = [
super
init]){
_scene = WXSceneSession;
}
return
self;
}
-(
void
) changeScene:(NSInteger)scene
{
_scene = scene;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// 其它代碼
// 向微信注冊應用ID
[WXApi registerApp:@
"xxooxoxoxoxoxoxo"
];
}
#pragma mark - 重寫AppDelegate的handleOpenURL和openURL方法
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
return
[WXApi handleOpenURL:url delegate:self];
}
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
return
[WXApi handleOpenURL:url delegate:self];
}
-(
void
) onReq:(BaseReq*)req
{
if
([req isKindOfClass:[GetMessageFromWXReq
class
]])
{
// 微信請求App提供內容, 需要app提供內容后使用sendRsp返回
NSString *strTitle = [NSString stringWithFormat:@
"微信請求App提供內容"
];
NSString *strMsg = @
"微信請求App提供內容,App要調用sendResp:GetMessageFromWXResp返回給微信"
;
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:strTitle message:strMsg delegate:self cancelButtonTitle:@
"OK"
otherButtonTitles:nil, nil];
alert.tag =
1000
;
[alert show];
[alert release];
}
else
if
([req isKindOfClass:[ShowMessageFromWXReq
class
]])
{
ShowMessageFromWXReq* temp = (ShowMessageFromWXReq*)req;
WXMediaMessage *msg = temp.message;
//顯示微信傳過來的內容
WXAppExtendObject *obj = msg.mediaObject;
NSString *strTitle = [NSString stringWithFormat:@
"微信請求App顯示內容"
];
NSString *strMsg = [NSString stringWithFormat:@
"標題:%@ \n內容:%@ \n附帶信息:%@ \n縮略圖:%u bytes\n\n"
, msg.title, msg.description, obj.extInfo, msg.thumbData.length];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:strTitle message:strMsg delegate:self cancelButtonTitle:@
"OK"
otherButtonTitles:nil, nil];
[alert show];
[alert release];
}
else
if
([req isKindOfClass:[LaunchFromWXReq
class
]])
{
//從微信啟動App
NSString *strTitle = [NSString stringWithFormat:@
"從微信啟動"
];
NSString *strMsg = @
"這是從微信啟動的消息"
;
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:strTitle message:strMsg delegate:self cancelButtonTitle:@
"OK"
otherButtonTitles:nil, nil];
[alert show];
[alert release];
}
}
-(
void
) onResp:(BaseResp*)resp
{
if
([resp isKindOfClass:[SendMessageToWXResp
class
]])
{
NSString *strTitle = [NSString stringWithFormat:@
"發送媒體消息結果"
];
NSString *strMsg = [NSString stringWithFormat:@
"errcode:%d"
, resp.errCode];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:strTitle message:strMsg delegate:self cancelButtonTitle:@
"OK"
otherButtonTitles:nil, nil];
[alert show];
[alert release];
}
}
4
.這是我寫好的在微信會話和朋友圈分享文字或者圖片的方法
直接調用就可以
#pragma mark - 微信, 朋友圈分享
#pragma mark 文字分享
- (
void
)sharedByWeChatWithText:(NSString *)WeChatMessage sceneType:(
int
)sceneType
{
SendMessageToWXReq *req = [[[SendMessageToWXReq alloc] init]autorelease];
req.text = WeChatMessage;
req.bText = YES;
req.scene = sceneType;
[WXApi sendReq:req];
}
#pragma mark 圖片分享
- (
void
)sharedByWeChatWithImage:(NSString *)imageName sceneType:(
int
)sceneType
{
WXMediaMessage *message = [WXMediaMessage message];
[message setThumbImage:[UIImage imageNamed:imageName]];
WXImageObject *ext = [WXImageObject object];
NSString *filePath = [[NSBundle mainBundle] pathForResource:imageName ofType:@
"png"
];
ext.imageData = [NSData dataWithContentsOfFile:filePath];
UIImage *image = [UIImage imageWithData:ext.imageData];
ext.imageData = UIImagePNGRepresentation(image);
message.mediaObject = ext;
SendMessageToWXReq *req = [[[SendMessageToWXReq alloc] init]autorelease];
req.bText = NO;
req.message = message;
req.scene = sceneType;
[WXApi sendReq:req];
}
|