Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions ImageSharer.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
07636B942167356A00B17AF1 /* URL+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 07636B932167356A00B17AF1 /* URL+Extension.swift */; };
4CC944BF215FA62B0068E2D5 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CC944BE215FA62B0068E2D5 /* AppDelegate.swift */; };
4CC944C1215FA62B0068E2D5 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CC944C0215FA62B0068E2D5 /* ViewController.swift */; };
4CC944C4215FA62B0068E2D5 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 4CC944C2215FA62B0068E2D5 /* Main.storyboard */; };
Expand All @@ -15,6 +16,7 @@
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
07636B932167356A00B17AF1 /* URL+Extension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "URL+Extension.swift"; sourceTree = "<group>"; };
4CC944BB215FA62B0068E2D5 /* ImageSharer.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = ImageSharer.app; sourceTree = BUILT_PRODUCTS_DIR; };
4CC944BE215FA62B0068E2D5 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
4CC944C0215FA62B0068E2D5 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = "<group>"; };
Expand All @@ -35,6 +37,14 @@
/* End PBXFrameworksBuildPhase section */

/* Begin PBXGroup section */
07636B922167355400B17AF1 /* Extensions */ = {
isa = PBXGroup;
children = (
07636B932167356A00B17AF1 /* URL+Extension.swift */,
);
path = Extensions;
sourceTree = "<group>";
};
4CC944B2215FA62B0068E2D5 = {
isa = PBXGroup;
children = (
Expand All @@ -54,6 +64,7 @@
4CC944BD215FA62B0068E2D5 /* ImageSharer */ = {
isa = PBXGroup;
children = (
07636B922167355400B17AF1 /* Extensions */,
4CC944BE215FA62B0068E2D5 /* AppDelegate.swift */,
4CC944C0215FA62B0068E2D5 /* ViewController.swift */,
4CC944C2215FA62B0068E2D5 /* Main.storyboard */,
Expand Down Expand Up @@ -137,6 +148,7 @@
files = (
4CC944C1215FA62B0068E2D5 /* ViewController.swift in Sources */,
4CC944BF215FA62B0068E2D5 /* AppDelegate.swift in Sources */,
07636B942167356A00B17AF1 /* URL+Extension.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>SchemeUserState</key>
<dict>
<key>ImageSharer.xcscheme</key>
<dict>
<key>orderHint</key>
<integer>0</integer>
</dict>
</dict>
</dict>
</plist>
25 changes: 0 additions & 25 deletions ImageSharer/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
return true
}

func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}

func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}

func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


}

5 changes: 3 additions & 2 deletions ImageSharer/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="eE3-Yb-KUw">
<rect key="frame" x="99.5" y="318.5" width="176" height="30"/>
<state key="normal" title="Share Image to Instagram"/>
<rect key="frame" x="98" y="317" width="179" height="33"/>
<fontDescription key="fontDescription" name="HelveticaNeue-CondensedBold" family="Helvetica Neue" pointSize="17"/>
<state key="normal" title="Share Image on Instagram"/>
<connections>
<action selector="instaShareAct:" destination="BYZ-38-t0r" eventType="touchUpInside" id="6hG-jV-oVR"/>
</connections>
Expand Down
18 changes: 18 additions & 0 deletions ImageSharer/Extensions/URL+Extension.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// URL+Extension.swift
// ImageSharer
//
// Created by Ranjith Kumar on 10/5/18.
// Copyright © 2018 Drawrect. All rights reserved.
//

import Foundation

extension URL: ExpressibleByStringLiteral {
public init(stringLiteral value: String) {
self = URL(string: value)!
}
}



60 changes: 25 additions & 35 deletions ImageSharer/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,61 +15,51 @@ class ViewController: UIViewController {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}


@IBAction func instaShareAct(_ sender: Any) {
self.downloadMedia(urlString: "https://images.pexels.com/photos/236047/pexels-photo-236047.jpeg?auto=compress&cs=tinysrgb&h=640")
}

func downloadMedia(urlString: String) {
guard let url = URL(string: urlString) else { return }
let url:URL = "https://images.pexels.com/photos/236047/pexels-photo-236047.jpeg?auto=compress&cs=tinysrgb&h=640"
do {
let urlData = try Data(contentsOf: url)
guard UIImage(data: urlData, scale: 640) != nil else { fatalError("Image Data error") }
UIImageWriteToSavedPhotosAlbum(UIImage(data: urlData, scale: 640)!, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil)
let contents = try Data(contentsOf: url)
guard let scaledImage = UIImage(
data: contents, scale: 640) else { fatalError("Data has corrupted") }
UIImageWriteToSavedPhotosAlbum(scaledImage, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil)
} catch let error {
debugPrint("Download media error: \(error)")
}
}
@objc func image(_ image: UIImage, didFinishSavingWithError error: NSError?, contextInfo: UnsafeRawPointer) {

@objc private func image(_ image: UIImage, didFinishSavingWithError error: NSError?, contextInfo: UnsafeRawPointer) {
if let err = error {
debugPrint("Image save error: \(err)")
} else {
sharePostOnInstagram()
}
}
@objc func sharePostOnInstagram() {

@objc private func sharePostOnInstagram() {
let fetchOptions = PHFetchOptions()
fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
let result = PHAsset.fetchAssets(with: .image, options: fetchOptions)
let assetToShare = result.count > 0 ? result[0] : nil

if let asset = assetToShare {
let localIdentifier = asset.localIdentifier
let urlString = "instagram://library?LocalIdentifier=\(localIdentifier)"
let instagramScheme = URL(string: "instagram://app")
let instagramURL = URL(string: urlString)
let application = UIApplication.shared

if let scheme = instagramScheme,
let url = instagramURL,
application.canOpenURL(scheme) {
if #available(iOS 10.0, *) {
application.open(url, options: [:]) { (success) in
debugPrint("Open \(urlString): \(success)")
}
} else {
let success = application.openURL(url)
guard let asset = result.firstObject else {fatalError("Does not find Image Asset")}
let urlString = "instagram://library?LocalIdentifier=\(asset.localIdentifier)"
let scheme:URL = "instagram://app"
let instagramURL = URL(string: urlString)
let application = UIApplication.shared

let url = instagramURL
if application.canOpenURL(scheme) {
if #available(iOS 10.0, *) {
application.open(url!, options: [:]) { (success) in
debugPrint("Open \(urlString): \(success)")
}
} else {
debugPrint("Instagram not installed on this iPhone")
let success = application.openURL(url!)
debugPrint("Open \(urlString): \(success)")
}
}
}
}