How to "preselect" UIActivityViewController Icon? - ios

I would like to redirect the user to an app that could be selected in UIActivityViewController.
For example, the "Message" app is often shown in UIActivityViewController. How could I,in advance, directly redirect the user to this app without showing the UIActivityViewController?
I would be glad to know if someone can help me out.

If you need to open the Messages App, it's simple.
#IBAction func openMessagesApp(sender: UIButton){
// Swift 3
UIApplication.sharedApplication().openURL(NSURL(string: "sms:")!)
// Swift 4
UIApplication.shared.open(URL(string: "sms:")!, options: [:], completionHandler: nil)
}

Try this one!
#IBAction func openMessagesApp(sender: UIButton){
let sms = "sms:\(number)&body=\(message or link)"
let url = URL(string:sms)!
let shared = UIApplication.shared
if(shared.canOpenURL(url)){
shared.openURL(url)
} else{
print("unable to send message")
}
}

Related

Hyperlinking a button

I am trying to create a button that goes to a link when pressed using Swift 3. When I run the app and click the button, nothing happens. Here is the code for the button's #IBAction:
#IBAction func facebookButton(_ sender: AnyObject) {
if let url = NSURL(string: "http://www.facebook.com/MatchWear-1638094356470603/info?tab=overview"){
UIApplication.shared.open(url as URL, options: [:], completionHandler: nil)
}
}
Assuming that you are pretty sure that the #IBAction is connected to your button, it should looks like this:
if let url = URL(string: "http://www.facebook.com/MatchWear-1638094356470603/info?tab=overview") , UIApplication.shared.canOpenURL(url) {
UIApplication.shared.openURL(url)
} else {
// invalid URL...
}
Hope this helped.

Open URL from UIButton in CollectionViewCell

I have a UIButton in my UICollectionViewCell and it's getting data from JSON. Now I need to open a URL from each button (each button have a different url that also comes from JSON).
I managed to open the URL with:
let weburl = "http://example.com"
UIApplication.shared.openURL(URL(string: weburl)!)
But now I need to kinda pass an url to each button. Any ideas of how can i achieve this?
You can have an array of urls:
let urls = [url1, url2, ...]
And then assign the tag property of each button to the index of its corresponding url. Now you can easily manage what you want:
#IBAction func handleTouch(_ sender: UIButton) {
// assumes that the buttons' tags start at 0, which isn't a good idea.
// see #rmaddy comment bellow
let url = urls[sender.tag]
// use the version of the open method shown bellow because the other one becomes deprecated in iOS 10
UIApplication.shared.open(URL(string: url)!, options: [:], completionHandler: nil)
}
EDIT
Other solution would be to just store the url in the cell itself, and in the button handler open the url corresponding to its cell.
FYI openURL is deprecated in iOS 10. I suggest the following if you need to support older versions of ios:
let url = URL(string: "alexa://")!
if #available(iOS 10, *) {
UIApplication.shared.open(url, options: [:], completionHandler: {
(success) in
guard success else {
//Error here
}
//Success here
})
} else {
if let success = UIApplication.shared.openURL(url) {
//Success here
} else {
//Error here
}
}
Otherwise just use UIApplication.shared.open. Also I would add a URL field to the data model you are passing to your tableViewCell and just look up the URL from the model.

How to open Facebook Messenger and direct to someone?

I have FBSDK already and can use FBSDKShareKit send something on Messenger.
I want just open a chat with someone in Facebook Messenger. like this:
Click a button in my app(sample: Facebook app):
http://i.imgur.com/j60P3Ng.png
Go to Facebook Messenger and open a chat with this person:
http://i.imgur.com/yU0EXKF.png
How Can I do it?
Swift 4
UI
In my app I show a user's Facebook ID as a button:
Code
#IBAction func messengerButton(_ sender: UIButton) {
if let id = sender.titleLabel?.text {
if let url = URL(string: "fb-messenger://user-thread/\(id)") {
// Attempt to open in Messenger App first
UIApplication.shared.open(url, options: [:], completionHandler: {
(success) in
if success == false {
// Messenger is not installed. Open in browser instead.
let url = URL(string: "https://m.me/\(id)")
if UIApplication.shared.canOpenURL(url!) {
UIApplication.shared.open(url!)
}
}
})
}
}
}
User's Facebook ID
The user can get another person's Facebook ID from looking them up on Facebook and then observing the URL:
I found a solution with URL Scheme:
let userID = 4
let urlStr = String(format: "fb-messenger://user-thread/%d", userID)
let theUrl = NSURL(string: urlStr)
[UIApplication.sharedApplication() .openURL(theUrl!)]
This code will open Message application and talk to Mark.
in swift 5.1, use
guard let messenger = URL(string: "fb-messenger://user-thread/your_id") else { return }
UIApplication.shared.open(messenger)

