How to detect if the url sent to SafariViewController gives error 404 - http-status-code-404

Is there any way to detect if the url sent to SafariViewController gives error 404 (page not found) and then submit to another default url or even dismiss the SafariViewController?
I'm using this way right now, objetive-c please.
SFSafariViewController * sfSafariVC = [[SFSafariViewController alloc] initWithURL:url entersReaderIfAvailable:NO];
sfSafariVC.delegate = self;
[self presentViewController:sfSafariVC animated:YES completion:nil];

you can implement "safariViewController: didCompleteInitialLoad:" in SFSafariViewControllerDelegate.

Related

Video sharing with thumbnail and play button, using UIActivityViewController in ios7?

Want to share video using UIActivityViewController with its thumbnail and playbutton icon.
I used .mp4 url but facebook shows url not thumbnail.
how to make possible to show video(thumbnail with play button) on Facebook without using following code, because it doesnt work.
ALAsset * asset = [[ALAsset alloc] init];
[asset setVideoAtPath:urlToVideoFile completionBlock:NULL];
NSArray * activityItems = #[asset];
UIActivityViewController * activityController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:NULL]
The code which you have put is adding asset into array, don't put asset directly instead put url and try.

how can I send response 404?

I have page that when it load it first check in the database if the page exist.
The problem is that if the page does not exist the page is load with Response 200.
Is there a way to send to the header Response 404 from HTML page?
Or Can I call to a function in CGI that will do it?
Thanks!
I assume your page is not just HTML as you check in your database.
You just need to send an HTTP header 404 :
(example in PHP)
<?php
ob_start(); // Turn on output buffering to be sure not to send anything before the header
(your code)
ob_end_clean(); // clean the output buffer
header("HTTP/1.0 404 Not Found"); // send the header
?>

How to get driving directions within my app iOS6

