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];

    }

}

 

转载于:https://www.cnblogs.com/code-le/p/9725053.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值