Swift - Rate App

I'm trying to add a Rate App button on my iOS Application and I've looked everywhere online by trying different ways of doing it but none of them have worked.
Here's what I've tried
1.
let appId = 1040912970
#IBAction func btnRateApp(sender: UIButton) {
let url = "itunes.apple.com/app/id\(appId)"
UIApplication.sharedApplication().openURL(NSURL(string: url)!)
}
2.
let appId = 1040912970
#IBAction func btnRateApp(sender: UIButton) {
let urlString = "http://itunes.apple.com/app/id\(appId)?mt=8"
let url = NSURL(string: urlString)
UIApplication.sharedApplication().openURL(url!)
}
3.
#IBAction func btnRateApp(sender: UIButton) {
UIApplication.sharedApplication().openURL(NSURL(string: url)!)
UIApplication.sharedApplication().openURL(NSURL(string:"https://itunes.apple.com/us/app/wsw-song-chants/id1040912970?ls=1&mt=8")!);
}
4.
#IBAction func btnRateApp(sender: UIButton) {
UIApplication.sharedApplication().openURL(NSURL(string : "itms-apps://itunes.apple.com/app/id1040912970")!);
}
When do this one above and click the button, a message on the output console will say
ERROR: There is no registered handler for URL scheme itms-apps Message
from debugger: Terminated due to signal 15
I've also checked if my app Id is correct which shows on itunes connect.
First and foremost you must use store-kit in order to actually have in-app ratings.
Secondly check iRate GitHub Page
It's a very common requirement and this Library will handle it.

How can I add a link for a rate button with swift?

