網頁

顯示具有 NSURL 標籤的文章。 顯示所有文章
顯示具有 NSURL 標籤的文章。 顯示所有文章

2012年7月11日 星期三

NSURL根據RFC 1808下的解析方式

【環境:xCode4.3.2 , ios5.1 , USE ARC】
參考資料:RFC 1808 文件How to Parse an NSURL Object
說明:
ios在URL的使用下,除了可以利用URL Scheme的方式,呼叫外部app,或者開啟iTunes等應用<相關文章:ios URL Scheme Referenceapp溝通其他外部程式(一)>,還可以依照RFC 1808的規則,去解析URL的成分。

<scheme>://<net_loc>/<path>;<params>?<query>#<fragment>

根據規則,可以解析URL的成分,如下:
 
NSURL *url = [NSURL URLWithString:
 @"http://some-site.com:999/dir1/dir2;param?field-1=value-1&field-2=value-2#anchor1"];
 
NSLog(@"Scheme: %@", [url scheme]); 
NSLog(@"Host: %@", [url host]); 
NSLog(@"Port: %@", [url port]);     
NSLog(@"Path: %@", [url path]);     
NSLog(@"Relative path: %@", [url relativePath]);
NSLog(@"Path components as array: %@", [url pathComponents]);        
NSLog(@"Parameter string: %@", [url parameterString]);   
NSLog(@"Query: %@", [url query]);       
NSLog(@"Fragment: %@", [url fragment]);

The output is shown below:






As always , if you have any question , feel free to contact me.
有任何問題,請聯絡我

歡迎轉載,請註明出處,感謝。

app溝通其他外部程式(一)

【環境:xCode4.3.2 , ios5.1 , USE ARC】
參考來源:Communicating with Other Apps


說明:
透過NSURL和UIApplicetion的功能,可以呼叫外部的應用程式。
其中todolist為URL Scheme,是讓應用程式間,彼此知道對方代號的名稱


Note: If more than one third-party app registers to handle the same URL scheme, there is currently no process for determining which app will be given that scheme.
這是Guide中特別標示的一段,表示若多個app均用相同的URL scheme,則目前無法判斷。
有趣的是,國外網站有做一個搜尋目前已經使用過的scheme,目的就是為了盡量避免使用相同的scheme,並且幫助他們建立數據庫:Zwapp


關於URL scheme的介紹,可參考:ios URL Scheme Reference


//使用下方的語法,就可以透過你的app去呼叫外部的應用程式,但前提是必須取得外部應用程式的URL Scheme,才可以呼叫得到。 
NSURL *myURL = [NSURL URLWithString:@"todolist:"];
//『todolist』就是外部app的URL Scheme
[[UIApplication sharedApplication] openURL:myURL];

至於如何呼叫外部app後,做一些事情後,再返回原本的app?


若要帶一些參數呢??

<解析回傳的URL使用方式請參考:NSURL根據RFC 1808下的解析方式>


若想要返回dic的資料呢??


ios提供給我們一個很好的方法,就請參考下一篇文章:app溝通其他外部程式(二)


ps:
要注意的有:從外部app返回後,是返回最後app關到背景的頁面,無法指定跳到任意頁面。
除非動一點小手腳,這邊可以參考UIApplicationDelegate Protocol Reference有興趣的,可以參考,這邊就不多做討論。

As always , if you have any question , feel free to contact me.
有任何問題,請聯絡我

歡迎轉載,請註明出處,感謝。