Share kit integration into iOS - Dropbox Authentication on button press - ios

I’m trying to adapt the original Sharekit demo app so that it allows me to authenticate dropbox on a single button press (instead of appearing a new view etc. like it does in the demo).
To do this I have wired up a button with the following method as the IBAction (in the initial ViewController it launches into on Storyboard):
-(IBAction)assignDropbox:(id)sender
{
[SHK setRootViewController:self];
Class sharerClass = NSClassFromString(#"SHKDropbox");
BOOL isiOSSharer = [sharerClass isSubclassOfClass:[SHKiOSSharer class]];
if ([sharerClass isServiceAuthorized]) {
if (isiOSSharer) {
[sharerClass getUserInfo];
UIAlertView *iosSharerAlert = [[UIAlertView alloc] initWithTitle:#"iOS Social.framework sharer"
message:#"You can deauthorize this kind of sharer in settings.app, not here. By tapping this button you have refetched user info data only."
delegate:nil
cancelButtonTitle:nil
otherButtonTitles:#"OK", nil];
[iosSharerAlert show];
} else {
[sharerClass logout];
//[self.tableView reloadData];
}
} else {
SHKSharer *sharer = [[sharerClass alloc] init];
[sharer authorize];
if (isiOSSharer) {
UIAlertView *iosSharerAlert = [[UIAlertView alloc] initWithTitle:#"iOS Social.framework sharer"
message:#"You can authorize this kind of sharer in settings.app, not here."
delegate:nil
cancelButtonTitle:nil
otherButtonTitles:#"OK", nil];
[iosSharerAlert show];
}
}
if ([sharerClass isServiceAuthorized]) {
NSLog(#"After Logging in Username: %#", [sharerClass username]);
} else {
NSLog(#"After Logging in Username: %#", [sharerClass username]);
}
}
I’ve added the following notification code ViewDidLoad of the ViewController:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(authDidFinish:)
name:#"SHKAuthDidFinish"
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(sendDidCancel:)
name:#"SHKSendDidCancel"
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(sendDidStart:)
name:#"SHKSendDidStartNotification"
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(sendDidFinish:)
name:#"SHKSendDidFinish"
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(sendDidFailWithError:)
name:#"SHKSendDidFailWithError"
object:nil];
With the following code within the actual main (to make the notifications actually work - as per setup):
- (void)authDidFinish:(NSNotification*)notification
{
NSDictionary *userInfo = notification.userInfo;
NSNumber *success = [userInfo objectForKey:#"success"];
if (NO == [success boolValue]) {
NSLog(#"authDidFinish: NO");
} else {
NSLog(#"authDidFinish: YES");
NSLog(#"userInfo: %#", userInfo);
}
}
- (void)sendDidCancel:(NSNotification*)notification
{
NSLog(#"sendDidCancel:");
}
- (void)sendDidStart:(NSNotification*)notification
{
NSLog(#"sendDidStart:");
}
- (void)sendDidFinish:(NSNotification*)notification
{
NSLog(#"sendDidFinish:");
}
- (void)sendDidFailWithError:(NSNotification*)notification
{
NSLog(#"sendDidFailWithError:");
}'
It launches the web view in the app, and I can authenticate (I get notifications from dropbox saying another device has logged on), however the save authentication/session code in ‘SHKDropbox.m’ is never called.
Method under:
- (void)restClient:(DBRestClient*)client loadedAccountInfo:(DBAccountInfo*)info
I’ve done a lot of comparison work to see what the differences are between the projects, but can’t see any difference in settings.
Have you got any storyboard/button presses sample code as described above. Or have you got any idea on why this isn’t working on ShareKit?

Related

PushNotification redirect to respected viewcontroller

I am using push notification in my application. My question is, if user receives a push notification out of the app, user should be able to click that notification banner and be
brought to the activities page where notification is displayed.
Anybody can help me how to solve this issue. I am new in push notification.
//Use this code in your AppDelegate.m
-(void)application:(UIApplication *)app didReceiveRemoteNotification:(NSDictionary *)userInfo
{
//code for navigate to viewcontroller
ActivityViewController *objActivity = [[ActivityViewController alloc] initWithNibName:#"ActivityViewController" bundle:nil];
[self.navigationcontroller pushViewController: objActivity animated:YES];
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
NSString *aPayload=[userInfo objectForKey:#"payload"];
NSDictionary *JSON =
[NSJSONSerialization JSONObjectWithData: [aPayload dataUsingEncoding:NSUTF8StringEncoding]
options: NSJSONReadingMutableContainers
error: NULL];
if ([[JSON objectForKey:#"activity"]isEqualToString:#"encore"]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Piggyback"
message: [[userInfo objectForKey:#"aps"] objectForKey:#"alert"]
delegate: nil
cancelButtonTitle:#"DONE"
otherButtonTitles: nil];
self.likeBedge=self.likeBedge+1;
[alert show];
[[NSNotificationCenter defaultCenter] postNotificationName:#"likeComment" object:self userInfo:userInfo];
}
else if ([[JSON objectForKey:#"activity"]isEqualToString:#"like"]||[[JSON objectForKey:#"activity"]isEqualToString:#"comment"]){
self.likeBedge=self.likeBedge+1;
[[NSNotificationCenter defaultCenter] postNotificationName:#"likeComment" object:self userInfo:userInfo];
}
else if ([[JSON objectForKey:#"activity"]isEqualToString:#"started playing"]){
self.postBedge=self.postBedge+1;
NSLog(#"%d",self.postBedge);
[[NSNotificationCenter defaultCenter] postNotificationName:#"started playing" object:self userInfo:userInfo];
}
else if ([[JSON objectForKey:#"activity"]isEqualToString:#"stopped playing"]){
[[NSNotificationCenter defaultCenter] postNotificationName:#"stopped playing" object:self userInfo:userInfo];
}
else if ([[JSON objectForKey:#"activity"]isEqualToString:#"#user play"]||[[JSON objectForKey:#"activity"]isEqualToString:#"#user comment"]){
[[NSNotificationCenter defaultCenter] postNotificationName:#"likeComment" object:self userInfo:userInfo];
}
return;
}
i have already done this above code but when app is in background and if notification is come user tap on banner it should redirect to activity page
So when the application goes to background and you receive a notification the method you are going to call in appdelegate.m file is
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
// Here present the class you want
}

xcode - refresh tableview after called method from anther class

i create button to add student , i make method handle that by showing pop menu(alert view)
then get the code entered in variable called result (NSString) then called web service get required data from my web service
- (IBAction)add_button:(id)sender {
[self addStudent];
}
- (void) addStudent {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Add Student" message:#"Enter Connection Code" delegate:self cancelButtonTitle:#"Dismiss" otherButtonTitles:#"Add", nil];
[alert setAlertViewStyle:UIAlertViewStylePlainTextInput];
[alert show];
}
-(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
result = [[alertView textFieldAtIndex:0] text];
NSLog(#"the result connection code %#",result);
WebService *web = [[WebService alloc]init];
[web retriveDataWithCodeAndUrl:result Andurl:STUDENT_URL];
}
in web service class
after finish loading and get required data
i need back my tableview and refresh it with new data
i try protocol but it doent work so i try make instance of my tableview class
, i try also make notification to handle it and refresh view
(webservice class)
-(void) connectionDidFinishLoading :(NSURLConnection *) connection{
NSLog(#"data finish loading ");
School_TableViewController *home = [[School_TableViewController alloc]init];
[home didFinishWithData:respone];
[[self delegate]didFinishLoadingwithData:respone];
[[NSNotificationCenter defaultCenter]
postNotificationName:#"TestNotification"
object:self];
}
back to my table view class to handle notification and refresh table
- (void) receiveTestNotification:(NSNotification *) notification
{
if ([[notification name] isEqualToString:#"TestNotification"])
NSLog (#"Successfully received the test notification!");
[self.tableView reloadData];
}
but it dosnt work , i need nice way to communicate between singleton and tableview
and method to refresh view
thank you in advance
Certainly It doen't work because you didn't update data for your tableView
and I wonder why you didn't use the delegate didFinishLoadingwithData
use delegate:
-In School_TableViewController.h:
#interface School_TableViewController : UITableViewController <YourWebServiceDelegate>
-In School_TableViewController.m:
-(void) didFinishLoadingwithData:(NSData*) data {
// Do parse respone data method and update yourTableViewData
yourtableViewData = notification.object
[self.tableView reloadData];
}
or try
-In service class
-(void) connectionDidFinishLoading :(NSURLConnection *) connection{
NSLog(#"data finish loading ");
/* remove this code you don't need alloc School_TableViewController
School_TableViewController *home = [[School_TableViewController alloc]init];
[home didFinishWithData:respone];
*/
[[self delegate]didFinishLoadingwithData:respone];
[[NSNotificationCenter defaultCenter]
postNotificationName:#"TestNotification"
object:response];
}
-In your tableView
- (void) receiveTestNotification:(NSNotification *) notification
{
if ([[notification name] isEqualToString:#"TestNotification"])
NSLog (#"Successfully received the test notification!");
// Do parse respone data method and update yourTableViewData
yourtableViewData = notification.object
[self.tableView reloadData];
}

MCSession ofen can't reconnect when app return to foreground from background

I'm using Mutipeer Connectivity to make a chat app. At first, everything worked well and MCSession can connect with each other. But when user tap home button and then tap app icon, the apps disconnect with each other and often can't reconnect. But sometimes it can reconnect. The success is random.
- (void)viewDidLoad
{
[super viewDidLoad];
...
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(DidBecomeActive:)
name: UIApplicationDidBecomeActiveNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(didEnterBackground:)
name: UIApplicationDidEnterBackgroundNotification object:nil];
NSString *displayname=[[NSUUID UUID] UUIDString];
_MyPeerID=[[MCPeerID alloc] initWithDisplayName:displayname];
}
- (void)DidBecomeActive:(NSNotification *)notification
{
if (notFirstLaunch==YES) {
NSLog(#"DidBecomeActive");
NSString *displayname=[[NSUUID UUID] UUIDString];
_MyPeerID=[[MCPeerID alloc] initWithDisplayName:displayname];
[self createSession];
[self createAdvertiser];
[self beginBrowsing];
}
notFirstLaunch=YES;
}
- (void)didEnterBackground:(NSNotification *)notification
{
NSLog(#"didEnterBackground");
[_MySession disconnect];
_MySession=nil;
_MyBrowser=nil;
_MyAdver=nil;
}
- (void)createSession
{
_MySession=[[MCSession alloc] initWithPeer:_MyPeerID];
_MySession.delegate=self;
}
- (void)createAdvertiser
{
_MyAdver=[[MCNearbyServiceAdvertiser alloc] initWithPeer:_MyPeerID discoveryInfo:nil serviceType:MyServiceType];
_MyAdver.delegate=self;
[_MyAdver startAdvertisingPeer];
}
- (void)beginBrowsing {
_MyBrowser=[[MCNearbyServiceBrowser alloc] initWithPeer:_MyPeerID serviceType:MyServiceType];
_MyBrowser.delegate=self;
[_MyBrowser startBrowsingForPeers];
}
Judging by the code you've posted, nothing reconnects because in didEnterBackground you disconnect from the session and destroy the session, browser and advertiser. In DidBecomeActive you have a guard (notFirstLaunch) that means that the session and advertiser are only created the first time you become active, and NOT after you become active when you come back from being in the background.

Integrate Box IOS sdk in my ios project

I'm trying to integrate BOX V2 IOS SDk on my ios project , the integration is fine, but when I try to login , and after I enter my username and password and granted the access , I get a white screen , and the boxAPIAuthenticationDidSucceed method is not called , her is my code
the connexion Method :
-(void) connectToBox {
[BoxSDK sharedSDK].OAuth2Session.clientID = #"my-client-id";
[BoxSDK sharedSDK].OAuth2Session.clientSecret = #"my-client-secret";
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(boxAPIAuthenticationDidSucceed:)
name:BoxOAuth2SessionDidBecomeAuthenticatedNotification
object:[BoxSDK sharedSDK].OAuth2Session];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(boxAPIAuthenticationDidFail:)
name:BoxOAuth2SessionDidReceiveAuthenticationErrorNotification
object:[BoxSDK sharedSDK].OAuth2Session];
self.LoginCotroller = [[BoxAuthorizationViewController alloc] initWithAuthorizationURL:authorizationURL redirectURI:redirectURI];
[self presentViewController:self.LoginCotroller animated:YES completion:nil];
}
and I implement the two methods :
- (void)boxAPIAuthenticationDidSucceed:(NSNotification *)notification;
- (void)boxAPIAuthenticationDidFail:(NSNotification *)notification;
and the notifications methods :
#pragma mark - Handle OAuth2 session notifications
- (void)boxAPIAuthenticationDidSucceed:(NSNotification *)notification
{
BoxOAuth2Session *session = (BoxOAuth2Session *) notification.object;
NSLog(#"Received OAuth2 successfully authenticated notification");
NSLog(#"Access token (%#) expires at %#", session.accessToken, session.accessTokenExpiration);
NSLog(#"Refresh token (%#)", session.refreshToken);
dispatch_sync(dispatch_get_main_queue(), ^{
[self.LoginCotroller dismissViewControllerAnimated:YES completion:nil];
});
}
- (void)boxAPIAuthenticationDidFail:(NSNotification *)notification
{
NSLog(#"Received OAuth2 failed authenticated notification");
NSString *oauth2Error = [[notification userInfo] valueForKey:BoxOAuth2AuthenticationErrorKey];
NSLog(#"Authentication error (%#)", oauth2Error);
dispatch_sync(dispatch_get_main_queue(), ^{
[self dismissViewControllerAnimated:YES completion:nil];
});
}
I dont know what wrong with my code.So if any one can help.Thanks
I'd suggest adding your controller as an observer for BoxOAuth2SessionDidRefreshTokensNotification as well to call boxAPIAuthenticationDidSucceed: method. BoxOAuth2SessionDidRefreshTokensNotification is posted when a new access-token is created/refresed.

How to correctly setup NSNotification and post?

I want to make a singleton Reachability observer for each connection delegate to use,
however I couldn't build it correctly, here is some snippet of code.
MYReachability.m
static MYReachability *sharedInstance;
+ (MYReachability *)sharedInstance
{
if (sharedInstance == NULL) {
sharedInstance = [[MYReachability alloc] init];
}
return sharedInstance;
}
- (void)addReachabilityObserver
{
[[NSNotificationCenter defaultCenter] addObserver: self
selector: #selector(reachabilityChanged:)
name: kReachabilityChangedNotification
object: nil];
Reachability *reach = [Reachability reachabilityWithHostname: #"www.apple.com"];
[reach startNotifier];
}
- (void) reachabilityChanged: (NSNotification *)notification {
NSLog(#"notification: %#", notification);
Reachability *reach = [notification object];
if( [reach isKindOfClass: [Reachability class]]) {
NetworkStatus status = [reach currentReachabilityStatus];
NSLog(#"reachability status: %u", status);
if (status == NotReachable) {
// Insert your code here
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error"
message:#"Cannot connect to server..."
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
}
}
in MYConnectionDelegate.m
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
if (error.code) {
// sending notification to viewController
[[MYReachability sharedInstance] addReachabilityObserver];
MYTestClass *myTestClass = [MYTestClass new];
[myTestClass notificationTrigger];
}
}
in MYTestClass.m
- (void)notificationTrigger
{
// All instances of TestClass will be notified
[[NSNotificationCenter defaultCenter]
postNotificationName:kReachabilityChangedNotification
object:self];
}
Rick, Thanks, that works, but here comes another problem,
each time it calls will generates one more notifications into stack...
I made a dealloc in MYReachability.m
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
but previous notification still exists.
You're calling the method notificationTrigger but your test class only has the method notificationTrigger: with a colon, i.e. one parameter.
Change [myTestClass notificationTrigger]; to [myTestClass notificationTrigger:self]; and it should work.
If you only want the notification to show once, remove yourself as an observer once the alert view has been shown, like this:
- (void) reachabilityChanged: (NSNotification *)notification {
NSLog(#"notification: %#", notification);
Reachability *reach = [notification object];
if( [reach isKindOfClass: [Reachability class]]) {
NetworkStatus status = [reach currentReachabilityStatus];
NSLog(#"reachability status: %u", status);
if (status == NotReachable) {
// Insert your code here
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Cannot connect to server..." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
}
}
In one class:
[[NSNotificationCenter defaultCenter] postNotificationName:#"notifyMe" object:self];
In another class:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(doSomething)
name:#"notifyMe"
object:nil];

Resources