通过URL协议实现从Safari等浏览器中跳转打开你的app

效果如图,打开safari或其他浏览器输入abcdefg://
通过URL协议实现从Safari等浏览器中跳转打开你的app_第1张图片

然后回车确认跳转到app

通过URL协议实现从Safari等浏览器中跳转打开你的app_第2张图片

下边是具体流程:
1、首先在项目中的plist文件中添加如下内容(红色箭头标明出是随便写的,其中abcdefg为跳转时的URL前缀,com.abc.abc尚未发现有何作用...)
通过URL协议实现从Safari等浏览器中跳转打开你的app_第3张图片
2、在视图控制器中有如下方法

+(void)alert:(NSString*)information{

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

    [alert show];

    [alert release];

}

3、在项目入口类AppDelegate.m中有如下回调方法

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{

    

   NSString *urlString=[url absoluteString];

    [ViewController alert:urlString];//调用视图弹出框

    

    NSLog(@"openURL---->%@",url);

    NSLog(@"sourceApplication---->%@",sourceApplication);

    NSLog(@"annotation---->%@",annotation);

    

    return YES;

}

通过URL协议实现从Safari等浏览器中跳转打开你的app

版权注明,原文链接:http://www.cocoachina.com/bbs/read.php?tid=109315

你可能感兴趣的:(通过URL协议实现从Safari等浏览器中跳转打开你的app)