iOS 微信分享到朋友圈
最近要加微信朋友圈分享的功能,上官網下檔案,照著文件搭環境,但是總有錯誤,於是百度部落格來看,發現和官方文件一樣,解決不了自己的問題,現在問題解決了,分享出來希望對大家有幫助。
1.首先要向微信註冊你的應用程式ID
https://open.weixin.qq.com/cgi-bin/frame?t=home/app_tmpl&lang=zh_CN&token=9d9a7a5e0d6fe8c9e4713c3352b88b60db8b83aa
2.下載資原始檔
https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&lang=zh_CN&token=9d9a7a5e0d6fe8c9e4713c3352b88b60db8b83aa
3.搭建開發環境
將SDK檔案中包含的 libWeChatSDK.a,WXApi.h,WXApiObject.h 三個檔案新增到你所建的工程中
新增庫:SystemConfiguration.framework,libz.dylib,libsqlite3.0.dylib,新增庫後還是報錯,後來知道還要新增libc++.dylib庫。
4.在Xcode中,選擇你的工程設定項,選中“TARGETS”一欄,在“info”標籤欄的“URL type“新增“URL scheme”為你所註冊的應用程式id
5。要使你的程式啟動後微信終端能響應你的程式,必須在程式碼中向微信終端註冊你的id。(如下圖所示,在 在AppDelegate中import WXApi.h 標頭檔案並在didFinishLaunchingWithOptions 函式中向微信註冊id),
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[WXApi registerApp:@"wx7562168c9ad2223c"];
return YES;
}
好了,AppDelegate中的工作完成了。接下來就去ViewController中完成分享操作,這裡我直接拖得一個分享按鈕操作-(BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{ return [WXApi handleOpenURL:url delegate:[ShareOperation shareOperation]]; } //--- - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation { //這裡如果還有其他第三方應用分享,可以這樣寫 這裡我把代理設為ViewController。 ViewController *viewc = [[ViewController alloc] init]; return [WeiboSDK handleOpenURL:url delegate:viewc] || [WXApi handleOpenURL:url delegate:viewc]; }
- (IBAction)Share:(id)sender {
if ([WXApi isWXAppInstalled] && [WXApi isWXAppSupportApi]) {
SendMessageToWXReq* req = [[SendMessageToWXReq alloc] init];
req.bText = YES;
req.text = @"浮沉浪似人潮";
req.scene = WXSceneTimeline;
[WXApi sendReq:req];
} else{
UIAlertView *alView = [[UIAlertView alloc]initWithTitle:@"" message:@"你還沒有安裝微信,無法使用此功能" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"免費下載微信", nil];
[alView show];
}
}
如果沒有安裝微信,呼叫微信的getWXAppInstallUrl方法獲取微信下載地址跳轉到商店下載
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 1) {
NSString *weiXinLink = [WXApi getWXAppInstallUrl];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:weiXinLink]];
}
}
現在直接執行demo點選分享按鈕進行朋友圈分享。