How to load a facebook page link on iOS Facebook App - objective-c

I'm trying to open a Facebook link and using the following code would conform with the URL protocol on iOS.
NSString *url = [self.dataSource getFacebookURL];
url = [url stringByReplacingOccurrencesOfString:#"http://www.facebook.com/" withString:#"fb://profile/"];
NSLog(#"LINK: %#", url);
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
But when trying to load a profile or a page (i.e. of an organization) it always brings me to the screen that was left open last. Is there a way of linking directly to the page and profile? I followed the instructions as described here, but no success.
The page I want to link to is "https://www.facebook.com/junction11" if that helps...
Thanks, Max
FIX
So I found a solution which is to load the group/person/page's graph values first, parse them and then make a new url with the Facebook-ID. The code works (don't know how well) and looks like this:
// Look at graph page instead of www page
NSString *dataURL = [url stringByReplacingOccurrencesOfString:#"www" withString:#"graph"];
// Load data and parse using JSON parser
NSData *data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:dataURL]];
NSError *error;
NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
if (!error) { NSLog(#"ERROR: %#", error); return; }
// If no error occurred, make new url and replace the username by the facebook-ID
url = [url stringByReplacingOccurrencesOfString:[dictionary objectForKey:#"username"] withString:[dictionary objectForKey:#"id"]];
// And voilà, it works :]
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
Thank you google and time...

NSURL *url = [NSURL URLWithString:#"fb://profile/<id>"];
[[UIApplication sharedApplication] openURL:url];
Schemes
fb://profile – Open Facebook app to the user’s profile
fb://friends – Open Facebook app to the friends list
fb://notifications – Open Facebook app to the notifications list (NOTE: there appears to be a bug with this URL. The Notifications page opens. However, it’s not possible to navigate to anywhere else in the Facebook app)
fb://feed – Open Facebook app to the News Feed
fb://events – Open Facebook app to the Events page
fb://requests – Open Facebook app to the Requests list
fb://notes – Open Facebook app to the Notes page
fb://albums – Open Facebook app to Photo Albums list

Related

Open OneNote from my Ipad Application - Need OneNote URL SCHEME?

When I use onenote:// for opening OneNote from my ipad Application, it opens OneNote but gives an error message that says
Cannot Open Link" There’s a problem with this link in One Note.
This is my Code
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"onenote://"]];
PS: OneNote for Ipad already has to be installed in the Ipad Device before trying this.
Is this the right URL SCHEME for OneNote - onenote://
Or am I doing it wrong.
The Error Message does note create a problem when closed, but I just want to get rid of the Error Message.
Also can parameters be passed to OneNote from my Application.
Help Appreciated!!
Using "onenote-cmd://" should open up OneNote without any errors.
The reason you're getting an error is that the iOS OneNote client doesn't support being launched by a protocol handler without having parameters completed for notebook, section, and page (it doesn't know where to navigate to).If you're just using onenote:// without specifying a destination, it's "by design" that you will receive an error.
We recommend you link to a specific page when opening OneNote from your application to avoid the error. The OneNote REST API returns a link to the page you've created when Creating a Page, and you can also retrieve a link using the "Copy Link to Page" function from within the iOS app.
As Nick pointed out, the OneNote app needs additional parameters to open successfully, without showing the "Cannot Open Link" alert. Here is what I did to solve the problem:
Note: In my app, the user is already logged in using the LiveSDK (Microsoft Account Login) so I have their Access Token saved.
I created an "Authorize OneNote" method that sends a request to the OneNote API to create a "Page" in the User's default notebook:
(void)AuthorizeOneNote
{
NSString *date = [NSString stringWithFormat:#"%#", [NSDate date]];
NSString *simpleHtml = [NSString stringWithFormat:
#"<html>"
"<head>"
"<title>\"%#\"</title>"
"<meta name=\"created\" content=\"%#\" />"
"</head>"
"<body>"
"<p></p>"
"</body>"
"</html>", #"Page Title", date];
NSData *presentation = [simpleHtml dataUsingEncoding:NSUTF8StringEncoding];
NSString *endpointToRequest = #"https://www.onenote.com/api/v1.0/pages";
NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:endpointToRequest]];
request.HTTPMethod = #"POST";
request.HTTPBody = presentation;
[request addValue:#"text/html" forHTTPHeaderField:#"Content-Type"];
[request setValue:[#"Bearer " stringByAppendingString:#"LIVE-ID-ACCESS-TOKEN"] forHTTPHeaderField:#"Authorization"];
objConnectionOneNote = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
}
This is how I handle the response:
(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
if (connection == objConnectionOneNote)
{
NSString *strData = [[NSString alloc]initWithData:objData encoding:NSUTF8StringEncoding];
NSMutableDictionary *dictData1 = [[NSMutableDictionary alloc] init];
dictData1 = [NSJSONSerialization JSONObjectWithData:objData options:kNilOptions error:nil];
oneNoteClientUrl = [dictData1 valueForKeyPath:#"links.oneNoteClientUrl.href"];
NSLog(#"Response: %#",oneNoteClientUrl);
NSLog(#"Response: %#",strData);
// Launch OneNote Client
if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:#"onenote://"]])
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:oneNoteClientUrl]];
}
else
{
[[UIApplication sharedApplication] openURL: [NSURL URLWithString:#"https://itunes.apple.com/us/app/microsoft-onenote-for-iphone/id410395246?mt=8&uo=4"]];
}
}
There are couple things to note here:
This will create a new page every single time OneNote is launched from the app. In order to open the same page, you can persist the returned URL and use that to Open the client.
The Access Token from the MS Live Authentication expires in about an hour. When this happens, the OneNote API request returns a 401 Unauthorized response. To mitigate this, you can refresh the Access Token using the refresh token, which is also returned during Live Authentication.
Hope it helps!
With the addition of new automation app Workflow for iOS I'm extremely interested in figuring out how to incorporate OneNote into my actions.
I'm not looking to make an app and thus far I cannot find any examples for advanced url schemes for OneNote so the ability to grab a link that would open the OneNote app on either iPhone or iPad and have it go to the correct notebook and page would be great.
Ideally we could use a url scheme which OneNote:// works but need additional parameters for which notebook and page and how to create either a new notebook or page with that.
Workflow goes above and beyond what IFTTT can do (situation depending) and really looking forward to starting to use OneNote more due to automation possibilities.

How to share picture on Google+?

Our two teams are developing same app: adroid team and iOS team. Android team was able to do text + photo sharing. Simple post with text is done normally but text + image requires google+ app and the code that opens small center'ed web view looks like this:
Intent shareIntent = ShareCompat.IntentBuilder.from(act)
.setText(message).setStream(uri).setType("image/jpeg")
.getIntent().setPackage("com.google.android.apps.plus");
act.startActivityForResult(shareIntent, SocialNetworksConstants.GOOGLE_POST_REQUERST);
However, I can't find anything similar in the newest Google+ iOS sdk. GPPShareBuilder protocol only has:
- (id<GPPShareBuilder>)setPrefillText:(NSString *)prefillText;
and
- (id<GPPShareBuilder>)setTitle:(NSString *)title
description:(NSString *)description
thumbnailURL:(NSURL *)thumbnailURL;
Has anybody faced this problem?
UPDATE: Here's a link to solution for Android. It looks like Android has some "ShareCompat" Android native library that extends functionality for sharing data between apps.
UPDATE2 Tried to use "setTitle:description:thumbnailURL:" with google+ sharing sample code in recent Google+ sdk. Tried to push NSURL of some image from bundle:
- (IBAction)shareButton:(id)sender{
...
if (title && description) {
//NSURL *imageUrl = [NSURL URLWithString:[deepLinkThumbnailURL_ text]];
NSString *imagePathString = [[NSBundle mainBundle] pathForResource:#"my_image" ofType:#"png"];
NSURL *imageUrl = [[NSURL alloc] initWithString:imagePathString];
shareBuilder = [shareBuilder setTitle:title
description:description
thumbnailURL:imageUrl]; // throw image path
}
However that didn't work out. Image placeholder is empty when google+ opens sharing window in safari. Only some external URL link is working.
Use this :
NSData *imageData = UIImagePNGRepresentation(myImage.image);
[(id< GPPNativeShareBuilder >)shareBuilder attachImageData:imageData];

Post photo to Instagram using their iOS hooks

I use the following code in my iOS app to use Instagram iPhone hooks to post a photo to Instagram. I only want the "Open In..." menu to have Instagram app, no other apps. But in my case Camera+ also shows up. How can I restrict to Instagram?
Also, can I directly open Instagram instead of showing Open In menu?
NSURL *instagramURL = [NSURL URLWithString:#"instagram://app"];
if ([[UIApplication sharedApplication] canOpenURL:instagramURL]) {
//imageToUpload is a file path with .ig file extension
self.documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:imageToUpload]];
self.documentInteractionController.UTI = #"com.instagram.photo";
self.documentInteractionController.annotation = [NSDictionary dictionaryWithObject:#"my caption" forKey:#"InstagramCaption"];
[self.documentInteractionController presentOpenInMenuFromBarButtonItem:self.exportBarButtonItem animated:YES];
}
BTW Instagram added an exclusive file extention (ig) and UTI (com.instagram.exclusivegram) for this. It still opens the Open with... menu but the only option is Instagram.
More info here: https://instagram.com/developer/mobile-sharing/iphone-hooks/
You can get the solution from this link.
Save image with the .igo extension instead of .ig. This is the "exclusive" version of the filetype.
Create a UIDocumentInteractionController, then assign the value com.instagram.exclusivegram to the property UTI.
Present your UIDocumentInteractionController with presentOpenInMenuFromRect:inView:animated.
This worked for me, do it like this and you will have only Instagram as the exclusive app to open your image.
NSString *documentDirectory = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents"];
// *.igo is exclusive to instagram
NSString *saveImagePath = [documentDirectory stringByAppendingPathComponent:#"Image.igo"];
NSData *imageData = UIImagePNGRepresentation(filteredImage);
[imageData writeToFile:saveImagePath atomically:YES];
NSURL *imageURL=[NSURL fileURLWithPath:saveImagePath];
_docController=[[UIDocumentInteractionController alloc]init];
_docController.delegate=self;
_docController.UTI=#"com.instagram.photo";
[_docController setURL:imageURL];
_docController.annotation=[NSDictionary dictionaryWithObjectsAndKeys:#"#yourHashTagGoesHere",#"InstagramCaption", nil];
[_docController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];
To answer only your first question: you may probably be able to restrict the "Open in ..." menu to just showing Instagram for your device (by deleting the Camera+ App, for example), but you won't be able to restrict users that install your app to their devices. And that's because the iPhone recognizes which applications are able to open a specific kind of files and it automatically show every one that does.
self.documentInteractionController = [self setupControllerWithURL:imgurl usingDelegate:self];
self.documentInteractionController=[UIDocumentInteractionController interactionControllerWithURL:imgurl];
self.documentInteractionController.UTI = #"com.instagram.exclusivegram";
use this code in same sequence .
here documentInteractionController is object of UIDocumentInteractionController.just for your knowledge.
you will get instagram only in "open in" window.

Broken (grey) thumbnails in Facebook mobile feed?

I have an iOS app that posts to users FB feeds with a link, description, and image.
If I check posts that have been made through my app in a full sized browser, I can see that everything, including the thumbnail is showing as expected.
The problem is if I open the FB feed from a mobile browser or the FB iOS app, I get grey boxes instead of thumbnails.
I have tried this multiple times and I get the grey box in mobile pages every single time and all of the Facebook debug data for the link checks out as far as I can see.
The following is the objective-c code that posts the Feed to FB.
NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
[params setObject:self.captionTextView.text forKey:#"message"];
[params setObject:[NSString stringWithFormat:#"%#'s photo on Wander", [appDelegate.prefs valueForKey:#"firstName"]] forKey:#"name"];
[params setObject:shortUrl forKey:#"link"];
NSString *imageURL = [NSString stringWithFormat:#"http://%#/mobile/photo.php?p=%#", appDelegate.apiHost, photoParams];
[params setObject:imageURL forKey:#"picture"];
[params setObject:[NSString stringWithFormat:#"%#, %#", [appDelegate.prefs valueForKey:#"cityName"], [appDelegate.prefs valueForKey:#"countryName"]] forKey:#"caption"];
[params setObject:[NSString stringWithFormat:#"I'm using Wander to explore %# with %# as a local guide!", [matchData valueForKey:#"cityName"], [matchData valueForKey:#"firstName"]] forKey:#"description"];
[appDelegate.facebook requestWithGraphPath:[NSString stringWithFormat:#"%#/feed", fbId] andParams:params andHttpMethod:#"POST" andDelegate:self];
[params release];
This is the test URL: http://maruta.wanderwith.us/viewPhoto/index/My0zNA (this might disappear because it's on a development server and I regularly clear data, I'll try my best to keep it live until this gets resolved)
Am I missing something?
I ran into the same situation. Turns out it was the question mark in your URL. Facebook passes your image URL through their own proxy. If your URL contains "?", then it doesn't work through the mobile feed somehow.
Your server expects url in this format: http://hostname/mobile/photo.php?p=1234567889. If you change this to this format: http://hostname/mobile/photo/1234567889, then Facebook will be happy. try mod rewrite if you are using apache.

UIWebView, enter a password

I want to enter a password in a webpage I load in an UIWebView. The password is a string, and I want to enter the password and hit a button in the page, automatically when the View loads.
Any help?
If you're using a UIWebView, I assume you're loading some web service into that WebView. Why not build your login system into the web service? You could even just use HTTP Basic Auth.
If you're managing the passwords within your cocoa app, then I would probably implement a UIAlertView with a UITextField asking for the password. When the user presses Login you can validate the password and then load your web service in your UIWebView.
If you're managing the passwords in a database on the website then you'd build an html/js/php/mysql login form and load it in the UIWebView first.
Update
I'd go with the first option and display your own login form using cocoa controls like the UIAlertView with 2 UITextFields. You can then save the username and password using the keychain services and submit them every time the users launches the app or the web session expires.
If you want to try this method I'd suggest having a look at the ASIHTTPRequest framework. In particular ASIFormDataRequest which let's you easily send a form with the correct fields.
- (void)login
{
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
// Change these values to the ones in NSUserDefaults and the KeyChain
[request setPostValue:#"Joe" forKey:#"username"];
[request setPostValue:#"Secret" forKey:#"password"];
[request setDelegate:self];
[request startAsynchronous];
}
- (void)requestFinished:(ASIHTTPRequest *)request
{
// Use when fetching text data
NSString *responseString = [request responseString];
// Use when fetching binary data.
NSData *responseData = [request responseData];
}
- (void)requestFailed:(ASIHTTPRequest *)request
{
NSError *error = [request error];
}
Just an idea : you could try to add some javascript to the page before displaying it in the webview and use the ONLOAD event to fill the password field and post the form.
To do so you have 2 solutions :
Use the stringByEvaluatingJavaScriptFromString instance method of UIWebView , you could evaluate your javascript in the webViewDidFinishLoad delegate method for example. You will find an example here : http://iphoneincubator.com/blog/windows-views/how-to-inject-javascript-functions-into-a-uiwebview
Or if for some reason you can't use this solution in your context you can always download the HTML using NSURLConnection, and then edit the string to insert your javascript. Then load the WebView using loadHTMLString:baseURL: