1. 程式人生 > >App間相互跳轉及圖片分享

App間相互跳轉及圖片分享

present select window named 應用 細節 item lis jump

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間相互跳轉及圖片分享