第三方分享,今天聊的是友盟分享,官方链接:http://www.umeng.com/social
官方的SDK都能看懂,并且没有什么大的坑,这里不再累述。这里简单的举个例子。
首先注意添加第三方依赖库,不然一片红。
需要添加的依赖库:Security.framework,libiconv.dylib,SystemConfiguration.framework,CoreGraphics.framework,libsqlite3.dylib,CoreTelephony.framework,libstdc++.dylib,libz.dylib
直接上代码,这只是一个小例子。不对的地方还请指正。
//
// AppDelegate.m
// 第三方分享Demo
//
// Created by vicchou on 16/7/3.
// Copyright © 2016年 vicchou. All rights reserved.
//
#import "AppDelegate.h"
//友盟
#import "UMSocial.h"
//支持QQ
#import "UMSocialQQHandler.h"
//支持微信
#import "UMSocialWechatHandler.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//注册友盟APPKey
[UMSocialData setAppKey:@"575f5d09e0f55a09cd003209"];
//设置QQ的appkey和appId,这两个参数在腾讯开放平台申请获得,申请的时候个人开发者需要上传手持身份证正反面照片,公司开发者需要上传公司合法营业执照照片,提交之后一般在7个工作日之内审核出结果 ( 腾讯的不好申请 这可能就是大公司架子大 你爱用不用 )
//url在实际项目开发中后台会给你一个url,如果你的应用已经在app store上线成功的话,也可以填写当前应用在app store的下载地址,如果填为nil,默认的设置为友盟的官方网站
[UMSocialQQHandler setQQWithAppId:@"1104908293" appKey:@"MnGtpPN5AiB6MNvj" url:nil];
//设置微信的appkey和appSecret,这两个参数是在微信开放平台申请得到的,申请的时候需要上传应用所对应的appIcon,大小分别为28*28,108*108,必须为PNG格式,大小在300k以下,提交之后一般在3~4个工作日之内出结果 (同样很难申请)
[UMSocialWechatHandler setWXAppId:@"wx12b249bcbf753e87" appSecret:@"0a9cd00c48ee47a9b23119086bcd3b30" url:nil];
//需要隐藏设备上未安装的客户端,当进行分享的时候,首先友盟SDK会判断当前设备上时候安装有设定的第三方平台,如果没有,则隐藏对应的图标,如果不做这一步操作,审核不通过,主要还是针对财大气粗的QQ和微信
[UMSocialConfig hiddenNotInstallPlatforms:@[UMShareToQQ,UMShareToQzone,UMShareToWechatSession,UMShareToWechatTimeline]];
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
- (void)applicationWillTerminate:(UIApplication *)application {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
@end
//
// ViewController.m
// 第三方分享Demo
//
// Created by vicchou on 16/6/3.
// Copyright © 2016年 vicchou. All rights reserved.
//
#import "ViewController.h"
#import "UMSocial.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//直接用友盟的UI
UIButton * shareButton = [UIButton buttonWithType:UIButtonTypeCustom];
shareButton.frame = CGRectMake(100, 100, 100, 40);
[shareButton setTitle:@"分享" forState:UIControlStateNormal];
[shareButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[shareButton addTarget:self action:@selector(shareButtonClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:shareButton];
//自定义分享列表UI(1.可以基于UIWindow重写系统的UIActionSheet控件,2.直接基于UIView封装一个基础控件,可以添加到UIWindow上)
}
-(void)shareButtonClick
{
UIImage * image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"网络图片的地址"]]];
[UMSocialSnsService presentSnsIconSheetView:self appKey:@"575f5d09e0f55a09cd003209" shareText:@"需要分享的文字,公司里边一般会提供一个自己的url链接,url由后台给" shareImage:[UIImage imageNamed:@"需要分享的图片,有可能是本地图片,也可能是网络图片,如果是网络图片,需要将网络图片的地址转化为UIImage对象"] shareToSnsNames:@[UMShareToQzone,UMShareToQQ,UMShareToSina,UMShareToWechatSession,UMShareToWechatTimeline] delegate:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end