总结一下shareSDK的登录和分享,以新浪微博和微信为例,其他的自己参考文档修改参数即可。
步骤如下:
1.首先要注册开发者账号。shareSDK和新浪微博的是免费的;微信分享是免费的,登录权限需要300元/年。新浪微博分享网络图片时,需要申请高级接口--微博高级写入接口。
2.从shareSDK官网下载最新的shareSDKdemo,需要的文件是ShareSDK。
3.建一个项目,导入ShareSDK文件,再导入必要的库文件UIKit.framework、CoreGraphics.framework、Foundation.framework、SystemConfiguration.framework、QuartzCore.framework、CoreTelephony.framework、Security.framework、libicucore.dylib、libz.1.2.5.dylib、libstdc++.dylib、libsqlite3.dylib,运行不报错
4.在AppDelegate.m里面修改参数。导入#import<ShareSDK/ShareSDK.h>和#import"WXApi.h"
在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions里面加入
[ShareSDKregisterApp:@"35c545bbc368"];
//添加新浪微博应用注册网址http://open.weibo.com
[ShareSDKconnectSinaWeiboWithAppKey:@"3593224192"
appSecret:@"466fc13899efa3ab135bc6381036ccf4"
redirectUri:@"https://api.weibo.com/oauth2/default.html"];
//添加微信应用
[ShareSDKconnectWeChatWithAppId:@"wx7eb86cb678377508"
appSecret:@"aaa8d9438c7dd2378e796cd95590dd24"
wechatCls:[WXApi class]];
再加入2个方法
- (BOOL)application:(UIApplication *)application
handleOpenURL:(NSURL *)url
{
return [ShareSDKhandleOpenURL:url
wxDelegate:self];
}
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation
{
return [ShareSDKhandleOpenURL:url
sourceApplication:sourceApplication
annotation:annotation
wxDelegate:self];
}
5.然后在target->info->URL Types里面增加回调URL
6.在需要登录或分享的文件里导入#import <ShareSDK/ShareSDK.h>和#import "WXApi.h"
//微信登录
[ShareSDKgetUserInfoWithType:ShareTypeWeixiSessionauthOptions:nilresult:^(BOOL result,id<ISSPlatformUser> userInfo,id<ICMErrorInfo> error) {
NSLog(@"=====%@",userInfo.uid);
if (result) {
NSLog(@"登录成功");
//成功登录后,判断该用户的ID是否在自己的数据库中。
//如果有直接登录,没有就将该用户的ID和相关资料在数据库中创建新用户。
// [self reloadStateWithType:ShareTypeSinaWeibo];
}
}];
//微博登录
[ShareSDKgetUserInfoWithType:ShareTypeSinaWeiboauthOptions:nilresult:^(BOOL result,id<ISSPlatformUser> userInfo,id<ICMErrorInfo> error) {
NSLog(@"=====%@",userInfo.uid);
if (result) {
NSLog(@"登录成功");
//成功登录后,判断该用户的ID是否在自己的数据库中。
//如果有直接登录,没有就将该用户的ID和相关资料在数据库中创建新用户。
// [self reloadStateWithType:ShareTypeSinaWeibo];
}
}];
//分享图片
NSString *imagePath = [[NSBundlemainBundle]pathForResource:@"icon调整" ofType:@"png"];
//构造分享内容
id<ISSContent> publishContent = [ShareSDKcontent:@"分享内容"
defaultContent:@"默认分享内容,没内容时显示"
image:[ShareSDKimageWithPath:imagePath]
title:@"ShareSDK"
url:@"http://www.sharesdk.cn"
description:@"这是一条测试信息"
mediaType:SSPublishContentMediaTypeNews];
id<ISSAuthOptions> authOptions = [ShareSDKauthOptionsWithAutoAuth:YES
allowCallback:NO
authViewStyle:SSAuthViewStyleFullScreenPopup
viewDelegate:nil
authManagerViewDelegate:nil];
若采用九宫格显示则加入方法,里面集成了很多分享
[ShareSDKshowShareActionSheet:nil
shareList:nil
content:publishContent
statusBarTips:NO
authOptions:authOptions
shareOptions:nil
result:^(ShareType type,SSResponseState state, id<ISSPlatformShareInfo> statusInfo,id<ICMErrorInfo> error,BOOL end) {
if (state ==SSResponseStateSuccess)
{
NSLog(@"分享成功");
}
elseif (state ==SSResponseStateFail)
{
NSLog(NSLocalizedString(@"TEXT_SHARE_FAI",@"发布失败!error code == %d, error code == %@"), [errorerrorCode], [errorerrorDescription]);
}
}];
//ShareTypeWeixiSession 修改这个属性可以设置不同的分享
[ShareSDKshowShareViewWithType:ShareTypeSinaWeibo
container:nil
content:publishContent
statusBarTips:NO
authOptions:authOptions
shareOptions:nil
result:^(ShareType type,SSResponseState state, id<ISSPlatformShareInfo> statusInfo,id<ICMErrorInfo> error,BOOL end) {
if (state ==SSResponseStateSuccess)
{
NSLog(@"分享成功");
}
elseif (state ==SSResponseStateFail)
{
NSLog(NSLocalizedString(@"TEXT_SHARE_FAI",@"发布失败!error code == %d, error code == %@"), [errorerrorCode], [errorerrorDescription]);
}
}];
8.登录和分享功能就可以使用了,注意微信的登录和分享必须真机测试。新浪微博分享网络图片需要高级接口--微博高级写入接口。
demo下载地址:http://download.csdn.net/detail/baidu_21172753/8361223