首先要在微信開放平臺申請 AppID(我第一天晚上申請的,次日中午就經過了),接着導入 SDK,也就是3個 .h 和一個 .a 文件,詳情見這裏html
若是你是 copy 在自建 group 裏面,web
1.須要在 Build Phases - Link Binary With Libraries 裏面 .a 文件微信
2.在 Copy Bundle Resources add .h 文件app
3.在 Bulid Settings - Library Search Paths "+" sdk 路徑ide
若是 copy 在自帶 group 裏面,則不須要,已經自動配置好ui
AppDelegate.h ,import "WXApi.h" 和 遵照協議atom
#import <UIKit/UIKit.h> #import "WXApi.h" @interface AppDelegate : UIResponder <UIApplicationDelegate,WXApiDelegate> @property (strong, nonatomic) UIWindow *window; @end
AppDelegate.murl
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { //申請的 AppID [WXApi registerApp:@"wxxxxx"]; return YES; } //重寫 handleOpenURL -(BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{ return [WXApi handleOpenURL:url delegate:self]; } //重寫 openURL -(BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options{ return [WXApi handleOpenURL:url delegate:self]; }
在你須要引用的類裏面spa
#import "ViewController.h" #import "WXApi.h" @interface ViewController () @property (nonatomic,strong)UIButton *button; @end @implementation ViewController static NSString *KLinkURL = @"http://www.iqiyi.com/a_19rrhbkfv1.html?vfm=2008_aldbd"; static NSString *KLinkTitle = @"史上最帥男人"; static NSString *KLinkDescription = @"媽呀,誰都別攔我,我要嫁給他!!!!!!!!"; - (void)viewDidLoad { [super viewDidLoad]; [self.view addSubview:self.button]; } -(void)Click{ //建立發送對象實例 SendMessageToWXReq *sendReq = [[SendMessageToWXReq alloc] init]; sendReq.bText = NO;//不使用文本信息 sendReq.scene = 0;//0 = 好友列表 1 = 朋友圈 2 = 收藏 //建立分享內容對象 WXMediaMessage *urlMessage = [WXMediaMessage message]; urlMessage.title = KLinkTitle;//分享標題 urlMessage.description = KLinkDescription;//分享描述 [urlMessage setThumbImage:[UIImage imageNamed:@"sb2.jpg"]];//分享圖片,使用SDK的setThumbImage方法可壓縮圖片大小,圖片大小不能超過 32 KB //建立多媒體對象 WXWebpageObject *webObj = [WXWebpageObject object]; webObj.webpageUrl = KLinkURL;//分享連接 //完成發送對象實例 urlMessage.mediaObject = webObj; sendReq.message = urlMessage; //發送分享信息 [WXApi sendReq:sendReq]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } -(UIButton *)button{ if (!_button) { _button = [[UIButton alloc]init]; _button.frame = CGRectMake(100, 100, 100, 100); _button.backgroundColor = [UIColor redColor]; [_button addTarget:self action:@selector(Click) forControlEvents:UIControlEventTouchUpInside]; } return _button; }