实现例子
网易新闻-》分享-》 微信 -》 回到网易新闻:
1. URL统一资源定位符:
http:///www.baidu.com
tel://110
file:///apple/Desktop/
组成: 协议头/资源路径
给微信配置url: 点击info->URL Types-> URL Schemes添加协议头weixing
2. 网易新闻通过 UIApplication打开微信
- (IBAction)weixingShare:(id)sender {
NSURL* weixing= [NSURL URLWithString:@"weixing://session?news"];
// 分享给好友
if([[UIApplication sharedApplication] canOpenURL:weixing]){
[[UIApplication sharedApplication] openURL:weixing options:@{} completionHandler:nil];
}
}
- (IBAction)wexingsharepengyouquan:(id)sender {
NSURL* weixing_pengyouquan= [NSURL URLWithString:@"weixing://pengyouquan?news"];
// 分享到朋友圈
if([[UIApplication sharedApplication] canOpenURL:weixing_pengyouquan]){
[[UIApplication sharedApplication] openURL:weixing_pengyouquan options:@{} completionHandler:nil];
}
}
格式: weixing://session?news
weixing: 协议头 session:可以表示跳入不同的页面,比如分享给朋友,分享朋友圈, news该应用的协议头
3. 注意: 苹果公司在IOS9以后做了更新,但判断其他应用是否安装或者调用第三方应用的时候,
都需要在info.plist中配置 白名单
在 网易中: LSApplicationQueriesSchemes添加wexing
跳转到了微信,返回的时候也要在微信的白名单中配置网易的白名单才可以返回网易
4. 网易可以跳转微信了:
在微信的application中截获参数,跳入不同页面
-(BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options{
// 可以通过导航空气器跳转指定控制器
UINavigationController* navcontroller=(UINavigationController*) self.window.rootViewController;
// 取出viewcontroller 可以通过
// 连线 然后 performSegueWidthINdentifier:@"seeesion"来跳转
UIViewController* vewcontroller=[navcontroller.childViewControllers firstObject];
[navcontroller popToRootViewControllerAnimated:YES];
// 微信可以根据不同参数跳转不同页面
NSString* urlStr= url.absoluteString;
if([urlStr rangeOfString:@"session"].length){
NSRange range= [urlStr rangeOfString:@"?"];
NSString* fanghuiUrl= [urlStr substringFromIndex:range.location+1];
NSLog(@"fanghuiUrl:%@",fanghuiUrl);
NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:fanghuiUrl forKey:@"fanghuiurl"];
[userDefaults synchronize];
NSLog(@"%@",NSHomeDirectory());
NSLog(@"%@",@"这里是分享控制器");
}else if([urlStr rangeOfString:@"pengyouquan"].length){
NSLog(@"%@",@"分享朋友圈");
[navcontroller performSegueWithIdentifier:@"login2contact" sender:nil];
}
return YES;
}
5. 在微信中返回网易新闻:
- (IBAction)backhome:(id)sender {
NSUserDefaults* userDefault= [NSUserDefaults standardUserDefaults];
_result= [userDefault objectForKey:@"fanghuiurl"];
NSString* result2= [_result stringByAppendingString:@"://"];
NSLog(@"返回的结果是result2:%@",result2);
NSURL* weixing= [NSURL URLWithString:result2];
if([[UIApplication sharedApplication] canOpenURL:weixing]){
[[UIApplication sharedApplication] openURL:weixing options:@{} completionHandler:nil];
}
}
但是也要配置白名单在微信中,要给网易配置自己的协议头