diff --git a/librx.xcodeproj/project.pbxproj b/librx.xcodeproj/project.pbxproj index ec18476..903c043 100644 --- a/librx.xcodeproj/project.pbxproj +++ b/librx.xcodeproj/project.pbxproj @@ -9,6 +9,7 @@ /* Begin PBXBuildFile section */ 18988A881B93641900FCB1A8 /* libiconv.2.4.0.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 18988A871B93641900FCB1A8 /* libiconv.2.4.0.dylib */; }; 4501B6A21BDFA66F00B9D4AF /* LogupViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4501B6A11BDFA66F00B9D4AF /* LogupViewController.swift */; }; + 450BED401C03369F0094BDCF /* NSURL-Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 450BED3F1C03369F0094BDCF /* NSURL-Extensions.swift */; }; 450C3AAC1BC621F7006B5A5F /* QuartzCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 450C3AAB1BC621F7006B5A5F /* QuartzCore.framework */; }; 450C3AAE1BC62201006B5A5F /* ImageIO.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 450C3AAD1BC62201006B5A5F /* ImageIO.framework */; }; 450C3AB01BC62214006B5A5F /* Security.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 450C3AAF1BC62214006B5A5F /* Security.framework */; }; @@ -147,6 +148,7 @@ 18988A871B93641900FCB1A8 /* libiconv.2.4.0.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libiconv.2.4.0.dylib; path = usr/lib/libiconv.2.4.0.dylib; sourceTree = SDKROOT; }; 3C589578C2D1AD931B429F24 /* Pods-librx.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-librx.release.xcconfig"; path = "Pods/Target Support Files/Pods-librx/Pods-librx.release.xcconfig"; sourceTree = ""; }; 4501B6A11BDFA66F00B9D4AF /* LogupViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LogupViewController.swift; sourceTree = ""; }; + 450BED3F1C03369F0094BDCF /* NSURL-Extensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "NSURL-Extensions.swift"; sourceTree = ""; }; 450C3AAB1BC621F7006B5A5F /* QuartzCore.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = QuartzCore.framework; path = System/Library/Frameworks/QuartzCore.framework; sourceTree = SDKROOT; }; 450C3AAD1BC62201006B5A5F /* ImageIO.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = ImageIO.framework; path = System/Library/Frameworks/ImageIO.framework; sourceTree = SDKROOT; }; 450C3AAF1BC62214006B5A5F /* Security.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Security.framework; path = System/Library/Frameworks/Security.framework; sourceTree = SDKROOT; }; @@ -476,6 +478,7 @@ 45C24EFD1BADA48E00BACB81 /* UIColorExtension.swift */, 45D09EAF1BDBD375006F91C2 /* LineChart.swift */, 4541536D1BE7B34800A8C681 /* CAPSPageMenu.swift */, + 450BED3F1C03369F0094BDCF /* NSURL-Extensions.swift */, ); name = extensions; sourceTree = ""; @@ -897,6 +900,7 @@ 45F6B0B51B95CCA200BDF464 /* BaiscTabBarController.swift in Sources */, 45D4B1CB1BDCC55600E3FA32 /* ExploreTableViewController.swift in Sources */, 45D4B1CD1BDCC76600E3FA32 /* ExploreSource.swift in Sources */, + 450BED401C03369F0094BDCF /* NSURL-Extensions.swift in Sources */, 45C24EFE1BADA48E00BACB81 /* UIColorExtension.swift in Sources */, 45D09EB01BDBD375006F91C2 /* LineChart.swift in Sources */, 45CF60481B67C3D900CA7F06 /* StorysTableViewController.swift in Sources */, diff --git a/librx/CreatePostViewController.swift b/librx/CreatePostViewController.swift index ca489d4..702d5bc 100644 --- a/librx/CreatePostViewController.swift +++ b/librx/CreatePostViewController.swift @@ -44,22 +44,29 @@ class CreatePostViewController: UITableViewController { let title = self.share_title?.text let link = self.share_link?.text - let post = Post(title: title!, type: shareType, url: link!) - - LibrxService.createPost(post) { [weak self](json) -> () in - - if let selfView = self { - - if let msg = json["msg"].string { - selfView.alert(msg) + NSURL.validateUrl(link) { [weak self](success, urlString, error) -> Void in + if let _ = self { + if success { + let post = Post(title: title!, type: self!.shareType, url: link!) + LibrxService.createPost(post) { [weak self](json) -> () in + + if let selfView = self { + if let msg = json["msg"].string { + selfView.alert(msg) + }else { + selfView.view.makeToast("分享成功", duration:3.0, position: CSToastPositionTop) + NSTimer.scheduledTimerWithTimeInterval(2.0, target: selfView, selector: Selector("dismiss"), userInfo: nil, repeats: false) + } + }} + }else { - selfView.view.makeToast("分享成功", duration:3.0, position: CSToastPositionTop) - NSTimer.scheduledTimerWithTimeInterval(2.0, target: selfView, selector: Selector("dismiss"), userInfo: nil, repeats: false) + self?.view.makeToast("请输入正确的链接地址", duration:2.0, position: CSToastPositionTop) } } } + } func dismiss() { diff --git a/librx/LibrxService.swift b/librx/LibrxService.swift index 3c971df..0402f71 100644 --- a/librx/LibrxService.swift +++ b/librx/LibrxService.swift @@ -373,6 +373,8 @@ struct LibrxService { let json = JSON(data: data!) + print(json) + response(json) } diff --git a/librx/NSURL-Extensions.swift b/librx/NSURL-Extensions.swift new file mode 100644 index 0000000..678671f --- /dev/null +++ b/librx/NSURL-Extensions.swift @@ -0,0 +1,55 @@ +// +// NSURL-Extensions.swift +// librx +// +// Created by Yunlong Zheng on 11/23/15. +// Copyright © 2015 librx. All rights reserved. +// + +import Foundation + +extension NSURL +{ + + class func validateUrl(urlString: String?, completion:(success: Bool, urlString: String? , error: NSString) -> Void) + { + // Description: This function will validate the format of a URL, re-format if necessary, then attempt to make a header request to verify the URL actually exists and responds. + // Return Value: This function has no return value but uses a closure to send the response to the caller. + + var formattedUrlString : String? + + // Ignore Nils & Empty Strings + if (urlString == nil || urlString == "") + { + completion(success: false, urlString: nil, error: "Url String was empty") + return + } + + // Ignore prefixes (including partials) + let prefixes = ["http://www.", "https://www.", "www."] + for prefix in prefixes + { + if ((prefix.rangeOfString(urlString!, options: NSStringCompareOptions.CaseInsensitiveSearch, range: nil, locale: nil)) != nil){ + completion(success: false, urlString: nil, error: "Url String was prefix only") + return + } + } + + // Ignore URLs with spaces (NOTE - You should use the below method in the caller to remove spaces before attempting to validate a URL) + // Example: + // textField.text = textField.text.stringByReplacingOccurrencesOfString(" ", withString: "", options: nil, range: nil) + let range = urlString!.rangeOfCharacterFromSet(NSCharacterSet.whitespaceCharacterSet()) + if let _ = range { + completion(success: false, urlString: nil, error: "Url String cannot contain whitespaces") + return + } + + // Check that URL already contains required 'http://' or 'https://', prepend if it does not + formattedUrlString = urlString + if (!formattedUrlString!.hasPrefix("http://") && !formattedUrlString!.hasPrefix("https://")) + { + formattedUrlString = "http://"+urlString! + } + + } +} \ No newline at end of file