SocialLib 通过 CocoaPods 提供。为了安装,只需将以下行添加到您的 Podfile 中
安装所有平台(Facebook,Twitter,Tumblr,Instagram,Weibo 和 Weixin)
pod 'SocialLib'
对于特定社交平台,使用 subspec
pod 'SocialLib/Facebook'
pod 'SocialLib/Twitter'
pod 'SocialLib/Tumblr'
pod 'SocialLib/Instagram'
pod 'SocialLib/Weibo'
pod 'SocialLib/Weixin'
在您的 .pch 中添加以下行
//Facebook:
#import <SocialLib/SocialLib+Facebook.h>
//Twitter:
#import <SocialLib/SocialLib+Twitter.h>
//Tumblr:
#import <SocialLib/SocialLib+Tumblr.h>
//Weibo:
#import <SocialLib/SocialLib+Weibo.h>
//Weixin / WeChat:
#import <SocialLib/SocialLib+Weixin.h>
在您的 AppDelegate 中
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
return [SocialLib connectSocialPlatformWithApplication:application didFinishLaunchingWithOptions:launchOptions];
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
[SocialLib applicationDidBecomeActie:application];
}
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {
return [SocialLib handleOpenURL:application
openURL:url
sourceApplication:sourceApplication
annotation:annotation];
}
<dict>...</dict>
)。<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>fb{FACEBOOK_APP_ID}</string>
</array>
</dict>
</array>
<key>FacebookAppID</key>
<string>{FACEBOOK_APP_ID}</string>
<key>FacebookDisplayName</key>
<string>{Your App Name}</string>
对于 iOS 9
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>facebook.com</key>
<dict>
<key>NSIncludesSubdomains</key> <true/>
<key>NSExceptionRequiresForwardSecrecy</key> <false/>
</dict>
<key>fbcdn.net</key>
<dict>
<key>NSIncludesSubdomains</key> <true/>
<key>NSExceptionRequiresForwardSecrecy</key> <false/>
</dict>
<key>akamaihd.net</key>
<dict>
<key>NSIncludesSubdomains</key> <true/>
<key>NSExceptionRequiresForwardSecrecy</key> <false/>
</dict>
</dict>
</dict>
<key>LSApplicationQueriesSchemes</key>
<array>
<string>fbapi</string>
<string>fbapi20130214</string>
<string>fbapi20130410</string>
<string>fbapi20130702</string>
<string>fbapi20131010</string>
<string>fbapi20131219</string>
<string>fbapi20140410</string>
<string>fbapi20140116</string>
<string>fbapi20150313</string>
<string>fbapi20150629</string>
<string>fbauth</string>
<string>fbauth2</string>
<string>fb-messenger-api20140430</string>
</array>
<key>CFBundleURLTypes</key> <array>...</array>
)。将 {URL Scheme} 替换为您想要的
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>{URL Scheme}</string>
</array>
</dict>
替换
<key>TwitterCallbackURL</key>
<string>{URL Scheme}</string>
<key>TwitterConsumerKey</key>
<string>{Twitter Consumer Key}</string>
<key>TwitterConsumerSecret</key>
<string>{Twitter Consumer Secret}</string>
对于 Twitter 应用程序,您必须有一个有效的回调 URL,否则共享将不会工作。
<key>CFBundleURLTypes</key> <array>...</array>
)。将 {URL Scheme} 替换为您想要的,必须唯一
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>{URL Scheme}</string>
</array>
</dict>
在文件的主体中添加以下行
替换
<key>TumblrCallbackURL</key>
<string>{URL Scheme}</string>
<key>TumblrConsumerKey</key>
<string>{Tumblr Consumer Key}</string>
<key>TumblrConsumerSecret</key>
<string>{Tumblr Consumer Secret}</string>
<key>CFBundleURLTypes</key> <array>...</array>
)。将 {WEIBO API ID} 替换为您的 Weibo App ID
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLName</key>
<string>com.weibo</string>
<key>CFBundleURLSchemes</key>
<array>
<string>{WEIBO API ID}</string>
</array>
</dict>
在文件的主体中添加以下行
替换
<key>WeiboAppID</key>
<string>{WEIBO API ID}</string>
<key>CFBundleURLTypes</key> <array>...</array>
)。将 {WEIXIN API ID} 替换为您的 Weixin App ID
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>{WEIXIN API ID}</string>
</array>
</dict>
在文件的主体中添加以下行
替换
<key>WeixinAppID</key>
<string>ENTER{WEIXIN API ID}</string>
<key>WeixinAppName</key>
<string>{WEIXIN API NAME}</string>
要运行示例项目,克隆仓库,首先从 Example 目录运行 pod install
。 提供的示例 Facebook、Twitter 和 Tumblr API 密钥,Weibo 和 Weixin API 密钥为空
要使用 SocialLib 分享,您需要模态对象
InfoModal.h
#import <Foundation/Foundation.h>
@interface InfoModal : NSObject <SocialLibFacebookMessage, SocialLibTwitterMessage, SocialLibTumblrMessage, SocialLibWeiboMessage, SocialLibWeixinMessage>{
}
@property (nonatomic, strong) NSString *infoTitle;
@property (nonatomic, strong) NSString *infoContent;
@property (nonatomic, strong) NSString *infoContentURL;
@property (nonatomic, strong) NSArray *infoImages;
@property (nonatomic, strong) NSString *infoThumbnailImageURL;
@property (nonatomic, strong) NSString *infoVideoURL;
InfoModal.m
#import "InfoModal.h"
@implementation InfoModal
#pragma mark - SocialLibMessage
- (NSString *)title{
return _infoTitle;
}
- (NSString *)content{
return _infoContent;
}
- (NSString *)contentURL{
return _infoContentURL;
}
- (NSArray *)images{
return _infoImages;
}
- (NSString *)thumbnailImageURL{
return _infoThumbnailImageURL;
}
- (NSString *)videoURL{
return _infoVideoURL;
}
- (NSString *)tweetContent{
return [NSString stringWithFormat:@"%@ - %@ %@",_infoTitle, _infoContent, _infoContentURL];
}
- (SocialLibTwitterMessageType)twitterMessageType{
return SocialLibTwitterMessageTypeText;
}
- (SocialLibTumblrMessageType)tumblrMessageType{
return SocialLibTumblrMessageTypeLink;
}
- (SocialLibWeiboMessageType)weiboMessageType{
return SocialLibWeiboMessageTypeText;
}
- (SocialLibWeixinMessageType)weixinMessageType{
return SocialLibWeixinMessageTypeLink;
}
@end
要分享到 Facebook,请使用
InfoModal *info = [[InfoModal alloc] init];
info.infoTitle = @"SocialLib";
info.infoContent = @"Share via SocialLib";
info.infoContentURL = @"http://darkcl.github.io/SocialLib";
[SocialLib shareModal:info
toPlatform:kSocialLibPlatformFacebook
success:^(NSDictionary *message) {
NSLog(@"%@", message);
}
failure:^(NSDictionary *message, NSError *error) {
NSLog(@"%@", error);
}];
要分享到 Twitter,请使用
InfoModal *info = [[InfoModal alloc] init];
info.infoTitle = @"SocialLib";
info.infoContent = @"Share via SocialLib";
info.infoContentURL = @"http://darkcl.github.io/SocialLib";
[SocialLib shareModal:info
toPlatform:kSocialLibPlatformTwitter
success:^(NSDictionary *message) {
NSLog(@"%@", message);
}
failure:^(NSDictionary *message, NSError *error) {
NSLog(@"%@", error);
}];
SocialLib 可以在 MIT 许可证下获得。有关更多信息,请参阅 LICENSE 文件。