App間相互跳轉及圖片分享

A-app:mvc

Info--URL Types--URL Schemes:A-app(一個標識,容許別的app調用本App)app

info.plist 添加白名單:url

LSApplicationQueriesSchemes(Array)component

B-app(String)orm

 

//使用字符串

- (void)jumpToBapp {get

    

    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];string

    pasteboard.image = [UIImage imageNamed:@"qq"];it

 

//    NSData *imageDataa = UIImageJPEGRepresentation([UIImage imageNamed:@"qq"], 1);io

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

    }

}

相關文章
相關標籤/搜索