ios - Send device token to PHP script via UIWebView -
i working on transit web app used san diego's metropolitan transit system. ios, planning on using tabbar 3 items, [home] [favorites] [notifications]. [home] leads main page (and other pages), [favorites] can setup favorite bus/trolley stops.
i planning on having [favorites] can setup notification sent before bus arrives @ scheduled times provided mts. example, if bus come @ 10:30 , set notification arrive 5 minutes before alert you, 1 @ 10:25.
i wanting link accounts device tokens, , read of questions on here such linking user account device token push notifications.
i believe possible somehow link accounts sending device token uiwebview when login.
hope can me.
edit: below obj-c code in home.m
#import "home.h" @implementation homeviewcontroller - (void)viewdidload { [super viewdidload]; nsurl *url1 = [nsurl urlwithstring:@"url"]; nsurlrequest *request1 = [nsurlrequest requestwithurl:url1]; [webview1 loadrequest:request1]; } - (void)webviewdidfinishload:(uiwebview *)webview { nslog(@"1"); nsuserdefaults *defaults =[nsuserdefaults standarduserdefaults]; nsstring *devicetoken = [defaults objectforkey:@"devicetoken"]; bool tokenissent = [defaults boolforkey:@"tokenissent"]; nsstring *newtoken = [defaults stringforkey:@"newtoken"]; nslog(@"2"); nsstring *urlstring = [nsstring stringwithformat:@"url"]; nsurl *url = [[nsurl alloc] initwithstring:urlstring]; nsurlrequest *urlrequest = [nsurlrequest requestwithurl:url]; nsdata *usernamedata; nsurlresponse *response; usernamedata = [nsurlconnection sendsynchronousrequest:urlrequest returningresponse:&response error:nil]; nsstring *username = [[nsstring alloc] initwithdata:usernamedata encoding:nsutf8stringencoding]; nslog(@"3"); if (devicetoken != nil && tokenissent == no) { nslog(@"4"); nsstring *urlstring = [nsstring stringwithformat:@"url/s=%@&u=%@",newtoken, username]; nsurl *url = [[nsurl alloc] initwithstring:urlstring]; nsurlrequest *urlrequest = [nsurlrequest requestwithurl:url]; nsdata *urldata; nsurlresponse *response; urldata = [nsurlconnection sendsynchronousrequest:urlrequest returningresponse:&response error:nil]; nsstring *info = [[nsstring alloc] initwithdata:urldata encoding:nsutf8stringencoding]; if([info isequal: @"success"]){ [defaults setbool:yes forkey:@"tokenissent"]; nslog(@"5"); }else{ [defaults setbool:no forkey:@"tokenissent"]; nslog(@"6"); } [defaults synchronize]; } else { nslog(@"7"); } }
first save token nsuserdefaults
-(void)application:(uiapplication *)application didregisterforremotenotificationswithdevicetoken:(nsdata *)devicetoken { nsuserdefaults *defaults = [nsuserdefaults standarduserdefaults]; [defaults setobject:devicetoken forkey:@"devicetoken"]; [defaults setbool:no forkey:@"tokenissent"]; [defaults synchronize]; }
because done in appdelegate
, @ point user not logged in yet, that's why need track using value in example above tokenissent
, set no (false).
now once user authenticates, can send sever , associated user.
nsuserdefaults *defaults =[nsuserdefaults standarduserdefaults]; bool devicetoken = [defaults boolforkey:@"devicetoken"]; bool tokenissent = [defaults boolforkey:@"tokenissent"]; if (devicetoken != nil && tokenissent) { // not null , , not sent php send if(success){ [defaults setbool:yes forkey:@"tokenissent"]; }else{ [defaults setbool:no forkey:@"tokenissent"]; } [defaults synchronize]; }
if delgate not being called need implement it:
@interface homeviewcontroller : uiviewcontroller<uiwebviewdelegate>
and make sure assign delegate.
- (void)viewdidload { [super viewdidload]; //assign delegate webview1 webview1.delegate = self //perform request nsurl *url1 = [nsurl urlwithstring:@"http://clovercloud.net/app/f/"]; nsurlrequest *request1 = [nsurlrequest requestwithurl:url1]; [webview1 loadrequest:request1]; }
Comments
Post a Comment