標籤:
原始碼下載: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---從瀏覽器啟動應用程式