[iOS] UIApplication內建服務的openURL

使用Line分享

 NSURL *appURL = [NSURL URLWithString:@"line://msg/text/IamHappyMan:)"];
 if ([[UIApplication sharedApplication] canOpenURL: appURL]) {
 [[UIApplication sharedApplication] openURL: appURL];
 }
 else { //如果使用者沒有安裝,連結到App Store
 NSURL *itunesURL = [NSURL URLWithString:@"itms-apps://itunes.apple.com/app/id443904275"];
 [[UIApplication sharedApplication] openURL:itunesURL];
 }

分享Line要注意的是:必須將<CONTENT KEY>的內容轉成UTF-8,

iOS有個方法能夠將字串以UTF-8來編碼:

/* Adds all percent escapes necessary to convert the receiver into a legal URL string.  Uses the given encoding to determine the correct percent escapes (returning nil if the given encoding cannot encode a particular character).  See CFURLCreateStringByAddingPercentEscapes in CFURL.h for more complex transformations

*/

– (NSString *)stringByAddingPercentEscapesUsingEncoding:(NSStringEncoding)enc;


分享圖片

UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"happyman.jpg"]];
[pasteboard setData:UIImageJPEGRepresentation(imageView.image, 0.9) forPasteboardType:@"public.jpeg"];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"line://msg/image/%@", pasteboard.name]];
[[UIApplication sharedApplication] openURL: url];

在 iOS 10中 openURL 的新方法:

// Objective-C
- (void)openURL:(NSURL*)url options:(NSDictionary *)options
 completionHandler:(void (^ __nullable)(BOOL success))completion
 
// Swift
open func open(_ url: URL, options: [String : Any] = [:],
 completionHandler completion: (@escaping (Bool) -> Swift.Void)? = nil)

現在變為三個參數

打開APP的URL
可選字典參數(請參見以下有效條目)。傳入一個空字典可以達到openURL:一樣的行為。
執行成功後completionhandler在主隊列中回調。如果你並不關心它的返回狀態也可以傳空。

 

– (BOOL)openURL:(NSURL*)url NS_DEPRECATED_IOS(2_0, 10_0, “Please use openURL:options:completionHandler: instead”) NS_EXTENSION_UNAVAILABLE_IOS(“”);

– (BOOL)canOpenURL:(NSURL *)url NS_AVAILABLE_IOS(3_0);


下面範例是 swift, 但大多 objective-c 應該也是通用。

呼叫UIApplication內建服務的openURL
以下利用UIApplication內建服務,並利用NSURL載入相關服務的scheme
版本:Xcode 7.0.1 + Swift 2

傳送簡訊
[swift]
let sms: String = “sms:0912666999”
UIApplication.sharedApplication().openURL(NSURL(string: sms)!)
[/swift]

撥打電話
[swift]
let tel: String = “tel:0912666999”
UIApplication.sharedApplication().openURL(NSURL(string: tel)!)
[/swift]

開啟網頁
[swift]
let url: String = “http://www.apple.com”
UIApplication.sharedApplication().openURL(NSURL(string: url)!)
[/swift]

開啟 App Store
[swift]
// id791084265 => id軟體編號(AppleID)
let appStoreUrl: NSURL = NSURL(string: “itms-apps://itunes.apple.com/app/id791084265”)!
UIApplication.sharedApplication().openURL(appStoreUrl)
[/swift]

前往指定評價
[swift]
// itms-apps://itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=AppleID
let str = “itms-apps://itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=791084265”
let url: NSURL = NSURL(string: str)!
UIApplication.sharedApplication().openURL(url)
[/swift]

呼叫 Line

[swift]
// b. Xcode 撰寫
let text: String! = “HappyBirthday生日快樂!!”
// 設定文字編碼,防止Line無法讀取中文
let encodeMessage: String! = text.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())
var lineURL = NSURL(string: “line://msg/text/” + encodeMessage)

if UIApplication.sharedApplication().canOpenURL(lineURL!) {
UIApplication.sharedApplication().openURL(lineURL!)
} else {
lineURL = NSURL(string: “itms-apps://itunes.apple.com/app/id443904275”)!
UIApplication.sharedApplication().openURL(lineURL!)
}
[/swift]

使用地圖
[swift]
// URL模式:http://maps.google.com/maps?q=<strong>${QUERY_STRING}</strong>
let address = “高雄市三多三路214-15號”
let urlString = “http://maps.google.com/maps?q=\(address)”

// 處理編碼
let encodeMessage: String! = urlString.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())
let url: NSURL = NSURL(string: encodeMessage)!

UIApplication.sharedApplication().openURL(url)
print(“地圖開啟中”)
[/swift]

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *