Skip to content

Commit

Permalink
Nil check fix for mediaURL
Browse files Browse the repository at this point in the history
  • Loading branch information
daansari committed Aug 21, 2017
1 parent 0707273 commit f30dd03
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "OnTheMap/Extension/StudentMapVC/OTM_StudentsMapDelegateExtension.swift"
timestampString = "524949928.886234"
timestampString = "525010344.032882"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "22"
Expand Down Expand Up @@ -111,8 +111,24 @@
endingColumnNumber = "9223372036854775807"
startingLineNumber = "50"
endingLineNumber = "50"
landmarkName = "unknown"
landmarkType = "0">
landmarkName = "setupUIForTheMapViewWithStudentLocationData()"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "OnTheMap/Extension/StudentTableVC/OTM_StudentsTableDelegateExtension.swift"
timestampString = "525010344.033061"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "43"
endingLineNumber = "43"
landmarkName = "tableView(_:didSelectRowAt:)"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
</Breakpoints>
Expand Down
10 changes: 7 additions & 3 deletions OnTheMap/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@
<objects>
<viewController id="OSe-rU-TiV" customClass="OTM_StudentsTableViewController" customModule="OnTheMap" customModuleProvider="target" sceneMemberID="viewController">
<view key="view" contentMode="scaleToFill" id="gG1-YP-gcE">
<rect key="frame" x="0.0" y="0.0" width="375" height="550"/>
<rect key="frame" x="0.0" y="0.0" width="375" height="554"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<tableView clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="plain" separatorStyle="default" rowHeight="-1" estimatedRowHeight="-1" sectionHeaderHeight="28" sectionFooterHeight="28" translatesAutoresizingMaskIntoConstraints="NO" id="kh9-i5-5ss">
Expand Down Expand Up @@ -286,7 +286,7 @@
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<mapView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" mapType="standard" showsUserLocation="YES" showsScale="YES" translatesAutoresizingMaskIntoConstraints="NO" id="cHq-j6-Eej">
<rect key="frame" x="0.0" y="0.0" width="375" height="550"/>
<rect key="frame" x="0.0" y="0.0" width="375" height="554"/>
<connections>
<outlet property="delegate" destination="UFk-oM-B1a" id="kNd-0h-L2x"/>
</connections>
Expand Down Expand Up @@ -521,6 +521,9 @@
</view>
<mapView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" mapType="standard" showsUserLocation="YES" translatesAutoresizingMaskIntoConstraints="NO" id="uQb-3H-uNk">
<rect key="frame" x="0.0" y="250" width="375" height="367"/>
<connections>
<outlet property="delegate" destination="kgm-Y1-UtF" id="Up8-qh-Owr"/>
</connections>
</mapView>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="H3f-as-9sg">
<rect key="frame" x="0.0" y="617" width="375" height="50"/>
Expand Down Expand Up @@ -578,6 +581,7 @@
<outlet property="enterYourLocationStackView" destination="4NX-qB-05W" id="RwS-wu-lRy"/>
<outlet property="enterYourLocationTextField" destination="grv-0H-tZ2" id="7mN-7N-yWb"/>
<outlet property="findOnMapBtn" destination="vze-Dj-2D6" id="6fZ-VN-ljs"/>
<outlet property="mapView" destination="uQb-3H-uNk" id="V0e-bm-Fxj"/>
<outlet property="submitBtn" destination="H3f-as-9sg" id="a1H-CK-kHH"/>
</connections>
</viewController>
Expand Down Expand Up @@ -649,6 +653,6 @@
<image name="pin" width="32" height="32"/>
</resources>
<inferredMetricsTieBreakers>
<segue reference="mAc-9e-DZG"/>
<segue reference="ZXK-pJ-efz"/>
</inferredMetricsTieBreakers>
</document>
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import TPKeyboardAvoiding
import AXWireButton
import JVFloatLabeledTextField
import ChameleonFramework
import MapKit

