iOS 不用微信 SDK 喚起微信支付
要想知道微信SDK是如何調起微信客戶端,那么咱們先看看微信SDK到底做了什么
SDK集成流程
- 將解壓的微信OpenSDK文件夾拷貝到項目文件夾下,并導入開發環境中。 libWeChatSDK.a
WechatAuthSDK.h
WXApi.h
WXApiObject.h
導入系統依賴庫
在link Binary With Libraries 里面添加
libc++.tbd
CoreTelephony.framework
libsqlite3.0.tbd
libz.tbd
SystemConfiguration.framework
還有之前導入的 libWeChatSDK.a
到這一步,SDK集成已經完畢
配置代碼
-
添加URL Schemes
點擊項目名稱,點擊“Info”選項卡,在“URL Types”選項中,點擊“+”, 在“URL Schemes”中輸入微信的注冊碼
c1000
- 在支付的類里引用頭文件:
#import "WXApi.h"
- 配置wxpay支付
#pragma mark ==============配置wxpay支付==============
- (void)payAction{
//發起網絡請求,去你們服務器請求1.訂單id,2. 錢(單位:分),3. 內容描述
//訂單最好服務器生成,本文為了各位看的明白,所以在本地生成!
[NetWorkTooldingiD:[self generateTradeNO]andDetail:@"描述"success:^(NSDictionary responseObject) {
//服務器返回數據
//調起微信支付
PayReq wxreq = [[PayReq alloc]init];
/ appid */
wxreq.openID = responseObject[@"result"][@"appid"];
/ 商家向財付通申請的商家id /
wxreq.partnerId = responseObject[@"result"][@"partnerid"];
/** 預支付訂單 /
wxreq.prepayId = responseObject[@"result"][@"prepayid"];
/ 隨機串,防重發 */
wxreq.nonceStr = responseObject[@"result"][@"noncestr"];
/ 時間戳,防重發 /
wxreq.timeStamp = [responseObject[@"result"][@"timestamp"]intValue];
/** 商家根據財付通文檔填寫的數據和簽名 /
wxreq.package = responseObject[@"result"][@"package"];
/* 商家根據微信開放平臺文檔對數據做的簽名 /
wxreq.sign = responseObject[@"result"][@"sign"];
[WXApisendReq:wxreq];
}failure:^(NSError *error) {
NSLog(@"%@",error);
}];
}
#pragma mark ==============產生隨機訂單號==============
- (NSString )generateTradeNO
{
static int kNumber = 15;
NSString sourceStr = @"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
NSMutableString resultStr = [[NSMutableString alloc]init];
srand((unsigned)time(0));
for (int i = 0; i < kNumber; i++)
{
unsigned index = rand() % [sourceStrlength];
NSString oneStr = [sourceStrsubstringWithRange:NSMakeRange(index, 1)];
[resultStrappendString:oneStr];
}
return resultStr;
}
</code></pre>
- 配置返回處理代碼
在 AppDelegate.h 文件中,增加微信協議:
#import "WXApi.h"
@interface AppDelegate : UIResponder <UIApplicationDelegate,WXApiDelegate>
在 AppDelegate.m 文件中,增加引用代碼:
- (BOOL)application:(UIApplication )applicationdidFinishLaunchingWithOptions:(NSDictionary )launchOptions {
// 初始化微信sdk
[WXApiregisterApp:@"wx*********"];
return YES;
}
- (BOOL)application:(UIApplication )applicationopenURL:(NSURL )urlsourceApplication:(NSString )sourceApplicationannotation:(id)annotation {
/! @brief 處理微信通過URL啟動App時傳遞的數據
需要在 application:openURL:sourceApplication:annotation:或者application:handleOpenURL中調用。
@param url 微信啟動第三方應用時傳遞過來的URL
@param delegate WXApiDelegate對象,用來接收微信觸發的消息。
@return 成功返回YES,失敗返回NO。
/
return [WXApihandleOpenURL:urldelegate:self];
}
- (void)onResp:(BaseResp )resp
{
//支付返回結果,實際支付結果需要去微信服務器端查詢
NSString strMsg = [NSStringstringWithFormat:@"支付結果"];
switch (resp.errCode) {
caseWXSuccess:
strMsg = @"支付結果:成功!";
NSLog(@"支付成功-PaySuccess,retcode = %d", resp.errCode);
break;
default:
strMsg = [NSStringstringWithFormat:@"支付結果:失敗!retcode = %d, retstr = %@", resp.errCode,resp.errStr];
NSLog(@"錯誤,retcode = %d, retstr = %@", resp.errCode,resp.errStr);
break;
}
}
</code></pre>
不用微信SDK 喚起微信支付
- 眾所周知,iOS是一個封閉的系統,應用之間是不可以互相讀取文件的,蘋果就使用了URL Scheme來實現了這個功能。通過各個APP設計的符合蘋果的統一規范的URL Scheme,Url Scheme 是可以用來傳遞信息的
URL Scheme是為方便app之間互相調用而設計的。你可以通過一個類似URL的鏈接,通過系統的OpenURl來打開該app,并可以傳遞一些參數。每個URL必須能唯一標識一個APP,如果你設置的URL與別的APP的URL沖突,此時,你的APP不一定會被調用起來,原因是當APP在安裝的時候就已經在系統里面注冊了此APP的URL Scheme,如果你的一致但是是后安裝的,那么系統不會調用你的APP,因為你的APP設置的URL scheme被覆蓋了。
- 分析得出,微信,支付寶等sdk 的分享,支付功能,都是通過URL scheme 進行傳遞內容的
- 那么我們可以查看微信SDK喚起微信客服端支付的時候,傳遞的URL Scheme 是什么內容,如果可以找到其編碼規律,那么即可以不用sdk進行支付
那么,發起支付的過程中,系統會喚起微信客戶端,我們思路是查看微信sdk發送給微信客戶端的URL Scheme內容,就要偽造一個微信,也就是向系統聲明一個和微信URL Scheme相同的地址
查看微信URL Scheme地址
經測試: 微信的URL Scheme是:weixin://
那么,我們新建個工程,起名為:GetPayURLScheme
接著注冊自定義 URL Scheme
點擊 項目里info.plist (非test里面的info.plist)并選擇 右鍵 Open As – Source Code:加入:
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>weixin</string>
</array>
<key>CFBundleURLName</key>
<string>1111</string>
</dict>
</array>
這時候,點擊 項目里info.plist (非test里面的info.plist)并選擇 右鍵 Open As – list:
這樣就生成了如下圖的URLscheme:

c1000
在 AppDelegate.m 里面添加
//應用app接收urlScheme傳值時會響應此方法
- (BOOL)application:(UIApplication )applicationopenURL:(NSURL )url
sourceApplication:(NSString )sourceApplicationannotation:(id)annotation{
//顯示截取的urlscheme
UIAlertView alert = [[UIAlertView alloc]initWithTitle:@"接收到的urlScheme"message:url.absoluteStringdelegate:nilcancelButtonTitle:nilotherButtonTitles:@"確定", nil];
[alertshow];
復制到剪貼板
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
pasteboard.string = url.absoluteString;
return YES;
}
</code></pre>
運行到手機里面,這時候,你無論那個app要微信付款的時候,喚起的是剛才新建的名叫GetPayURLScheme工程 ,如下圖:


可以看到:那個urlScheme:
weixin://app/wxdf261c3b90ffbc25/pay/?nonceStr=Ho7nAFOALQpVqSM7&package=Sign%3DWXPay&partnerId=1236537302&prepayId=wx201606052201506009de63980169148758&timeStamp=1465135310&sign=5A3EF234382FD61D36CEC104723387ED&signType=SHA1
可以看出:它的拼接方法是:
NSString *str = [NSStringstringWithFormat:@"weixin://app/%@/pay/?nonceStr=%@&package=Sign%%3DWXPay&partnerId=%@&prepayId=%@&timeStamp=%@&sign=%@&signType=SHA1",appid,noncestr,partnerid,prepayid,[NSStringstringWithFormat:@"%d",[timestampintValue]],sign];
不用SDK,只需要配置這么一段代碼,微信支付即可完成!!!!!!!!
- (void)payAction{
//發起網絡請求,去你們服務器請求1.訂單id,2. 錢(單位:分),3. 內容描述
//訂單最好服務器生成,本文為了各位看的明白,所以在本地生成!
[NetWorkTooldingiD:[self generateTradeNO]andDetail:@"描述"success:^(NSDictionary *responseObject) {
/** appid */
NSString *appid = responseObject[@"result"][@"appid"];
/** 商家向財付通申請的商家id */
NSString *partnerId = responseObject[@"result"][@"partnerid"];
/** 預支付訂單 */
NSString *prepayId = responseObject[@"result"][@"prepayid"];
/** 隨機串,防重發 */
NSString *nonceStr = responseObject[@"result"][@"noncestr"];
/** 時間戳,防重發 */
NSString *timeStamp = responseObject[@"result"][@"timestamp"];
/** 商家根據財付通文檔填寫的數據和簽名 */
NSString *package = responseObject[@"result"][@"package"];
/** 商家根據微信開放平臺文檔對數據做的簽名 */
NSString *sign = responseObject[@"result"][@"sign"];
//生成URLscheme
NSString *str = [NSStringstringWithFormat:@"weixin://app/%@/pay/?nonceStr=%@&package=Sign%%3DWXPay&partnerId=%@&prepayId=%@&timeStamp=%@&sign=%@&signType=SHA1",appid,nonceStr,partnerId,prepayId,[NSStringstringWithFormat:@"%d",[timeStampintValue] ],sign];
//通過openURL的方法喚起支付界面
[[UIApplication sharedApplication]openURL:[NSURLURLWithString:str]];
}failure:^(NSError *error) {
NSLog(@"%@",error);
}];
}

來自:http://ios.jobbole.com/85793/