源代码下载:http://download.csdn.net/detail/haogaoming123/8626957
通过URL协议实现从Safari等浏览器中跳转打开你的app,实现这样的功能并不麻烦,通过将网上一些相关教程汇总以后就写了下面的教程分享。
实现效果如下,在浏览器中输入“haogaoming.com://”之后就会打开这个程序,打开后程序中会显示跳转过来的链接地址。
下边是启动应用程序之后的画面,alert一下请求过来的协议。
第一步:在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]];//取出来的值