码迷,mamicode.com
首页 > 移动开发 > 详细

iOS---从浏览器启动应用程序

时间:2015-04-24 10:36:50      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:

源代码下载:http://download.csdn.net/detail/haogaoming123/8626957

通过URL协议实现从Safari等浏览器中跳转打开你的app,实现这样的功能并不麻烦,通过将网上一些相关教程汇总以后就写了下面的教程分享。

实现效果如下,在浏览器中输入“appABC://”之后就会打开这个程序,打开后程序中会显示跳转过来的链接地址。

技术分享



技术分享


第一步:在info.plist中加入这些内容

技术分享

其中URL identifier 可以随便取,URL Schemes 就是实现跳转URL协议的名称(可以多个)

然后,在视图控制器中加入这样的代码用于显示跳转过来的地址:

+(void)alert:(NSString*)information{
  UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"提示" message:[NSString stringWithFormat:@"程序通过URL协议打开,该URL为:“%@”",information] delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
  [alert show];
  [alert release];
}


  再在AppDelegate.m中加入这些代码

-(BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{
  if(!url){
    return NO;
  }
  NSString *urlString=[url absoluteString];
  [ViewController alert:urlString];
  return YES;
}

  就完成了这个看似很酷的功能,至于参数传递的问题差不多,取出值然后判断就行了:

NSArray *pathComponents = [url pathComponents];
    
    if(pathComponents.count != 3 ){
        return NO;
    }
    
    NSString *dataType = [NSString stringWithFormat:@"%@",[pathComponents objectAtIndex:1]];//取出来的值
    NSString *dataId = [NSString stringWithFormat:@"%@",[pathComponents objectAtIndex:2]];//取出来的值


iOS---从浏览器启动应用程序

标签:

原文地址:http://blog.csdn.net/haogaoming123/article/details/45242741

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!