天天看點

iOS 學習 --- NSURL的常用屬性

  • NSURL簡介: 

URL是對可以從網際網路上得到的資源的位置和通路方法的一種簡潔的表示,是網際網路上标準資源的位址。URL可能包含遠端伺服器上的資源位址,本地磁盤上的檔案的路徑,甚至任意一段編碼的資料。NSURL是為了友善我們操作。
  • NSURL用途

  1. 對于代表本地檔案的url,你可以直接操作這些檔案的屬性。例如,修改檔案的最後修改時間。
  2. 可以使用url進行網絡通信。例如,POST 、GET請求。
  3. 可以使用url讀寫本地檔案。例如,可以通過一個本地檔案的url,調用 stringWithContentsOfURL 方法,得到NSString格式的檔案内容。
  4. 可以使用url進行通訊。例如,可以使用openURL:方法來撥打電話,發郵件,發短信。 
  • 示例1 

NSURL *url = [NSURL URLWithString:@"https://www.taobao.com/markets/gmall/pc-index?spm=a21bo.2017.2005-qqg.3.5af911d9pNWXru"];
    
    NSLog(@"scheme:%@", [url scheme]); //協定 https
    NSLog(@"host:%@", [url host]); //域名 www.taobao.com
    NSLog(@"absoluteString:%@", [url absoluteString]); //完整的url字元串 https://www.taobao.com/markets/gmall/pc-index?spm=a21bo.2017.2005-qqg.3.5af911d9pNWXru
    NSLog(@"relativePath: %@", [url relativePath]); //相對路徑 /markets/gmall/pc-index
    NSLog(@"port :%@", [url port]); // 端口 (null)
    NSLog(@"path: %@", [url path]); // 路徑  /markets/gmall/pc-index
    NSLog(@"Query:%@", [url query]); //查詢字元串 spm=a21bo.2017.2005-qqg.3.5af911d9pNWXru
    NSLog(@"parameterString參數字元串:%@",[url parameterString]);
    NSLog(@"pathComponents:%@", [url pathComponents]);//路徑組成:
//    (
//     "/",
//     markets,
//     gmall,
//     "pc-index"
//     )
           
2019-01-09 17:55:00.502918+0800 TESTDEMO[1339:333290] scheme:https
2019-01-09 17:55:00.503081+0800 TESTDEMO[1339:333290] host:www.taobao.com
2019-01-09 17:55:00.503147+0800 TESTDEMO[1339:333290] absoluteString:https://www.taobao.com/markets/gmall/pc-index?spm=a21bo.2017.2005-qqg.3.5af911d9pNWXru
2019-01-09 17:55:00.503212+0800 TESTDEMO[1339:333290] relativePath: /markets/gmall/pc-index
2019-01-09 17:55:00.503263+0800 TESTDEMO[1339:333290] port :(null)
2019-01-09 17:55:00.503318+0800 TESTDEMO[1339:333290] path: /markets/gmall/pc-index
2019-01-09 17:55:00.503371+0800 TESTDEMO[1339:333290] Query:spm=a21bo.2017.2005-qqg.3.5af911d9pNWXru
2019-01-09 17:55:00.503486+0800 TESTDEMO[1339:333290] parameterString參數字元串:(null)
2019-01-09 17:55:00.503966+0800 TESTDEMO[1339:333290] pathComponents:(
    "/",
    markets,
    gmall,
    "pc-index"
)
           
  • 代碼示例4

//打電話
    NSString *str = [NSString stringWithFormat:@"telprompt://%@",@"02134134567"];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
//發短信
    NSString *str = [NSString stringWithFormat:@"sms://10086"];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
//發郵件
    NSString *str = [NSString stringWithFormat:@"mailto://[email protected]"];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
           
- (BOOL)openURL:(NSURL*)url NS_DEPRECATED_IOS(2_0, 10_0, "Please use openURL:options:completionHandler: instead") NS_EXTENSION_UNAVAILABLE_IOS("");
           
iOS10以後這個方法被棄用了。用下面方法代替。
- (void)openURL:(NSURL*)url options:(NSDictionary<NSString *, id> *)options completionHandler:(void (^ __nullable)(BOOL success))completion NS_AVAILABLE_IOS(10_0) NS_EXTENSION_UNAVAILABLE_IOS("");
           
NSString *str = [NSString stringWithFormat:@"telprompt://%@",@"02134134567"];
    NSDictionary *dict = [NSDictionary dictionary];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str] options:dict completionHandler:nil];
           

這個地方有個 options 參數 ,如果寫 nil ,就會報警告 Null passed to a callee that requires a non-null argument,如下圖,

是因為options不能為空,

__nullable 

指代對象可以為NULL或者為NIL

__nonnull 

指代對象不能為null

當我們不遵循這一規則時,編譯器就會給出警告。

iOS 學習 --- NSURL的常用屬性

相關文章:

Object-C中的黑魔法

錯誤收集:Null passed to a callee that requires a non-null argument

nullable與nonnull

UIApplication