0% found this document useful (0 votes)
4 views3 pages

Ios openUrlAccordingToType

The provided code is a method that handles row selection in a UITableView, determining the action based on the type of URL associated with the selected item. It supports various URL types such as phone numbers, emails, maps, and links, executing the appropriate action like opening a mail composer or displaying a map. Additionally, it includes error handling for cases where the user has not set up a mail account or when the URL is invalid.

Uploaded by

appfroster24x7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

Ios openUrlAccordingToType

The provided code is a method that handles row selection in a UITableView, determining the action based on the type of URL associated with the selected item. It supports various URL types such as phone numbers, emails, maps, and links, executing the appropriate action like opening a mail composer or displaying a map. Additionally, it includes error handling for cases where the user has not set up a mail account or when the URL is invalid.

Uploaded by

appfroster24x7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

if let pagelist = self.getPageManger(for: self.navigationPageIdentifier)?.list


, let dicList = pagelist[indexPath.row] as? [String : Any] , var urlPath =
dicList["subtext"] as? String , let urlType = dicList["image"] as? String{

urlPath = urlPath.replacingOccurrences(of: " ", with: "")


if urlType == "name"{
print("name")

return
}else if urlType == "phone"{
self.openUrlAccordingToType(urlType: urlType, number: urlPath)

}else if urlType == "sms"{


print("sms")
self.openUrlAccordingToType(urlType: urlType, number: urlPath)
}else if urlType == "whatsapp"{

self.openUrlAccordingToType(urlType: urlType, number: urlPath)

}else if urlType == "email"{

let mailComposer = MFMailComposeViewController()


mailComposer.mailComposeDelegate = self
mailComposer.setToRecipients([(urlPath)])

if !MFMailComposeViewController.canSendMail() {
let alert = UIAlertController(title: "No Mail Accounts", message:
"Please set up a Mail account in order to send email.", preferredStyle:
UIAlertController.Style.alert)
alert.addAction(UIAlertAction(title: "OK", style: .default,
handler: nil))
self.present(alert, animated: true, completion: nil)
} else {
self.present(mailComposer, animated: true, completion: nil)
}

}else if urlType == "skype"{

guard let skype = URL(string: String(format: "skype:")) as? URL


else{return} //add object skype like this
if UIApplication.shared.canOpenURL(skype) {
if #available(iOS 10.0, *) {
UIApplication.shared.open(skype)
}
else {
UIApplication.shared.openURL(skype)
}
}
else {
guard let urlStr = URL(string:
"http://itunes.com/apps/skype/skype") as? URL else{return}
if #available(iOS 10.0, *) {
UIApplication.shared.open(urlStr)
}
else {
UIApplication.shared.openURL(urlStr)
}
}

}else if urlType == "map"{


print("map")

let location = urlPath.replacingOccurrences(of: " ", with: "+")

let actionSheet = UIAlertController(title: nil, message: nil,


preferredStyle: .actionSheet)

let getDirection = UIAlertAction(title: "Get Direction",


style: .default) { (getDirection) in

guard let urlStr = URL(string: "https://www.google.com/maps/search/?


api=1&query=\(location)") as? URL else{return}
if #available(iOS 10.0, *) {
UIApplication.shared.open(urlStr)
}else {
UIApplication.shared.openURL(urlStr)
}
}

let shareLocation = UIAlertAction(title: "Share Location",


style: .default) { (shareLocation) in

if let pagelist = self.getPageManger(for:


self.navigationPageIdentifier)?.list , let dicList = pagelist[indexPath.row] as?
[String : Any] , let sutext = dicList["subtext"] as? String ,let text =
dicList["text"] as? String {
let address = "http://www.google.com/maps/place/\(sutext)"
self.shareLocation(address:address);
}
}

let showMap = UIAlertAction(title: "Show Map", style: .default) { (showMap)


in
guard let urlStr = URL(string: "https://www.google.com/maps/search/?
api=1&query=\(location)") as? URL else{return}
if #available(iOS 10.0, *) {
UIApplication.shared.open(urlStr)
}
else {
UIApplication.shared.openURL(urlStr)
}
}

let cancel = UIAlertAction(title: "Cancel", style: .default, handler: nil)

actionSheet.addAction(getDirection)
actionSheet.addAction(shareLocation)
actionSheet.addAction(showMap)
actionSheet.addAction(cancel)

self.present(actionSheet, animated: true, completion: nil)

}else if urlType == "link"{


print("link")

if urlPath.contains("openEmail") {
var emailAddess = ""
if let email = urlPath.extractEmailAddress() {
emailAddess = email
}
self.sendEmail(emailid: emailAddess)
}

else if let url = urlPath.extractUrlFromString() {

if !url.absoluteString.contains("@") {
self.pushToWebViewController(url.absoluteString)
} else {

APPUtility.shared.showAlertOnWindow(title:
AppManager.shared.getAppName(), msg: getAppLanguage(for: "Invalid_URL") ?? "Invalid
url!", firstBtnStr: nil)
}
}
}else if urlType == "img"{
print("img")
}else {
print("name")
}

You might also like