I am using Apple Map's in my app and on my view i want to show the driving direction from the user location to the current location that i have on the view, now i just want all this inside my app only i.e i can show the driving direction's on the mapview, I have tried using the apple map application but after i make call to it from my application it takes me to apple's map application where i get the driving direction's but i can not return back into my application so i am thinking that i can do something in my application itself so that i can get the driving directions on my current view itself ..
NSString* addr = [NSString stringWithFormat:#"http://maps.apple.com/maps?daddr=%1.6f,%1.6f&saddr=%1.6f,%1.6f",coordinate.latitude,coordinate.longitude,mapView.userLocation.coordinate.latitude,mapView.userLocation.coordinate.longitude];
NSURL* url = [[NSURL alloc] initWithString:[addr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[[UIApplication sharedApplication] openURL:url];
this code takes me to the apple's map app from my native app but i can't return directly back to my app.is there a possible solution so that i can move back to my APP AFTER GETTING THE DRIVING DIRECTIONS ?? (webview didnot work for me.can i add a back button on apple's app or what ).Please help.... Thanks a lot !!
Or Please can any one suggest me a better code for implementing so that i can do all of that in my application only ?
I want an in-app map depicting the navigation routes and driving directions...
This is not the way to achieve the directions,
I have made a sample for you which covers all the iOS versions, New Google Maps and the iOS 6 tom tom maps as well.
Here it is:
if([[[UIDevice currentDevice] systemVersion] compare:#"6.0" options:NSNumericSearch] == NSOrderedDescending){
//6.0 or above
NSString *Destinationlatlong =[NSString stringWithFormat:#"%#,%#",your.latitude,your.longitude];
NSString* addr = [NSString stringWithFormat:#"comgooglemaps://?saddr=%f,%f&daddr=%#",[AppDelegate zDelegate].location.coordinate.latitude,[AppDelegate zDelegate].location.coordinate.longitude, Destinationlatlong];
addr=[addr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL* url = [[[NSURL alloc] initWithString:addr]autorelease];
// NSLog(#"url %#",url);
if ([[UIApplication sharedApplication]canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
}else{
CLLocationCoordinate2D coords =
CLLocationCoordinate2DMake([your.latitude doubleValue],[your.longitude doubleValue]);
MKPlacemark *placeMark = [[MKPlacemark alloc]
initWithCoordinate:coords addressDictionary:nil];
MKMapItem *destination = [[MKMapItem alloc]initWithPlacemark:placeMark];
[destination openInMapsWithLaunchOptions:nil];
}
}else{
NSString *Destinationlatlong =[NSString stringWithFormat:#"%#,%#",your.latitude,your.longitude];
NSString* addr = [NSString stringWithFormat:#"http://maps.google.com/maps?saddr=Current+Location&daddr=%#",Destinationlatlong];
addr=[addr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL* url = [[[NSURL alloc] initWithString:addr]autorelease];
// NSLog(#"url %#",url);
if ([[UIApplication sharedApplication]canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
}else{
UIAlertView *alert=[[[UIAlertView alloc] initWithTitle:#"Warning!" message:#"Device does not support this functionality" delegate:nil cancelButtonTitle:#"Okay" otherButtonTitles: nil]autorelease] ;
[alert show];
}
}
This should get you started. In short, get driving instruction from google api in json, parse it, and display it on your own map using MKPolyline
http://iosguy.com/2012/05/22/tracing-routes-with-mapkit/

IOS HTTP Request Example

I am trying make a call to the fitbit API.
I am unsure how to input the HTTP request shown below into my Objective C code in order to make this call and handle the response.
POST /oauth/request_token HTTP/1.1
Host: api.fitbit.com
Authorization: OAuth oauth_consumer_key="fitbit-example-client-application",
oauth_signature_method="HMAC-SHA1",
oauth_timestamp="1270248082",
oauth_nonce="161822064",
oauth_callback="http%3A%2F%2Fexample.fitbit.com%2Fapp%2FcompleteAuthorization",
oauth_signature="Omf%2Bls2gn%2BDlghq245LRIyfMdd8%3D"
oauth_version="1.0"
A simple example would be helpful. Thank you.
I suggest using an OAuth library to handle the OAuth signature generation. It can be a pain in the ass to wrangle the Authorization header. I've used oauthconsumer with success.
Code sample:
OAConsumer *consumer = [[OAConsumer alloc] initWithKey:oauthConsumerKey secret:oauthConsumerSecret];
OAToken *token = [[OAToken alloc] initWithKey:oauthAccessToken secret:oauthAccessTokenSecret];
OAHMAC_SHA1SignatureProvider *provider = [[OAHMAC_SHA1SignatureProvider alloc] init];
OAMutableURLRequest *request = [[OAMutableURLRequest alloc] initWithURL:[NSURL URLWithString:urlString] consumer:consumer token:token realm:nil signatureProvider:provider];
[request prepare];
NSHTTPURLResponse *response = nil;
NSError *error = nil;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
In this example, the 5 strings you will provide are:
oauthConsumerKey
oauthConsumerSecret
oauthAccessToken
oauthAccessTokenSecret
urlString
I am trying to do the same thing and oauthconsumer looks quite nice.
Is it because I am not getting the oauthAccessTokenSecret?
[edit] Yes, it was.
I keep getting:
"This page is no longer valid. It looks like you provided an invalid token or someone already used the token you provided. Please return to the site or application which sent you to this page and try again."
[edit] This is because it didn't have the correct token on the url string.
Hi you can get the working FitBit Oauth1.0 Authentication sample code from below link
https://github.com/KaranRajpoot/FitBit
Use OAuth.io, and the OAuth.io iOS SDK, to connect to FitBit.

How to set serviceUnavailableAlertEnabled in RestKit v0.20.0

In Restkit versions before v0.20.0 it used to be a simple enough to check for service unavailability and show appropriate responses.
objectManager.client.serviceUnavailableAlertEnabled = YES;
How can we achieve the same in latest RestKit?
Figured it out myself.
Since RKClient is no more in the latest RestKit it has been replaced by the AFHTTPClient from AFNetworking. The wrapper for reachability in AFNetworking is simple enough to use.
Firstly add the SystemConfiguration.framework to your project.
Then add #import <SystemConfiguration/SystemConfiguration.h> to your .pch file.
Finally register a callback block whenever the network reachability changes.
[objectManager.HTTPClient setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
if (status == AFNetworkReachabilityStatusNotReachable) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"No network connection"
message:#"You must be connected to the internet to use this app."
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
}];
This also works when the app is started when no internet connection is present.

Resources