- 通过用户的分享,让更多的人去了解和使用这个APP
- 目前移动互联网应用程序推广的重要手段之一
- 属于口碑营销的范围
在iOS中,实现"社交分享"的方法
自己编写各个平台的分享代码(代码量较多)
利用iOS自带的Social.framework
利用第三方的分享框架(友盟+ ShareSDK 百度社会化分享组件)
iOS自带分享
//导入框架
Social.framework
//导入头文件
#import <Social/Social.h>
//1.判断分享平台是否可用
if(![SLComposeViewController isAvailableForServiceType:SLServiceTypeSinaWeibo]){
NSLog(@"请到系统设置中添加账号和密码");
}
//2.创建分享控制器
//ServiceType:分享的平台
SLComposeViewController *composeVC = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeSinaWeibo];
//设置预设的文字
[composeVC setInitialText:@"预设文字:"];
//预设的图片
[composeVC addImage:[UIImage imageNamed:@"图片"]];
//预设网址
[composeVC addURL:[NSURL URLWithString:@"http://www.baidu.com"]];
//3.弹出控制器
[self presentViewController:composeVC animated:YES completion:nil];
友盟分享
应用间跳转
if(![[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"change://"]]){
NSLog(@"不能打开对应的app");
}
跳转到指定页面
if(![[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"change://page1"]]){
NSLog(@"不能打开对应的app");
}
-(BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options{
NSLog(@"%@",url);
//转换成字符串
NSString *urlstr = url.absoluteString;
//获取//范围
NSRange range =[urlstr rangeOfString:@"//"];
//获取跳转的字符串
NSString *str = [urlstr substringFromIndex:range.location + range.length];
//根据截取的字符串,跳转不同的界面
//获取跟控制器
UINavigationController *nav = (UINavigationController *)self.window.rootViewController;
//跳转回跟控制器
[nav popToRootViewControllerAnimated:NO];
//获取控制器
ViewController *vc = nav.childViewControllers[0];
if(str = @"page1"){
//跳转控制器
[vc performSegueWithIdentifier:@"page1" sender:nil];
}
return YES;
}
回跳
//跳入的时候,传入自己的scheme
- (IBAction)page1:(id)sender {
NSString *header = @"change://page1";
//1.获取协议头
NSDictionary *info = [NSBundle mainBundle].infoDictionary;
NSLog(@"%@",info);
NSArray *urlTypes = info[@"CFBundleURLTypes"];
NSDictionary *typesInfo = urlTypes[0];
NSArray *schemes = typesInfo[@"CFBundleURLSchemes"];
NSString *scheme = schemes[0];
NSString *urlstr = [NSString stringWithFormat:@"%@?%@",header,scheme];
if(![[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlstr]]){
NSLog(@"不能打开对应的app");
}
}
//跳转到指定页面,并截取跳转过来页面的sheme数据
-(BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options{
NSLog(@"%@",url);
//转换成字符串
NSString *urlstr = url.absoluteString;
//获取//范围
NSRange range =[urlstr rangeOfString:@"//"];
//获取跳转的字符串
NSString *str = [urlstr substringFromIndex:range.location + range.length];
//根据截取的字符串,跳转不同的界面
self.str = str;
//获取跟控制器
UINavigationController *nav = (UINavigationController *)self.window.rootViewController;
//跳转回跟控制器
[nav popToRootViewControllerAnimated:NO];
//获取控制器
ViewController *vc = nav.childViewControllers[0];
if([str containsString:@"page1"]){
[vc performSegueWithIdentifier:@"page1" sender:nil];
}else if([str containsString:@"page2"]){
[vc performSegueWithIdentifier:@"page2" sender:nil];
}
//跳转控制器
return YES;
}
//从scheme中拿到回调页面的scheme,完成页面跳转
- (IBAction)back:(id)sender {
AppDelegate *d = (AppDelegate *)[UIApplication sharedApplication].delegate;
NSString *strurl = d.str;
NSRange range = [strurl rangeOfString:@"?"];
NSString *str = [strurl substringFromIndex:range.location + range.length];
if(![[UIApplication sharedApplication] openURL:[NSURL URLWithString:[str stringByAppendingString:@"://"]]]){
NSLog(@"不能打开对应的app");
}
}