1. 程式人生 > >IOS網路筆記--利用ShareSDK做分享頁面(新浪分享Demo)圖文教程

IOS網路筆記--利用ShareSDK做分享頁面(新浪分享Demo)圖文教程

#import "ViewController.h"

#import <ShareSDK/ShareSDK.h>

#import <ShareSDKUI/ShareSDK+SSUI.h>

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    //

    UIButton *button =  [UIButton buttonWithType:UIButtonTypeCustom];

    button.frame = CGRectMake(100, 100, 100, 40);

    button.backgroundColor = [UIColor blueColor];

    [self.view addSubview:button];

    [button addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];

}

#pragma mark - 分享按鈕

-(void)click

{

    //建立分享引數

    NSArray* imageArray = @[[UIImage imageNamed:@"shareImg.png"]];

    if (imageArray) {

        NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];

        [shareParams SSDKSetupShareParamsByText:@"分享內容"

                                         images:imageArray

                                            url:[NSURL URLWithString:@"http://mob.com"]

                                          title:@"分享標題"

                                           type:SSDKContentTypeAuto]; // 在這裡加分號;

        //2、分享(可以彈出我們的分享選單和編輯介面)

        [ShareSDK showShareActionSheet:nil //要顯示選單的檢視, iPad版中此引數作為彈出選單的參照檢視,只有傳這個才可以彈出我們的分享選單,可以傳分享的按鈕物件或者自己建立小的view 物件,iPhone可以傳nil不會影響

                                 items:nil

                           shareParams:shareParams

                   onShareStateChanged:^(SSDKResponseState state, SSDKPlatformType platformType, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error, BOOL end) {

                       switch (state) {

                           case SSDKResponseStateSuccess:

                           {

                               UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享成功"

                                                                                   message:nil

                                                                                  delegate:nil

                                                                         cancelButtonTitle:@"確定"

                                                                         otherButtonTitles:nil];

                               [alertView show];

                               break;

                           }

                           case SSDKResponseStateFail:

                           {

                               UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"分享失敗"

                                                                               message:[NSString stringWithFormat:@"%@",error]

                                                                              delegate:nil

                                                                     cancelButtonTitle:@"OK"

                                                                     otherButtonTitles:nil, nil];

                               [alert show];

                               break;

                           }

                           default:

                               break;

                       }

                   }];

    } // 在這裡加半個花括號

}