First I don't know how to get the link before I submit my app, and if the link is for each country app store or is it universal?
Also I don't know if the way to do it is just by putting the link there like:
#IBAction func rate(sender: AnyObject) {
UIApplication.sharedApplication().openURL(NSURL(string : "webLinkHere")!)
}
Or should I use another way to do this?
Thanks
Try This, change appId in your method by your App ID
Swift 5
import StoreKit
func rateApp() {
if #available(iOS 10.3, *) {
SKStoreReviewController.requestReview()
} else if let url = URL(string: "itms-apps://itunes.apple.com/app/" + "appId") {
if #available(iOS 10, *) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(url)
}
}
}
Swift 3 \ 4
func rateApp() {
guard let url = URL(string: "itms-apps://itunes.apple.com/app/" + "appId") else {
return
}
if #available(iOS 10, *) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(url)
}
}
id959379869 This is the id when you go on your Itune page of your app
Example :
https://itunes.apple.com/fr/app/hipster-moustache/id959379869?mt=8
How get the ID :
Itunesconnect account
My Apps
Click on "+" Button
New iOS App
Fill require details
After filling all details goto your App
Click on More Button
View on AppStore
It will redirect you to your App URL this will be universal
Look Http URL
This is working the best for me.
Directs the user straight to the 'Write A Review' composer of the application.
Swift 3.1 (Support for iOS10 and below)
Introduces new action=write-review
let appID = "959379869"
if let checkURL = URL(string: "http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?id=\(appID)&pageNumber=0&sortOrdering=2&type=Purple+Software&mt=8") {
open(url: checkURL)
} else {
print("invalid url")
}
...
func open(url: URL) {
if #available(iOS 10, *) {
UIApplication.shared.open(url, options: [:], completionHandler: { (success) in
print("Open \(url): \(success)")
})
} else if UIApplication.shared.openURL(url) {
print("Open \(url)")
}
}
Tested and works on Swift 2.2
let appID = "959379869" // Your AppID
if let checkURL = NSURL(string: "http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?id=\(appID)&pageNumber=0&sortOrdering=2&type=Purple+Software&mt=8") {
if UIApplication.sharedApplication().openURL(checkURL) {
print("url successfully opened")
}
} else {
print("invalid url")
}
Swift 4
let url = URL(string: "itms-apps:itunes.apple.com/us/app/apple-store/id\(YOURAPPID)?mt=8&action=write-review")!
UIApplication.shared.openURL(url)
Now after iOS 10.3+
The SKStoreReviewController allows users to rate an app directly from within the app through a dialog box. The only downsite is that you can only request StoreKit to display the dialog, but can't be sure if it will.
import StoreKit
func requestToRate() {
SKStoreReviewController.requestReview()
}
Swift 5.1: The following function sends your user directly to the review section of ANY store, not just on the American one:
func rateApp(id : String) {
guard let url = URL(string : "itms-apps://itunes.apple.com/app/id\(id)?mt=8&action=write-review") else { return }
if #available(iOS 10.0, *) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(url)
}
}
Usage:
rateApp(id: "1287000522")
Important Note: This doesn't work on simulator! Test it on a real device.
You can use the following function and replace the APP_ID with your one. Calling this function will open the app in app store link and user will see the Review page where he can click and write a review easily.
func rateApp(){
UIApplication.sharedApplication().openURL(NSURL(string : "itms-apps://itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?id=\(APP_ID)&onlyLatestVersion=true&pageNumber=0&sortOrdering=1)")!);
}
For iOS 10.3+ you can use SKStoreReviewController with simple dialog, and choose rating in alert-style window. To use it, you should import StoreKit library. So, universal way to rate your app inside itself is like this:
import StoreKit
func rateApp(){
if #available(iOS 10.3, *) {
SKStoreReviewController.requestReview()
} else {
guard let url = URL(string: "itms-apps://itunes.apple.com/ru/app/cosmeteria/id1270174484") else {
return
}
if #available(iOS 10.0, *) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(url)
}
}
}
And when you try to launch it in simulator, you won't see App Store window, so try it on device and it gonna work. This way covers all iOS versions, using all abilities. And the part of path in you application address "/us/app" means your App Store localisation, for example "us" means USA. You can easily find your app id in address string just by opening app in App Store in any browser.To get the link, just copy address in browser. Changing "https://" for "itms-apps://" lets you to open app in App Store application, while "https" opens web page in Safari
WARNING: If you are running your app on a simulator
UIApplication.sharedApplication().openURL(NSURL(string : "url")!)
will not work because there is no app store in the simulator. In order to test this functionality you must run your app on a device.
Swift 3
func rateApp(){
UIApplication.shared.open(URL(string : "itms-apps://itunes.apple.com/app/id959379869")!, options: [:]) { (done) in
// Handle results
}}
id959379869 This is the id when you go on your iTunes page of your app
Goto your itunesconnect account -> My Apps -> Click on "+" Button ->New iOS App -> Fill require details -> After filling all details goto your App -> Click on More Button -> View on AppStore -> it will redirect you to your App URL this will be universal & will be same after your app goes live .
All the above answers are not best practices they might be affecting your app store ratings. For best practice use the below code.
func ReviewAppController() {
let alert = UIAlertController(title: "Feedback", message: "Are you enjoying our App?", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Dismis", style: .cancel, handler: nil))
alert.addAction(UIAlertAction(title: "Yes, i Love it!", style: .default, handler: {_ in
SKStoreReviewController.requestReview()
}))
alert.addAction(UIAlertAction(title: "No, this sucks!", style: .default, handler: {_ in
//Collect feedback
}))
present(alert, animated: true)
}
This link opens your app page in the App Store and then presents the write review sheet.
itms-apps://itunes.apple.com/app/id[APP_ID]?action=write-review
You can find the APP_ID in the App Store Connect under the details of your app as Apple ID.
In case you want to directly write a review rather than just open an app page:
if let url = URL(string: "https://itunes.apple.com/in/app/\(yourappname)/id\(yourAppleAppId)?ls=1&mt=8&action=write-review") {
if #available(iOS 10.0, *) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
} else {
// Earlier versions
if UIApplication.shared.canOpenURL(url as URL) {
UIApplication.shared.openURL(url as URL)
}
}
}

Resources