class OTM_AddPinForStudentLocationViewController: UIViewController {

Expand All @@ -22,6 +23,7 @@ class OTM_AddPinForStudentLocationViewController: UIViewController {
@IBOutlet weak var enterLinkToShareTextField: JVFloatLabeledTextField!
@IBOutlet weak var submitBtn: UIButton!
@IBOutlet weak var cancelBtn: UIButton!
@IBOutlet weak var mapView: MKMapView!

var parseSingleton: ParseDataSingleton!

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,26 @@ extension OTM_StudentsMapViewController: MKMapViewDelegate {
func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
if let annotation = view.annotation {
if let mediaURL = annotation.subtitle as? String {
let url = URL(string: mediaURL)
let alert = UIAlertController(title: "Open in Safari", message: "\(mediaURL)?", preferredStyle: .alert)
let yesAction = UIAlertAction(title: "Yes", style: .default, handler: { (action) in
UIApplication.shared.open(url!, options: [:], completionHandler: nil)
})
alert.addAction(yesAction)

let noAction = UIAlertAction(title: "No", style: .destructive, handler: { (action) in
})
alert.addAction(noAction)
self.present(alert, animated: true, completion: nil)
if canOpenURL(string: mediaURL) {
if let url = URL(string: mediaURL) {
let alert = UIAlertController(title: "Open in Safari", message: "\(mediaURL)?", preferredStyle: .alert)
let yesAction = UIAlertAction(title: "Yes", style: .default, handler: { (action) in
UIApplication.shared.open(url, options: [:], completionHandler: nil)
})
alert.addAction(yesAction)

let noAction = UIAlertAction(title: "No", style: .destructive, handler: { (action) in
})
alert.addAction(noAction)
self.present(alert, animated: true, completion: nil)
}
else {
TSMessage.showNotification(in: self, title: "Error", subtitle: "MediaURL not valid", type: .error)
}
}
else {
TSMessage.showNotification(in: self, title: "Error", subtitle: "MediaURL not valid", type: .error)
}
}
else {
TSMessage.showNotification(in: self, title: "Error", subtitle: "MediaURL not found", type: .error)
Expand All @@ -56,4 +65,15 @@ extension OTM_StudentsMapViewController: MKMapViewDelegate {
TSMessage.showNotification(in: self, title: "Error", subtitle: "Annotation not found", type: .error)
}
}

func canOpenURL(string: String?) -> Bool {
guard let urlString = string else {return false}
guard let url = URL(string: urlString) else {return false}
if !UIApplication.shared.canOpenURL(url) {return false}

//
let regEx = "((https|http)://)((\\w|-)+)(([.]|[/])((\\w|-)+))+"
let predicate = NSPredicate(format:"SELF MATCHES %@", argumentArray:[regEx])
return predicate.evaluate(with: string)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,40 @@ extension OTM_StudentsTableViewController: UITableViewDelegate, UITableViewDataS
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let student = self.studentLocations[indexPath.row]
if let mediaURL = student.mediaURL {
let url = URL(string: mediaURL)
let alert = UIAlertController(title: "Open in Safari", message: "\(mediaURL)?", preferredStyle: .alert)
let yesAction = UIAlertAction(title: "Yes", style: .default, handler: { (action) in
UIApplication.shared.open(url!, options: [:], completionHandler: nil)
})
alert.addAction(yesAction)

let noAction = UIAlertAction(title: "No", style: .destructive, handler: { (action) in
})
alert.addAction(noAction)
self.present(alert, animated: true, completion: nil)
if canOpenURL(string: mediaURL) {
if let url = URL(string: mediaURL) {
let alert = UIAlertController(title: "Open in Safari", message: "\(mediaURL)?", preferredStyle: .alert)
let yesAction = UIAlertAction(title: "Yes", style: .default, handler: { (action) in
UIApplication.shared.open(url, options: [:], completionHandler: nil)
})
alert.addAction(yesAction)

let noAction = UIAlertAction(title: "No", style: .destructive, handler: { (action) in
})
alert.addAction(noAction)
self.present(alert, animated: true, completion: nil)
}
else {
TSMessage.showNotification(in: self, title: "Error", subtitle: "MediaURL not valid", type: .error)
}
}
else {
TSMessage.showNotification(in: self, title: "Error", subtitle: "MediaURL not valid", type: .error)
}
}
else {
TSMessage.showNotification(in: self, title: "Error", subtitle: "MediaURL not found", type: .error)
}
}

func canOpenURL(string: String?) -> Bool {
guard let urlString = string else {return false}
guard let url = URL(string: urlString) else {return false}
if !UIApplication.shared.canOpenURL(url) {return false}

//
let regEx = "((https|http)://)((\\w|-)+)(([.]|[/])((\\w|-)+))+"
let predicate = NSPredicate(format:"SELF MATCHES %@", argumentArray:[regEx])
return predicate.evaluate(with: string)
}
}

0 comments on commit f30dd03

Please sign in to comment.