社交分享功能

如何實現社交分享:

在iOS中實現社交分享的方法有三種:

->自己編寫各個平臺的分享代碼(代碼量較多)

->利用ios自帶的Social.framework

->利用第三方的分享框架

百度社會化分享組件:http://developer.baidu.com/soc/share

百度還有個“社會化登錄組件”:http://developer.baidu.com/soc/login


Social.framework

Social.framework支持的分享平臺(打開手機上的“設置”即可看到
       
使用Social.framework之前得在“設置”中添加相應分享平臺的帳號
示例:
#import "ViewController.h"
#import <Social/Social.h>

@interface ViewController ()

@end

@implementation ViewController

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    // 1.判斷平臺是否可用
    if (![SLComposeViewController isAvailableForServiceType:SLServiceTypeSinaWeibo]) {
        NSLog(@"查看您是否設置了新浪微博帳號,設置界面-->新浪微博-->配置帳號");
    }
    
    // 2.創建SLComposeViewController
    SLComposeViewController *composeVc = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeSinaWeibo];
    
    // 2.1.添加分享文字
    [composeVc setInitialText:@"做人如果沒有夢想,跟鹹魚有什麼區別"];
    
    // 2.2.添加分享圖片
    [composeVc addImage:[UIImage imageNamed:@"xingxing"]];
    
    // 3.彈出分享界面
    [self presentViewController:composeVc animated:YES completion:nil];
    
    
    // 4.設置block屬性(監聽用戶是點擊了取消分享還是分享按鈕)
    composeVc.completionHandler = ^ (SLComposeViewControllerResult result) {
        if (result == SLComposeViewControllerResultCancelled) {
            NSLog(@"用戶點擊了取消");
        } else {
            NSLog(@"用戶點擊了發送");
        }
    };
}

@end

友盟分享
官網:http://bbs.umeng.com/forum-social-1.html

oauth2.0授權

彈出個網頁讓用戶輸入賬號密碼授權

sso授權:
直接跳轉到手機上的新浪微博應用中,如果以前登陸過的話直接授權,然後跳回去

SSO 目前在國內使用比較多,如果本機安裝了某個應用程序,會直接進入該應用程序獲得授權。

URL Schemes"sina."+你的友盟AppKey.



下面以分享到新浪微博平臺爲例:
從網上下載友盟的SDK,然後導包


AppDelegate:

</pre></p><pre name="code" class="objc"><span style="font-size:18px;"></span>
<span style="font-size:18px;">#import "AppDelegate.h"
#import "UMSocial.h"
#import "UMSocialSinaHandler.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
     // appKey
    [UMSocialData setAppKey:@"54ced694fd98c588d2000210"];
    
    // sso授權地址
    [UMSocialSinaHandler openSSOWithRedirectURL:@"http://sns.whalecloud.com/sina2/callback"];
    
    return YES;
}

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
    NSLog(@"%@", url);
    return  [UMSocialSnsService handleOpenURL:url];
}

- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation
{
    return  [UMSocialSnsService handleOpenURL:url];
}

@end
</span>

ViewController:

<span style="font-size:18px;">#import "ViewController.h"
#import "UMSocial.h"

@interface ViewController ()

- (IBAction)shareClick;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)shareClick {
    NSString *shareText = @"出任CEO,贏取白富美,走向人生巔峯";
    UIImage *shareImage = [UIImage imageNamed:@"xingxing"];
    
//    [UMSocialSnsService presentSnsIconSheetView:self
//                                         appKey:@"54ced694fd98c588d2000210"
//                                      shareText:shareText
//                                     shareImage:shareImage
//                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToTencent,UMShareToRenren,UMShareToEmail,nil]
//                                       delegate:nil]; // 分享平臺
    
    [UMSocialSnsService presentSnsController:self appKey:@"54ced694fd98c588d2000210" shareText:shareText shareImage:shareImage shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToTencent,UMShareToRenren,UMShareToEmail,nil] delegate:nil];
}
@end
</span>







發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章