App间相互跳转及图片分享
Posted 贝贝le
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了App间相互跳转及图片分享相关的知识,希望对你有一定的参考价值。
A-app:
Info--URL Types--URL Schemes:A-app(一个标识,允许别的app调用本App)
info.plist 添加白名单:
LSApplicationQueriesSchemes(Array)
B-app(String)
//使用
- (void)jumpToBapp {
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
pasteboard.image = [UIImage imageNamed:@"qq"];
// NSData *imageDataa = UIImageJPEGRepresentation([UIImage imageNamed:@"qq"], 1);
// [pasteboard setData:imageDataa forPasteboardType:@"shareImageLE"];
NSString *stra = [NSString stringWithFormat:@"B-app://Page2?A-app"];
NSURL *appUrla = [NSURL URLWithString:stra];
if ([[UIApplication sharedApplication] canOpenURL:appUrla]) {
[[UIApplication sharedApplication] openURL:appUrla];
} else {
NSLog(@"=====can not OpenURL");
}
}
B-app:
Info--URL Types--URL Schemes:B-app(一个标识,允许别的app调用本App)
info.plist 添加白名单:
LSApplicationQueriesSchemes(Array)
A-app(String)
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
// 1.获取导航栏控制器
UINavigationController *rootNav = (UINavigationController *)self.window.rootViewController;
// 2.获得主控制器
// ViewController *mainVc = [rootNav.childViewControllers firstObject];
// 3.每次跳转前必须是在跟控制器(细节)
// [rootNav popToRootViewControllerAnimated:NO];
// 4.根据字符串关键字来跳转到不同页面
if ([url.absoluteString containsString:@"Page1"]) {
[rootNav pushViewController:[ViewController new] animated:NO];
} else if ([url.absoluteString containsString:@"Page2"]) {
MJViewController *mvc = [MJViewController new];
mvc.urlString = url.absoluteString;
[rootNav pushViewController:mvc animated:NO];
// [mainVc performSegueWithIdentifier:@"homeToPage2" sender:nil];
}
return YES;
}
//MJViewController.m
if (self.urlString) {
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
self.imageV.image = pasteboard.image;
// NSData *data = [pasteboard dataForPasteboardType:@"shareImageLE"];
// self.imageV.image = [UIImage imageWithData:data];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"关闭" style:UIBarButtonItemStyleDone target:self action:@selector(backToApp)];
}
/*
关于UIPasteboard,如果直接使用pasteboard.string/ pasteboard.URL /pasteboard.image等,一次只能使用一个,且用系统的话,都可以被使用(- (void)setXXX:(id)data forPasteboardType:(NSString *)pasteboardType;)
可以避免全局被使用(需要typeName);
传字典:需要归档
NSDictionary *dic = @{@"name":@"哈哈碉堡啦",@"image":[UIImage imageNamed:@"qq"]};
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
NSData *dictData = [NSKeyedArchiver archivedDataWithRootObject:dic];
[pasteboard setData:dictData forPasteboardType:@"AType"];
接收:
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
NSDictionary *dict = [NSKeyedUnarchiver unarchiveObjectWithData:[pasteboard dataForPasteboardType:@"AType"]];
NSString *name = [dict objectForKey:@"name"];
*/
- (void)backToApp {
// 1.拿到对应应用程序的URL Scheme
NSArray *arr = [self.urlString componentsSeparatedByString:@"?"];
NSString *urlSchemeString = arr[1];
NSString *urlString = [urlSchemeString stringByAppendingString:@"://"];
// 2.获取对应应用程序的URL
NSURL *url = [NSURL URLWithString:urlString];
// 3.判断是否可以打开
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
}
}
以上是关于App间相互跳转及图片分享的主要内容,如果未能解决你的问题,请参考以下文章