iOS---從瀏覽器啟動應用程式

來源:互聯網
上載者:User

標籤:

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.