-
NSURL簡介:
URL是對可以從互聯網上得到的資源的位置和訪問方法的一種簡潔的表示,是互聯網上標準資源的地址。URL可能包含遠程服務器上的資源地址,本地磁盤上的文件的路徑,甚至任意一段編碼的數據。NSURL是爲了方便我們操作。
-
NSURL用途
- 對於代表本地文件的url,你可以直接操作這些文件的屬性。例如,修改文件的最後修改時間。
- 可以使用url進行網絡通信。例如,POST 、GET請求。
- 可以使用url讀寫本地文件。例如,可以通過一個本地文件的url,調用 stringWithContentsOfURL 方法,得到NSString格式的文件內容。
- 可以使用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
當我們不遵循這一規則時,編譯器就會給出警告。
相關文章:
錯誤收集:Null passed to a callee that requires a non-null argument