欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

App间相互跳转及图片分享

程序员文章站 2022-07-20 20:57:51
A-app: Info--URL Types--URL Schemes:A-app(一个标识,允许别的app调用本App) info.plist 添加白名单: LSApplicationQueriesSchemes(Array) B-app(String) //使用 - (void)jumpToBa ......

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

    }

}