How to open twitter page in twitter app from my ip

2020-05-22 06:20发布

The page I want to open using twitter app:

https://twitter.com/#!/PAGE

To open twitter app I use the following code:

NSURL *urlApp = [NSURL URLWithString: [NSString stringWithFormat:@"%@", @"twitter://https://twitter.com/#!/PAGE"]];
[[UIApplication sharedApplication] openURL:urlApp];

But this code doesn't seem to work as expected, I got only twitter app launched without the page which i want to show.

5条回答
对你真心纯属浪费
2楼-- · 2020-05-22 06:58

The following code opens twitter page on twitter app if it is already installed, otherwise opens twitter on safari:

NSURL *twitterURL = [NSURL URLWithString:@"twitter://user?screen_name=username"];
if ([[UIApplication sharedApplication] canOpenURL:twitterURL])
    [[UIApplication sharedApplication] openURL:twitterURL];
else
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.twitter.com/username"]];

Don't forget to replace 'username' with your name.

查看更多
Rolldiameter
3楼-- · 2020-05-22 06:58

@Alexey: If you just want to know how to launch twitter from your application do this:

NSURL *urlApp = [NSURL URLWithString: [NSString stringWithFormat:@"%@", @"twitter://"]];
   if ([[UIApplication sharedApplication] canOpenURL:urlApp]){
        [[UIApplication sharedApplication] openURL:urlApp];
   }else{
        UIAlertView *appMissingAlertView = [[UIAlertView alloc] initWithTitle:@"Twitter App Not Installed!" message:@"Please install the Twitter App on your iPhone." delegate:nil cancelButtonTitle:nil otherButtonTitles:@"Ok",nil];
        [appMissingAlertView show];
        [appMissingAlertView release];
    }
查看更多
Rolldiameter
4楼-- · 2020-05-22 07:07

I know its quite a late response to this question and I agree that, Murat's answer is absolutely correct. Simply add a check as follows:

NSURL *urlApp = [NSURL URLWithString: [NSString stringWithFormat:@"%@", @"twitter:///user?screen_name=PAGE]];

if ([[UIApplication sharedApplication] canOpenURL:urlApp]){
        [[UIApplication sharedApplication] openURL:urlApp];
    }

I hope this helps someone. Cheers!! :)

查看更多
Melony?
5楼-- · 2020-05-22 07:12

You are looking for the following url:

twitter:///user?screen_name=PAGE

Note that Twitter is not installed on all devices. You should check the result of openURL method. If it fails, open the page in Safari with regular url.

查看更多
姐就是有狂的资本
6楼-- · 2020-05-22 07:12

This is the full code required in Swift. I am using Swift 4 but i believe it is the same for Swift 3.

let Username =  "YOUR_USERNAME_HERE" 
let appURL = NSURL(string: "twitter:///user?screen_name=\(Username)")!
let webURL = NSURL(string: "https://twitter.com/\(Username)")!
let application = UIApplication.shared
if application.canOpenURL(appURL as URL) {
      application.open(appURL as URL)
    } else {
        // if Twitter app is not installed, open URL inside Safari
        application.open(webURL as URL)
    }

Don't forget to add the Info keys needed to use canOpenURL: Info Keys Needed

查看更多
登录 后发表回答