版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ldszw/article/details/79518062
应用程序的跳转
默认情况下应用程序是没有URL,并且在iOS中打开一个应用程序只要能拿到这个应用程序的协议头即可
URL : 统一资源定位符(可以没有路径,但是必须有协议头 http:// ) 由以下两部分组成
scheme : 协议头,用来决定查找资源的方式 http:// ftp://
path: 路径
该协议头是目标APP决定,添加方式如下:
跳转代码:
// 获取应用程序的URL
NSURL *url = [NSURL URLWithString:URLstring];
// 跳转
[[UIApplication sharedApplication] openURL:url options:@{} completionHandler:^(BOOL success) {
}];
这种方式,只是打开该APP,但是有的需求可能会需要跳转到APP的具体界面,比如朋友圈和微信好友两个界面,具体代码实现如下:
- (IBAction)skipToWechat
{
// 跳转微信
[self openURLWithString:@"wechat://"];
}
- (IBAction)skipToMoments
{
// 跳转朋友圈
[self openURLWithString:@"wechat://moments"];
}
- (IBAction)skipToFriend
{
// 跳转微信好友
[self openURLWithString:@"wechat://friend"];
}
- (void)openURLWithString:(NSString *)URLstring
{
// 获取应用程序的URL
NSURL *url = [NSURL URLWithString:URLstring];
// 跳转
[[UIApplication sharedApplication] openURL:url options:@{} completionHandler:^(BOOL success) {
}];
}
这个也是需要目标APP决定,创建对应的字符串参数(朋友圈和微信好友),然后来源APP根据目标APP的协议头和路径(也就是某个字符串参数)去跳转到目标APP的某个具体界面
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options
{
NSString *urlString = [url absoluteString];
if ([urlString containsString:@"moments"]) {
NSLog(@"朋友圈");
}else if ([urlString containsString:@"friend"]) {
NSLog(@"微信好友");
}
return YES;
}
目标APP内部也是需要在AppDelegate中根据来源APP传进来的路径做出判断是展示哪一个分享界面
还有一种需求是,跳转到目标APP后,需要又回到来源APP,这个时候需要在目标APP中获取到来源APP的协议头以便跳转回去,所以在来源App跳转的时候,会根绝目标APP的某种规则,然后设计URL的路径,比如:
[self openURLWithString:@"wechat://friend?taobao"];
在路径后加一个?号,然后后面跟上自己APP的协议头,这样目标APP就可以根据传进来的url截取到来源APP的协议头,实现返回。