I have been trying to add a 'Follow us on Twitter' button to my app, however I have ran into a few issues.
First of all, if the user does not have Twitter installed, the code that I intended to redirect them to the website version of Twitter instead, doesn't seem to work.
Also, how can I handle situations when the users is offline? I would like to display a UIAlert to warn them that they must have a connection to use Twitter.
Here is my code:
#IBAction func followOnTwitter(sender: AnyObject) {
if UIApplication.sharedApplication().openURL(NSURL.URLWithString("twitter://user?screen_name=AffordIt_App")) {
if UIApplication.sharedApplication().openURL(NSURL.URLWithString("https://twitter.com/AffordIt_App")) {
}
}
}
From what I understand you need to see if the user has Twitter installed first.
#IBAction func followOnTwitter(sender: AnyObject) {
let screenName = "Your Twitter Handle"
let appURL = NSURL(string: "twitter://user?screen_name=\(screenName)")!
let webURL = NSURL(string: "https://twitter.com/\(screenName)")!
let application = UIApplication.sharedApplication()
if application.canOpenURL(appURL) {
application.openURL(appURL)
} else {
application.openURL(webURL)
}
}
I am new to this site and apologize if I am not posting in the proper format.
Related
When I open the iOS Facebook app with
fb://profile/44083897053
the app opens and goes to the proper page as expected.
When I use
fb://profile/philamuseum
the app opens, but I am taken to my timeline.
Can anyone explain why this is happening?
The code being used is:
#IBAction func gotoFacebook(_ sender: Any) {
UIApplication.tryURL(urls:["fb://profile/\(self.facebookUser)/", "https://www.facebook.com/\(self.facebookUser)"])
}
. . . .
extension UIApplication {
class func tryURL(urls: [String]) {
let application = UIApplication.shared
for url in urls {
if application.canOpenURL(URL(string: url)!) {
application.open(URL(string: url)!)
return
}
}
}
I've looked at number of questions re opening facebook from an iOS app here and elsewhere and have not found any recent answer that bears on this issue.
I'm adding an iMessage extension target to my app. The extension is supposed to send a message that has a url attribute. The behaviour I'm expecting when a user touches the message is to open the browser using the url attribute of the message.
I have a button in my messageView which executes this code:
#IBAction func labelButton(_ sender: Any) {
let layout = MSMessageTemplateLayout()
layout.imageTitle = "iMessage Extension"
layout.caption = "Hello world!"
layout.subcaption = "Test sub"
guard let url: URL = URL(string: "https://google.com") else { return }
let message = MSMessage()
message.layout = layout
message.summaryText = "Sent Hello World message"
message.url = url
activeConversation?.insert(message, completionHandler: nil)
}
If I touch the message, it expands the MessageViewController
I have then added this:
override func didSelect(_ message: MSMessage, conversation: MSConversation) {
if let message = conversation.selectedMessage {
// message selected
// Eg. open your app:
self.extensionContext?.open(message.url!, completionHandler: nil)
}
}
And now, when I touch the message, it opens my main app but still not my browser.
I have seen on another post (where I cannot comment, thus I opened this post) that it is impossible to open in Safari but I have a news app which inserts links to articles and allows with a click on the message to open the article in a browser window, while the app is installed.
So, can someone please tell how I can proceed to force opening the link in a browser window?
Thank you very much.
Here is a trick to insert a link in a message. It does not allow to create an object that has an url attribute but just to insert a link directly which will open in the default web browser.
activeConversation?.insertText("https://google.com", completionHandler: nil)
I have published a sample on github showing how to launch a URL from inside an iMessage extension. It just uses a fixed URL but the launching code is what you need.
Copying from my readme
The obvious thing to try is self.extensionContext.open which is documented as Asks the system to open a URL on behalf of the currently running app extension.
That doesn't work. However, you can iterate back up the responder chain to find a suitable handler for the open method (actually the iMessage instance) and invoke open with that object.
This approach works for URLs which will open a local app, like settings for a camera, or for web URLs.
The main code
#IBAction public func onOpenWeb(_ sender: UIButton) {
guard let url = testUrl else {return}
// technique that works rather than self.extensionContext.open
var responder = self as UIResponder?
let handler = { (success:Bool) -> () in
if success {
os_log("Finished opening URL")
} else {
os_log("Failed to open URL")
}
}
let openSel = #selector(UIApplication.open(_:options:completionHandler:))
while (responder != nil){
if responder?.responds(to: openSel ) == true{
// cannot package up multiple args to openSel so we explicitly call it on the iMessage application instance
// found by iterating up the chain
(responder as? UIApplication)?.open(url, completionHandler:handler) // perform(openSel, with: url)
return
}
responder = responder!.next
}
}
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)
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.
Currently I'm trying to implement a twitter user login for my app in Xcode 7 beta with parse. I followed the docs at parse.com for twitter. After implementing, I am still running to use of unresolved identifier for
PFTwitterUtils.initializeWithConsumerKey("somekey", consumerSecret:"somekey")
and
#IBAction func twitterButtonTapped(sender: AnyObject) {
PFTwitterUtils.logInWithBlock {
(user: PFUser?, error: NSError?) -> Void in
if let user = user {
if user.isNew {
// process user object
self.processTwitterUser()
} else {
// process user object
self.processTwitterUser()
}
} else {
print("Uh oh. The user cancelled the Twitter login.")
}
}
}
and
func processTwitterUser()
{
// Show activity indicator
let spiningActivity = MBProgressHUD.showHUDAddedTo(self.view, animated: true)
spiningActivity.labelText = "Loading"
spiningActivity.detailsLabelText = "Please wait"
let pfTwitter = PFTwitterUtils.twitter()
let twitterUsername = pfTwitter?.screenName
var userDetailsUrl:String = "https://api.twitter.com/1.1/users/show.json?screen_name="
userDetailsUrl = userDetailsUrl + twitterUsername!
let myUrl = NSURL(string: userDetailsUrl);
let request = NSMutableURLRequest(URL:myUrl!);
request.HTTPMethod = "GET";
pfTwitter!.signRequest(request);
any common pitfalls I might have fell into?
Note: I added the correct frameworks (accounts etc.)
Here is a solution that worked for me.
I added the "ParseTwitterUtils.framework"
In my Bridging header I added "#import ParseTwitterUtils/ParseTwitterUtils.h "
In my AppDelegate.Swift I added "import ParseTwitterUtils"
I filled the consumerKey and secretKey (You'll find them on your Twitter dev account)
And now it's working
Hope it helped